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 19:38:17 +00:00
|
|
|
var is_alive: bool
|
2024-02-12 23:22:27 +00:00
|
|
|
|
|
|
|
func _ready():
|
2024-02-13 12:49:28 +00:00
|
|
|
restore()
|
|
|
|
|
|
|
|
func restore():
|
2024-02-13 19:38:17 +00:00
|
|
|
is_alive = true
|
2024-02-12 23:22:27 +00:00
|
|
|
value = max_value
|
|
|
|
value_changed.emit()
|
|
|
|
|
|
|
|
func take_damage(attack: DamageInstance):
|
2024-02-13 19:38:17 +00:00
|
|
|
if !is_alive:
|
2024-02-13 12:49:28 +00:00
|
|
|
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 19:38:17 +00:00
|
|
|
is_alive = false
|
2024-02-13 10:17:45 +00:00
|
|
|
death.emit()
|