58 lines
1.9 KiB
GDScript
58 lines
1.9 KiB
GDScript
extends Control
|
|
|
|
@export var health_bar: TextureProgressBar
|
|
@export var stamina_bar: TextureProgressBar
|
|
@export var gold_label: Label
|
|
|
|
@onready var player: Player = get_tree().get_first_node_in_group("player")
|
|
@onready var crosshair: BoxContainer = $CrossHair
|
|
const GOLD_LABEL_PREFIX := "Gold: "
|
|
var unit: Unit
|
|
|
|
|
|
func _ready() -> void:
|
|
# Connect signals from the player to update the UI elements
|
|
player.health_changed.connect(update_health_bar)
|
|
player.stamina_changed.connect(update_stamina_bar)
|
|
player.gold_changed.connect(update_gold_text)
|
|
player.player_loaded.connect(_on_player_loaded)
|
|
# Connect the state_changed signal to update the crosshair visibility
|
|
unit = player as Unit
|
|
if unit:
|
|
unit.state_changed.connect(_on_state_changed)
|
|
|
|
|
|
func update_health_bar(current_health: int, maximum_health: int) -> void:
|
|
# Update the health bar with the current health
|
|
_set_progress(health_bar, current_health, maximum_health)
|
|
|
|
|
|
func update_stamina_bar(current_stamina: int, maximum_stamina: int) -> void:
|
|
# Update the stamina bar with the current stamina
|
|
_set_progress(stamina_bar, current_stamina, maximum_stamina)
|
|
|
|
|
|
func update_gold_text(gold: int) -> void:
|
|
# Update the gold label with the current gold amount
|
|
gold_label.text = "%s%d" % [GOLD_LABEL_PREFIX, gold]
|
|
|
|
|
|
func _on_player_loaded() -> void:
|
|
# Initialize the health, stamina, and gold bars when the player is loaded
|
|
update_health_bar(player.health, player.maximum_health)
|
|
update_stamina_bar(player.stamina, player.maximum_stamina)
|
|
update_gold_text(player.gold)
|
|
|
|
|
|
func _on_state_changed(_old_state: Unit.States, new_state: Unit.States) -> void:
|
|
# Update the crosshair visibility based on the new state
|
|
crosshair.visible = (new_state == Unit.States.aiming)
|
|
|
|
|
|
func _set_progress(bar: TextureProgressBar, current: int, maximum: int) -> void:
|
|
# Safeguard against division by zero and map to 0..100
|
|
if maximum <= 0:
|
|
bar.value = 0.0
|
|
return
|
|
bar.value = (100.0 * float(current)) / float(maximum)
|