last init
This commit is contained in:
33
scripts/components/health.gd
Normal file
33
scripts/components/health.gd
Normal file
@@ -0,0 +1,33 @@
|
||||
extends Node
|
||||
|
||||
@export var max_health := 100.0
|
||||
const HEALTH_REGEN := 1.0
|
||||
var current_health: float
|
||||
|
||||
func _ready() -> void:
|
||||
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:
|
||||
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())
|
||||
Reference in New Issue
Block a user