walker.gd 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. extends Pawn
  2. #warning-ignore:unused_class_variable
  3. @export var combat_actor: PackedScene
  4. #warning-ignore:unused_class_variable
  5. var lost = false
  6. var grid_size
  7. @onready var parent = get_parent()
  8. @onready var animation_playback = $AnimationTree.get("parameters/playback")
  9. @onready var walk_animation_time = $AnimationPlayer.get_animation("walk").length
  10. func _ready():
  11. update_look_direction(Vector2.RIGHT)
  12. grid_size = parent.tile_set.tile_size.x
  13. func _process(_delta):
  14. var input_direction = get_input_direction()
  15. if input_direction.is_zero_approx():
  16. return
  17. update_look_direction(input_direction)
  18. var target_position = parent.request_move(self, input_direction)
  19. if target_position:
  20. move_to(target_position)
  21. elif active:
  22. bump()
  23. func get_input_direction():
  24. return Vector2(
  25. Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
  26. Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
  27. )
  28. func update_look_direction(direction):
  29. $Pivot/Sprite2D.rotation = direction.angle()
  30. func move_to(target_position):
  31. set_process(false)
  32. var move_direction = (target_position - position).normalized()
  33. animation_playback.start("walk")
  34. var tween := create_tween()
  35. tween.set_ease(Tween.EASE_IN)
  36. var end = $Pivot.position + move_direction * grid_size
  37. tween.tween_property($Pivot, "position", end, walk_animation_time)
  38. await tween.finished
  39. $Pivot.position = Vector2.ZERO
  40. position = target_position
  41. animation_playback.start("idle")
  42. set_process(true)
  43. func bump():
  44. set_process(false)
  45. animation_playback.start("bump")
  46. await $AnimationTree.animation_finished
  47. animation_playback.start("idle")
  48. set_process(true)