class_name HUD extends CanvasLayer @onready var time: Label = %Time @onready var money: Label = %Money @onready var income: Label = %Income @onready var spawn_box: Control = %SpawnBox @onready var team_top: PanelContainer = %TeamTop @onready var team_bottom: PanelContainer = %TeamBottom @onready var lives_top: Label = %LivesTop @onready var lives_bottom: Label = %LivesBottom func _ready(): Client.player.money_changed.connect(func(): money.text = "Money: " + str(Client.player.money) ) Client.player.income_changed.connect(func(): income.text = "Income: " + str(Client.player.income) ) Client.current_stage.lives_changed.connect(func(): lives_top.text = "Lives: %02d" % Client.current_stage.teams.top.lives lives_bottom.text = "Lives: %02d" % Client.current_stage.teams.bottom.lives ) Client.stage_state_changed.connect(func(state: State): if state is StateBuild: %TowerConfigurationsContainer.visible = true else: %TowerConfigurationsContainer.visible = false ) # multi select setup %SelectionContainer.visible = false %MultiSelectionContainer.visible = false # add tower to selection Client.selection.added_to_group.connect(func(nodes: Array[Node], id: String): for tower: Tower in nodes: var multi_select_hud_data = TextureRect.new() multi_select_hud_data.modulate = Color(0.5, 0.5, 0.5) multi_select_hud_data.expand_mode = TextureRect.EXPAND_IGNORE_SIZE multi_select_hud_data.stretch_mode = TextureRect.STRETCH_KEEP_ASPECT_CENTERED multi_select_hud_data.custom_minimum_size = Vector2(32, 64) var texture = AtlasTexture.new() texture.atlas = preload("res://Towers/Assets/spritesheet.png") texture.region = Rect2(5, 1, 62, 95) multi_select_hud_data.texture = texture multi_select_hud_data.set_meta("tower", tower) tower.set_meta("multi_select_hud", multi_select_hud_data) multi_select_hud_data.add_to_group(id) %MultiSelectionList.add_child(multi_select_hud_data) if Client.selection.node_count == 1: %SelectionContainer.visible = true %MultiSelectionContainer.visible = false elif Client.selection.node_count > 1: %MultiSelectionContainer.visible = true redraw_multiselect() redraw_select(tower) ) # remove tower from selection Client.selection.removed_from_group.connect(func(nodes: Array[Node], _id: String): for tower: Tower in nodes: if Client.selection.node_count == 0: %SelectionContainer.visible = false %MultiSelectionContainer.visible = false elif Client.selection.node_count == 1: %MultiSelectionContainer.visible = false %SelectionContainer.visible = true # catch free error for selecting in quick succcession by double click if is_instance_valid(tower.get_meta("multi_select_hud")): tower.get_meta("multi_select_hud").queue_free() tower.remove_meta("multi_select_hud") ) # move tower to new selection group Client.selection.moved_to_group.connect(func(nodes: Array[Node], previous_id: String, new_id: String): for tower: Tower in nodes: tower.get_meta("multi_select_hud").remove_from_group(previous_id) tower.get_meta("multi_select_hud").add_to_group(new_id) if Client.selection.has_node_in_group(tower, new_id): redraw_select(tower) redraw_multiselect(previous_id, new_id) ) # redraw on selected group change Client.selection.selected_group_changed.connect(func(previous_id: String, new_id: String): if Client.selection.get_selected_nodes(): redraw_select(Client.selection.get_selected_nodes()[0]) redraw_multiselect(previous_id, new_id) ) func _input(event: InputEvent): if event.is_action_pressed("spawn_box_toggle"): spawn_box.visible = not spawn_box.visible if event.is_action_pressed("players_list_toggle"): team_top.visible = not team_top.visible team_bottom.visible = not team_bottom.visible if event.is_action_pressed("selection_cycle") and %MultiSelectionContainer.visible: if Input.is_action_pressed("reverse_cycle_direction"): Client.selection.go_to_previous() else: Client.selection.go_to_next() func redraw_select(tower: Tower): %TowerData.current_tower = tower func redraw_multiselect(previous_group: String = "", new_group: String = Client.selection.selected_group): var idx = 0 for id in Client.selection.get_ordered_group_ids(): var group = Client.selection.selection_groups[id] for node: Tower in group: # when double click selecting in quick succession not found error if (node.has_meta("multi_select_hud") and is_instance_valid(node.get_meta("multi_select_hud")) ): %MultiSelectionList.move_child(node.get_meta("multi_select_hud"), idx) idx += 1 # wait for nodes to be in the correct groups await get_tree().process_frame for node in get_tree().get_nodes_in_group(previous_group): node.modulate = Color(0.5, 0.5, 0.5) var tower = node.get_meta("tower") if tower: tower.is_highlighted = false for node in get_tree().get_nodes_in_group(new_group): node.modulate = Color(1.0, 1.0, 1.0) var tower = node.get_meta("tower") if tower: tower.is_highlighted = true func _on_build_mode_button_gui_input(event: InputEvent) -> void: if event.is_action_pressed("select"): if Client.state is StateDefault: get_tree().current_scene.get_node("StateManager").set_state("StateBuild") elif Client.state is StateBuild: get_tree().current_scene.get_node("StateManager").set_state("StateDefault") func _on_spawner_box_button_gui_input(event: InputEvent) -> void: if event.is_action_pressed("select"): spawn_box.visible = not spawn_box.visible func _on_player_list_button_gui_input(event: InputEvent) -> void: if event.is_action_pressed("select"): team_top.visible = not team_top.visible team_bottom.visible = not team_bottom.visible