61 lines
2.2 KiB
GDScript
61 lines
2.2 KiB
GDScript
extends CanvasLayer
|
|
|
|
|
|
## Interfaz de vida en CanvasLayer: sus coordenadas pertenecen a la ventana,
|
|
## no al escenario. Se mantiene en la misma esquina aunque avance la camara.
|
|
|
|
@onready var health_bar: ProgressBar = $HUD/HealthBar
|
|
@onready var health_label: Label = $HUD/HealthBar/HealthLabel
|
|
@onready var effects_label: Label = $HUD/EffectsLabel
|
|
|
|
var player_ref: Node
|
|
|
|
|
|
## Valida el contrato del jugador y conecta la senal de vida.
|
|
func _ready() -> void:
|
|
player_ref = get_parent()
|
|
if not player_ref.has_signal(&"health_changed"):
|
|
push_error("PlayerHealthHUD must be a child of a Player.")
|
|
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
|
|
|
|
|
|
## Actualiza los tiempos restantes de las mejoras temporales.
|
|
func _process(_delta: float) -> void:
|
|
if player_ref == null:
|
|
return
|
|
|
|
# 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:
|
|
health_bar.max_value = float(max_health)
|
|
health_bar.value = float(current_health)
|
|
health_label.text = "HP %d / %d" % [current_health, max_health]
|