37 lines
1.2 KiB
GDScript
37 lines
1.2 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)
|
|
|
|
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())
|