30 lines
1.1 KiB
GDScript
30 lines
1.1 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
|
|
|
|
|
|
func _ready() -> void:
|
|
var player: Node = get_parent()
|
|
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)
|
|
|
|
|
|
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]
|