Health.gd 508 B

1234567891011121314151617181920212223242526272829
  1. extends Node
  2. @export var max_health: int = 10
  3. @export var invulnerable_time = 1
  4. var invulnerable = false
  5. signal no_health
  6. @onready var health: int = max_health
  7. func hurt() -> void:
  8. if invulnerable or health <= 0:
  9. return
  10. invulnerable = true
  11. health -= 1
  12. print ("health: " + str(health))
  13. if health == 0:
  14. emit_signal("no_health")
  15. await get_tree().create_timer(invulnerable_time).timeout
  16. invulnerable = false
  17. func kill() -> void:
  18. if health <= 0:
  19. return
  20. health = 0
  21. emit_signal("no_health")