26 lines
626 B
GDScript
26 lines
626 B
GDScript
class_name HealthComponent extends Node
|
|
|
|
@export var max_health:float = 100
|
|
var _health = 100
|
|
|
|
signal health_changed(current_health:float)
|
|
|
|
# Called when the node enters the scene tree for the first time.
|
|
func _ready() -> void:
|
|
_health = max_health
|
|
|
|
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
|
func _process(_delta: float) -> void:
|
|
pass
|
|
|
|
func add_health(amount:float):
|
|
_health = max(_health + amount, max_health)
|
|
health_changed.emit(_health)
|
|
|
|
func subtract_health(amount:float):
|
|
_health = max(_health - amount, 0)
|
|
health_changed.emit(_health)
|
|
|
|
func get_health() -> float:
|
|
return _health
|