the-crypt/ai/Skeleton Enemy.gd

64 lines
1.8 KiB
GDScript3
Raw Normal View History

2020-07-20 14:14:34 -05:00
extends Enemy
2020-07-18 02:27:01 -05:00
2020-07-18 17:07:40 -05:00
var Spinning_Bone = preload("res://ai/SpinningBone.tscn")
onready var ShootDelay : Timer = $ShootDelay
onready var ProjectileSpawn: Node2D = $Position2D
2020-07-18 02:27:01 -05:00
func apply_gravity(delta, modifier = 1):
velocity.y += gravity * delta * modifier
func _physics_process(_delta):
2020-07-21 01:07:58 -05:00
var dist = global_position.distance_to(Player.position)
var gravity = 4350
2020-07-20 23:24:09 -05:00
if knockback != Vector2.ZERO:
knockback = lerp(knockback, Vector2.ZERO, 0.5)
move_and_slide(-knockback * knockback_amount)
if knockback.x < 1 and -1 < knockback.x and knockback.y < 1 and -1 < knockback.y:
knockback = Vector2.ZERO
return
2020-07-20 15:06:03 -05:00
if $RayCast2D.get_collider() != null && $RayCast2D.get_collider().has_method("get_type") and $RayCast2D.get_collider().get_type() == "player":
velocity = Vector2(0, 0)
throw_bone(Vector2.LEFT)
elif $RayCast2D2.get_collider() != null && $RayCast2D2.get_collider().has_method("get_type") and $RayCast2D2.get_collider().get_type() == "player":
velocity = Vector2(0, 0)
throw_bone(Vector2.RIGHT)
else:
apply_gravity(_delta)
velocity.x *= speed
velocity = move_and_slide(velocity, UP)
2020-07-20 15:06:03 -05:00
if dist <= 400:
velocity.x = position.direction_to(Player.position).normalized().x
2020-07-20 15:06:03 -05:00
else:
2020-07-21 01:07:58 -05:00
velocity = Vector2(0, 0)
2020-07-20 15:06:03 -05:00
2020-07-18 17:07:40 -05:00
if velocity.x > 0:
$Skeleton.flip_h = false
elif velocity.x < 0:
$Skeleton.flip_h = true
2020-07-21 01:07:58 -05:00
2020-07-20 15:06:03 -05:00
# Kill
2020-07-20 14:30:58 -05:00
if health <= 0:
queue_free()
2020-07-20 15:06:03 -05:00
# Jump
2020-07-18 17:07:40 -05:00
if is_on_wall() and is_on_floor():
velocity.y = -90
2020-07-20 14:14:34 -05:00
2020-07-20 15:06:03 -05:00
func throw_bone(direction):
if ShootDelay.is_stopped():
var temp = Spinning_Bone.instance()
get_tree().current_scene.add_child(temp)
temp.global_position = ProjectileSpawn.global_position
temp.launch_vector(direction)
ShootDelay.start()
2020-07-21 01:07:58 -05:00
func do_knockback(normal):
if $Skeleton.flip_h == true:
knockback = Vector2(-4, -4)
else:
knockback = Vector2(4, 0)
2020-07-20 15:06:03 -05:00