refactor
This commit is contained in:
@@ -1,32 +1,69 @@
|
||||
extends Node
|
||||
|
||||
func _ready() -> void:
|
||||
_emit_initial.call_deferred()
|
||||
EventBus.damage_requested.connect(_on_damage_requested)
|
||||
EventBus.heal_requested.connect(_on_heal_requested)
|
||||
if multiplayer.multiplayer_peer != null and not multiplayer.is_server() and not (multiplayer.multiplayer_peer is OfflineMultiplayerPeer):
|
||||
set_physics_process(false)
|
||||
else:
|
||||
set_physics_process(true)
|
||||
|
||||
func _process(delta: float) -> void:
|
||||
_regen_player(delta)
|
||||
_regen_entities(delta, EnemyData.entities)
|
||||
_regen_entities(delta, BossData.entities)
|
||||
var _regen_accum: float = 0.0
|
||||
|
||||
func _regen_player(delta: float) -> void:
|
||||
if not PlayerData.alive or PlayerData.health_regen <= 0:
|
||||
func _physics_process(delta: float) -> void:
|
||||
if not multiplayer.is_server() and multiplayer.multiplayer_peer != null:
|
||||
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:
|
||||
_regen_accum += delta
|
||||
if _regen_accum < 0.20:
|
||||
return
|
||||
var dt: float = _regen_accum
|
||||
_regen_accum = 0.0
|
||||
for entity in Stats.entities():
|
||||
if not is_instance_valid(entity):
|
||||
continue
|
||||
var data: Dictionary = entities[entity]
|
||||
if not data["alive"]:
|
||||
continue
|
||||
var regen: float = data["health_regen"]
|
||||
if regen > 0 and data["health"] < data["max_health"]:
|
||||
data["health"] = min(data["health"] + regen * delta, data["max_health"])
|
||||
EventBus.health_changed.emit(entity, data["health"], data["max_health"])
|
||||
var hp: float = float(Stats.get_stat(entity, "health", 0.0))
|
||||
var max_hp: float = float(Stats.get_stat(entity, "max_health", 0.0))
|
||||
var regen: float = float(Stats.get_stat(entity, "health_regen", 0.0))
|
||||
if regen > 0.0 and hp > 0.0 and hp < max_hp:
|
||||
var new_hp: float = min(max_hp, hp + regen * dt)
|
||||
Stats.set_stat(entity, "health", new_hp)
|
||||
EventBus.health_changed.emit(entity, new_hp, max_hp)
|
||||
|
||||
func _emit_initial() -> void:
|
||||
EventBus.health_changed.emit(PlayerData, PlayerData.health, PlayerData.max_health)
|
||||
EventBus.shield_changed.emit(PlayerData, PlayerData.shield, PlayerData.max_shield)
|
||||
func _on_damage_requested(attacker: Node, target: Node, amount: float, element: int) -> void:
|
||||
if not multiplayer.is_server() and multiplayer.multiplayer_peer != null:
|
||||
return
|
||||
if not is_instance_valid(target) or not Stats.has(target):
|
||||
return
|
||||
var hp: float = float(Stats.get_stat(target, "health", 0.0))
|
||||
if hp <= 0.0:
|
||||
return
|
||||
var remaining := amount
|
||||
var shield: float = float(Stats.get_stat(target, "shield", 0.0))
|
||||
if shield > 0.0:
|
||||
var absorbed: float = min(shield, remaining)
|
||||
Stats.set_stat(target, "shield", shield - absorbed)
|
||||
Stats.set_stat(target, "shield_regen_timer", float(Stats.get_stat(target, "shield_regen_delay", 5.0)))
|
||||
remaining -= absorbed
|
||||
EventBus.shield_changed.emit(target, Stats.get_stat(target, "shield"), Stats.get_stat(target, "max_shield", 0.0))
|
||||
if shield - absorbed <= 0.0:
|
||||
EventBus.shield_broken.emit(target)
|
||||
if remaining > 0.0:
|
||||
var new_hp: float = max(0.0, hp - remaining)
|
||||
Stats.set_stat(target, "health", new_hp)
|
||||
EventBus.health_changed.emit(target, new_hp, Stats.get_stat(target, "max_health"))
|
||||
EventBus.damage_dealt.emit(attacker, target, amount)
|
||||
if new_hp <= 0.0:
|
||||
EventBus.entity_died.emit(target)
|
||||
|
||||
func _on_heal_requested(healer: Node, target: Node, amount: float) -> void:
|
||||
if not multiplayer.is_server() and multiplayer.multiplayer_peer != null:
|
||||
return
|
||||
if not is_instance_valid(target) or not Stats.has(target):
|
||||
return
|
||||
var hp: float = float(Stats.get_stat(target, "health", 0.0))
|
||||
if hp <= 0.0:
|
||||
return
|
||||
var max_hp: float = float(Stats.get_stat(target, "max_health", 0.0))
|
||||
var new_hp: float = min(max_hp, hp + amount)
|
||||
Stats.set_stat(target, "health", new_hp)
|
||||
EventBus.health_changed.emit(target, new_hp, max_hp)
|
||||
|
||||
Reference in New Issue
Block a user