31 lines
481 B
GDScript

class_name HealthComponent
extends Node
signal value_changed
signal death
const DEATH_THRESHOLD = 0.01
@export var max_value: float
var value: float
var is_alive: bool
func _ready():
restore()
func restore():
is_alive = true
value = max_value
value_changed.emit()
func take_damage(attack: DamageInstance):
if !is_alive:
return
value = clampf(value - attack.damage, 0, max_value)
value_changed.emit()
if value < DEATH_THRESHOLD:
is_alive = false
death.emit()