31 lines
476 B
GDScript3
Raw Normal View History

2024-02-12 23:22:27 +00:00
class_name HealthComponent
extends Node
signal value_changed
2024-02-13 10:17:45 +00:00
signal death
const DEATH_THRESHOLD = 0.01
2024-02-12 23:22:27 +00:00
@export var max_value: float
var value: float
2024-02-13 12:49:28 +00:00
var is_dead: bool
2024-02-12 23:22:27 +00:00
func _ready():
2024-02-13 12:49:28 +00:00
restore()
func restore():
is_dead = false
2024-02-12 23:22:27 +00:00
value = max_value
value_changed.emit()
func take_damage(attack: DamageInstance):
2024-02-13 12:49:28 +00:00
if is_dead:
return
2024-02-13 10:17:45 +00:00
value = clampf(value - attack.damage, 0, max_value)
2024-02-12 23:22:27 +00:00
value_changed.emit()
2024-02-13 10:17:45 +00:00
if value < DEATH_THRESHOLD:
2024-02-13 12:49:28 +00:00
is_dead = true
2024-02-13 10:17:45 +00:00
death.emit()