player.gd 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. extends Node2D
  2. const tile_size = 16
  3. var direction : Vector2 = Vector2.DOWN
  4. var coords : Vector2 = Vector2(1, 0)
  5. var hunger : int = 30
  6. @onready var animation = $AnimationPlayer
  7. var tween
  8. signal command_finished
  9. # Called when the node enters the scene tree for the first time.
  10. func _ready() -> void:
  11. animation.play("idle_down")
  12. func move() -> void:
  13. tween = get_tree().create_tween()
  14. match direction:
  15. Vector2.DOWN:
  16. animation.play("run_down")
  17. Vector2.UP:
  18. animation.play("run_up")
  19. Vector2.LEFT:
  20. animation.play("run_left")
  21. Vector2.RIGHT:
  22. animation.play("run_right")
  23. tween.tween_property(self, "position", position + direction * tile_size, 1.5)
  24. coords += direction
  25. hunger -= 1
  26. await tween.finished
  27. command_finished.emit()
  28. func die() -> void:
  29. if hunger == 0:
  30. match direction:
  31. Vector2.DOWN:
  32. animation.play("die_down")
  33. Vector2.UP:
  34. animation.play("die_up")
  35. Vector2.LEFT:
  36. animation.play("die_left")
  37. Vector2.RIGHT:
  38. animation.play("die_right")
  39. func rotate_left() -> void:
  40. match direction:
  41. Vector2.DOWN:
  42. animation.play("rotate_down_left")
  43. direction = Vector2.RIGHT
  44. Vector2.UP:
  45. animation.play("rotate_up_left")
  46. direction = Vector2.LEFT
  47. Vector2.LEFT:
  48. animation.play("rotate_left_left")
  49. direction = Vector2.DOWN
  50. Vector2.RIGHT:
  51. animation.play("rotate_right_left")
  52. direction = Vector2.UP
  53. await animation.animation_finished
  54. command_finished.emit()
  55. func rotate_right() -> void:
  56. match direction:
  57. Vector2.DOWN:
  58. animation.play("rotate_down_right")
  59. direction = Vector2.LEFT
  60. Vector2.UP:
  61. animation.play("rotate_up_right")
  62. direction = Vector2.RIGHT
  63. Vector2.LEFT:
  64. animation.play("rotate_left_right")
  65. direction = Vector2.UP
  66. Vector2.RIGHT:
  67. animation.play("rotate_right_right")
  68. direction = Vector2.DOWN
  69. await animation.animation_finished
  70. command_finished.emit()