player.gd 969 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. class_name Player extends CharacterBody2D
  2. ## Movement speed in pixels per second.
  3. const MOVEMENT_SPEED = 240.0
  4. var health := 100.0:
  5. get:
  6. return health
  7. set(value):
  8. health = value
  9. progress_bar.value = value
  10. if health <= 0.0:
  11. # The player died.
  12. get_tree().reload_current_scene()
  13. var motion := Vector2()
  14. @onready var progress_bar := $ProgressBar as ProgressBar
  15. @onready var sprite := $Sprite2D as Sprite2D
  16. func _process(_delta: float):
  17. velocity = Input.get_vector(&"move_left", &"move_right", &"move_up", &"move_down")
  18. if velocity.length_squared() > 1.0:
  19. velocity = velocity.normalized()
  20. velocity *= MOVEMENT_SPEED
  21. move_and_slide()
  22. func _input(event: InputEvent):
  23. if event.is_action_pressed(&"move_left"):
  24. sprite.rotation = PI / 2
  25. elif event.is_action_pressed(&"move_right"):
  26. sprite.rotation = -PI / 2
  27. elif event.is_action_pressed(&"move_up"):
  28. sprite.rotation = PI
  29. elif event.is_action_pressed(&"move_down"):
  30. sprite.rotation = 0.0