Files
mortal-maze-the-game/ui/hud/player_health_hud.gd
T

63 lines
2.2 KiB
GDScript

extends CanvasLayer
## Minimal player HUD.
##
## This is a CanvasLayer on purpose: its coordinates belong to the camera
## viewport, not to the level. Therefore (18, 18) always means "18 pixels from
## the top-left corner currently visible by the camera", even while the player
## and Camera2D move through the world.
@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
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
# Children enter _ready before Player. Its initial signal arrives afterward.
player_ref.connect(&"health_changed", _on_health_changed)
effects_label.visible = false
func _process(_delta: float) -> void:
if player_ref == null:
return
# Display each active effect individually: essential when validating all
# eight pickup families on a phone. Property names match Player's timers.
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)
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]