85 lines
3.4 KiB
GDScript
85 lines
3.4 KiB
GDScript
extends CanvasLayer
|
|
|
|
|
|
## Conecta el estado de Player con el HUD. La estetica y la animacion viven en ThrashMeter.
|
|
## CanvasLayer mantiene la interfaz fija en pantalla aunque avance la camara.
|
|
|
|
## Umbral de aviso visual; no cambia la vida ni la dificultad del combate.
|
|
@export_range(0.05, 0.5, 0.05) var low_health_ratio: float = 0.25
|
|
|
|
@onready var health_bar: ThrashMeter = $HUD/HealthBar
|
|
@onready var health_label: Label = $HUD/HealthBar/HealthLabel
|
|
@onready var effects_label: Label = $HUD/EffectsLabel
|
|
@onready var warning_label: Label = $HUD/WarningLabel
|
|
|
|
var player_ref: Node
|
|
var _received_health: bool = false
|
|
var _effects_refresh_timer: float = 0.0
|
|
|
|
|
|
## Valida el contrato del jugador y conecta la senal de vida.
|
|
func _ready() -> void:
|
|
player_ref = get_parent()
|
|
if player_ref == null or not player_ref.has_signal(&"health_changed"):
|
|
# Abrir esta escena sola permite revisar su diseno sin crear un jugador de mentira.
|
|
player_ref = null
|
|
set_process(false)
|
|
return
|
|
# Los hijos ejecutan _ready antes que Player; su senal inicial llegara despues de esta conexion.
|
|
player_ref.connect(&"health_changed", _on_health_changed)
|
|
effects_label.visible = false
|
|
# Tambien funciona si el HUD se instancia despues de _ready del jugador.
|
|
if player_ref.is_node_ready():
|
|
var data: Resource = player_ref.get("character_data") as Resource
|
|
if data != null:
|
|
_on_health_changed(int(player_ref.get("current_health")), int(data.get("max_health")))
|
|
|
|
|
|
## Actualiza los tiempos restantes de las mejoras temporales.
|
|
func _process(delta: float) -> void:
|
|
if not is_instance_valid(player_ref):
|
|
return
|
|
# Los contadores muestran segundos: refrescarlos a 10 Hz evita reconstruir texto cada frame.
|
|
_effects_refresh_timer -= delta
|
|
if _effects_refresh_timer > 0.0:
|
|
return
|
|
_effects_refresh_timer = 0.1
|
|
|
|
# Muestra cada mejora por separado. Los nombres de propiedades corresponden al estado temporal de Player.
|
|
var active: Array[String] = []
|
|
var speed_seconds: float = float(player_ref.get("_speed_boost_timer"))
|
|
if speed_seconds > 0.0:
|
|
var bonus_percent: int = roundi(float(player_ref.get("_speed_boost_multiplier")) * 100.0)
|
|
var speed: int = roundi(float(player_ref.call("get_move_speed")))
|
|
active.append("VEL +%d%% | %d px/s | %ds" % [bonus_percent, speed, ceili(speed_seconds)])
|
|
|
|
for effect in [
|
|
["DANO", "_damage_boost_timer"],
|
|
["GOLPE AEREO", "_air_damage_boost_timer"],
|
|
["DEFENSA", "_damage_reduction_timer"],
|
|
["SALTO", "_jump_boost_timer"],
|
|
]:
|
|
var seconds: float = float(player_ref.get(effect[1]))
|
|
if seconds > 0.0:
|
|
active.append("%s: %ds" % [effect[0], ceili(seconds)])
|
|
|
|
var shield_hits: int = int(player_ref.get("_shield_hits"))
|
|
if shield_hits > 0:
|
|
active.append("ESCUDO: %d golpe(s)" % shield_hits)
|
|
|
|
effects_label.visible = not active.is_empty()
|
|
effects_label.text = "\n".join(active)
|
|
|
|
|
|
## Sincroniza barra y texto a partir del mismo par de valores.
|
|
func _on_health_changed(current_health: int, max_health: int) -> void:
|
|
var capacity: int = maxi(max_health, 0)
|
|
var amount: int = clampi(current_health, 0, capacity)
|
|
health_bar.set_amount(amount, capacity, not _received_health)
|
|
_received_health = true
|
|
health_label.text = "%d / %d" % [amount, capacity]
|
|
var low_health: bool = capacity > 0 and float(amount) / float(capacity) <= low_health_ratio
|
|
health_bar.highlighted = low_health and amount > 0
|
|
warning_label.visible = low_health or amount == 0
|
|
warning_label.text = "SIN VIDA" if amount == 0 else "PELIGRO"
|