45 lines
1.1 KiB
GDScript
45 lines
1.1 KiB
GDScript
|
extends KinematicBody2D
|
||
|
|
||
|
const SPEED := 50
|
||
|
|
||
|
var _velocity := Vector2.ZERO
|
||
|
|
||
|
onready var _agent: NavigationAgent2D = $NavigationAgent2D
|
||
|
|
||
|
var target: Node2D = null
|
||
|
|
||
|
# Called when the node enters the scene tree for the first time.
|
||
|
func _ready():
|
||
|
call_deferred("_find_target")
|
||
|
|
||
|
func _find_target() -> void:
|
||
|
target = find_parent("YSort").get_node("Player")
|
||
|
_agent.set_target_location(target.global_position)
|
||
|
|
||
|
func _at_target() -> void:
|
||
|
print("at target")
|
||
|
pass
|
||
|
|
||
|
func _physics_process(delta: float) -> void:
|
||
|
if target == null:
|
||
|
if $AnimatedSprite.animation != "still":
|
||
|
$AnimatedSprite.play("still")
|
||
|
return
|
||
|
|
||
|
var target_velocity = Vector2.ZERO
|
||
|
if not _agent.is_navigation_finished():
|
||
|
var direction_towards_travel := global_position.direction_to(_agent.get_next_location())
|
||
|
target_velocity = direction_towards_travel * SPEED
|
||
|
|
||
|
_velocity += (target_velocity - _velocity) * delta * 2.0
|
||
|
|
||
|
if _velocity.length_squared() > 16:
|
||
|
if $AnimatedSprite.animation != "crawl":
|
||
|
$AnimatedSprite.play("crawl")
|
||
|
else:
|
||
|
if $AnimatedSprite.animation != "still":
|
||
|
$AnimatedSprite.play("still")
|
||
|
|
||
|
#print(_velocity)
|
||
|
var _vel := move_and_slide(_velocity)
|