Files
mmo/scripts/components/health.gd
Marek Lenczewski 04749104a0 update
2026-03-30 22:56:58 +02:00

46 lines
1.5 KiB
GDScript

extends Node
@export var stats: EntityStats
var max_health: float
var health_regen: float
var current_health: float
func _ready() -> void:
max_health = stats.max_health
health_regen = stats.health_regen
current_health = max_health
EventBus.damage_requested.connect(_on_damage_requested)
EventBus.heal_requested.connect(_on_heal_requested)
func _process(delta: float) -> void:
if current_health > 0 and current_health < max_health and health_regen > 0:
current_health = min(current_health + health_regen * delta, max_health)
EventBus.health_changed.emit(get_parent(), current_health, max_health)
func _on_damage_requested(attacker: Node, target: Node, amount: float) -> void:
if target != get_parent():
return
var remaining: float = amount
var shield: Node = get_parent().get_node_or_null("Shield")
if shield:
remaining = shield.absorb(remaining)
EventBus.damage_dealt.emit(attacker, get_parent(), amount)
if remaining > 0:
take_damage(remaining, attacker)
func take_damage(amount: float, attacker: Node) -> void:
current_health -= amount
if current_health <= 0:
current_health = 0
EventBus.health_changed.emit(get_parent(), current_health, max_health)
if current_health <= 0:
EventBus.entity_died.emit(get_parent())
func heal(amount: float) -> void:
current_health = min(current_health + amount, max_health)
EventBus.health_changed.emit(get_parent(), current_health, max_health)
func _on_heal_requested(healer: Node, target: Node, amount: float) -> void:
if target == get_parent():
heal(amount)