actor.gd 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. extends Pawn
  2. var lost = false
  3. @onready var Grid = get_parent()
  4. func _ready():
  5. update_look_direction(Vector2.RIGHT)
  6. func _process(delta):
  7. var input_direction = get_input_direction()
  8. if not input_direction:
  9. return
  10. update_look_direction(input_direction)
  11. var target_position = Grid.request_move(self, input_direction)
  12. if target_position:
  13. move_to(target_position)
  14. else:
  15. bump()
  16. func get_input_direction():
  17. return Vector2(
  18. Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left"),
  19. Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
  20. )
  21. func update_look_direction(direction):
  22. $Pivot/Sprite2D.rotation = direction.angle()
  23. func move_to(target_position):
  24. set_process(false)
  25. $AnimationPlayer.play("walk")
  26. var move_direction = (position - target_position).normalized()
  27. var tween := create_tween()
  28. tween.set_ease(Tween.EASE_IN)
  29. tween.tween_property($Pivot, "position", $Pivot.position + move_direction * 32, $AnimationPlayer.current_animation_length)
  30. $Pivot/Sprite2D.position = position - target_position
  31. position = target_position
  32. await $AnimationPlayer.animation_finished
  33. set_process(true)
  34. func bump():
  35. $AnimationPlayer.play("bump")