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

49 lines
1.6 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 speed_label: Label = $HUD/SpeedLabel
var player_ref: Node
func _ready() -> void:
var player: Node = get_parent()
player_ref = player
if player == null or not player.has_signal(&"health_changed"):
push_error("PlayerHealthHUD must be a child of a Player with health_changed signal.")
return
# Child _ready() runs before Player._ready(). We connect here first and the
# Player will emit its initial health value a moment later during its _ready().
player.connect(&"health_changed", _on_health_changed)
if speed_label:
speed_label.visible = false
func _process(_delta: float) -> void:
if player_ref == null or speed_label == null:
return
if "get" in player_ref and player_ref.has_method("get"):
pass
var t = player_ref.get("_speed_boost_timer")
if t != null and float(t) > 0.0:
speed_label.visible = true
speed_label.text = "SPEED UP: %ds" % int(float(t))
else:
speed_label.visible = false
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]