SchildDerStaerke/scripts/ui.gd

45 lines
1.7 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 cross_hair: BoxContainer = $CrossHair
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
unit.connect("state_changed", Callable(self, "_on_state_changed"))
func update_health_bar(current_health: int, maximum_health: int) -> void:
# Update the health bar with the current health
health_bar.value = (100.0 / maximum_health) * current_health
func update_stamina_bar(current_stamina: int, maximum_stamina: int) -> void:
# Update the stamina bar with the current stamina
stamina_bar.value = (100.0 / maximum_stamina) * current_stamina
func update_gold_text(gold: int) -> void:
# Update the gold label with the current gold amount
gold_label.text = "Gold: " + str(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):
# Update the crosshair visibility based on the new state
if new_state == Unit.States.aiming:
cross_hair.visible = true
else:
cross_hair.visible = false