31 lines
476 B
GDScript
31 lines
476 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_dead: bool
|
|
|
|
func _ready():
|
|
restore()
|
|
|
|
func restore():
|
|
is_dead = false
|
|
value = max_value
|
|
value_changed.emit()
|
|
|
|
func take_damage(attack: DamageInstance):
|
|
if is_dead:
|
|
return
|
|
|
|
value = clampf(value - attack.damage, 0, max_value)
|
|
value_changed.emit()
|
|
|
|
if value < DEATH_THRESHOLD:
|
|
is_dead = true
|
|
death.emit()
|