update
This commit is contained in:
@@ -1,14 +1,25 @@
|
||||
extends Node
|
||||
|
||||
func _ready() -> void:
|
||||
EventBus.damage_requested.connect(_on_damage_requested)
|
||||
EventBus.heal_requested.connect(_on_heal_requested)
|
||||
_emit_initial.call_deferred()
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
for entity in Stats.entities:
|
||||
_regen_player(delta)
|
||||
_regen_entities(delta, EnemyData.entities)
|
||||
_regen_entities(delta, BossData.entities)
|
||||
|
||||
func _regen_player(delta: float) -> void:
|
||||
if not PlayerData.alive or PlayerData.health_regen <= 0:
|
||||
return
|
||||
if PlayerData.health < PlayerData.max_health:
|
||||
var health: float = min(PlayerData.health + PlayerData.health_regen * delta, PlayerData.max_health)
|
||||
PlayerData.set_health(health)
|
||||
|
||||
func _regen_entities(delta: float, entities: Dictionary) -> void:
|
||||
for entity in entities:
|
||||
if not is_instance_valid(entity):
|
||||
continue
|
||||
var data: Dictionary = Stats.entities[entity]
|
||||
var data: Dictionary = entities[entity]
|
||||
if not data["alive"]:
|
||||
continue
|
||||
var regen: float = data["health_regen"]
|
||||
@@ -16,34 +27,6 @@ func _process(delta: float) -> void:
|
||||
data["health"] = min(data["health"] + regen * delta, data["max_health"])
|
||||
EventBus.health_changed.emit(entity, data["health"], data["max_health"])
|
||||
|
||||
func _on_damage_requested(attacker: Node, target: Node, amount: float) -> void:
|
||||
if not Stats.is_alive(target):
|
||||
return
|
||||
var remaining: float = amount
|
||||
var shield_system: Node = get_node_or_null("../ShieldSystem")
|
||||
if shield_system:
|
||||
remaining = shield_system.absorb(target, remaining)
|
||||
EventBus.damage_dealt.emit(attacker, target, amount)
|
||||
if remaining > 0:
|
||||
_take_damage(target, remaining)
|
||||
|
||||
func _take_damage(entity: Node, amount: float) -> void:
|
||||
var health: float = Stats.get_stat(entity, "health")
|
||||
health -= amount
|
||||
if health <= 0:
|
||||
health = 0
|
||||
Stats.set_stat(entity, "health", health)
|
||||
var max_health: float = Stats.get_stat(entity, "max_health")
|
||||
EventBus.health_changed.emit(entity, health, max_health)
|
||||
if health <= 0:
|
||||
Stats.set_stat(entity, "alive", false)
|
||||
EventBus.entity_died.emit(entity)
|
||||
|
||||
func _on_heal_requested(healer: Node, target: Node, amount: float) -> void:
|
||||
if not Stats.is_alive(target):
|
||||
return
|
||||
var health: float = Stats.get_stat(target, "health")
|
||||
var max_health: float = Stats.get_stat(target, "max_health")
|
||||
health = min(health + amount, max_health)
|
||||
Stats.set_stat(target, "health", health)
|
||||
EventBus.health_changed.emit(target, health, max_health)
|
||||
func _emit_initial() -> void:
|
||||
EventBus.health_changed.emit(PlayerData, PlayerData.health, PlayerData.max_health)
|
||||
EventBus.shield_changed.emit(PlayerData, PlayerData.shield, PlayerData.max_shield)
|
||||
|
||||
Reference in New Issue
Block a user