Files
MaidEngine/scripts/combat_ui.gd
2026-04-02 09:01:10 -04:00

43 lines
1.4 KiB
GDScript

class_name CombatUI extends CanvasLayer
@onready var unit_panel: PanelContainer = %UnitPanel
@onready var name_label: Label = %NameLabel
@onready var hp_bar: ProgressBar = %HPBar
var _selected_unit: Unit
func _ready() -> void:
unit_panel.visible = false
for unit: Unit in get_tree().get_nodes_in_group("units"):
unit.unit_selected_changed.connect(_on_unit_selected_changed)
unit.unit_died.connect(_on_unit_died)
get_tree().node_added.connect(_on_node_added)
func _on_node_added(node: Node) -> void:
if node is Unit and node.is_in_group("units"):
if not node.unit_selected_changed.is_connected(_on_unit_selected_changed):
node.unit_selected_changed.connect(_on_unit_selected_changed)
if not node.unit_died.is_connected(_on_unit_died):
node.unit_died.connect(_on_unit_died)
func _on_unit_died(unit: Unit) -> void:
if _selected_unit == unit:
_selected_unit = null
unit_panel.visible = false
func _process(_delta: float) -> void:
if _selected_unit and is_instance_valid(_selected_unit):
hp_bar.max_value = _selected_unit.current_stats.max_hp
hp_bar.value = _selected_unit.current_stats.current_hp
func _on_unit_selected_changed(unit: Unit, selected: bool) -> void:
if selected:
_selected_unit = unit
name_label.text = unit.current_info.name
hp_bar.max_value = unit.current_stats.max_hp
hp_bar.value = unit.current_stats.current_hp
unit_panel.visible = true
else:
_selected_unit = null
unit_panel.visible = false