health.gd 482 B

1234567891011121314151617181920212223242526272829303132
  1. extends Node
  2. signal dead
  3. signal health_changed(life)
  4. @export var life = 0
  5. @export var max_life = 10
  6. @export var base_armor = 0
  7. var armor = 0
  8. func _ready():
  9. armor = base_armor
  10. func take_damage(damage):
  11. life = life - damage + armor
  12. if life <= 0:
  13. emit_signal("dead")
  14. else:
  15. emit_signal("health_changed", life)
  16. func heal(amount):
  17. life += amount
  18. life = clamp(life, life, max_life)
  19. emit_signal("health_changed", life)
  20. func get_health_ratio():
  21. return life / max_life