summaryrefslogtreecommitdiff
path: root/Stages/Wintermaul/HUD.gd
blob: 1033c8e111bb47f04119237de4f6d6c99f020011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
class_name HUD
extends CanvasLayer


var selection_groups := {}
var selected_group := "":
	set(value):
		redraw_multiselect(selected_group, value)
		selected_group = value
		redraw_select(selection_groups[selected_group][0])

@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
	%SelectionContainer.visible = false
	%MultiSelectionContainer.visible = false
	Client.placed_tower.connect(func(tower: Tower):
		if tower.owner_id == multiplayer.get_unique_id():
			tower.selected.connect(func():
				var multi_select_hud_data = TextureRect.new()
				multi_select_hud_data.name = tower.name
				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
				
				if not tower.selection_group_id in selection_groups:
					selection_groups[tower.selection_group_id] = []
				selection_groups[tower.selection_group_id].append(tower)
				multi_select_hud_data.add_to_group(tower.selection_group_id)
				
				if Tower.selected_towers.size() == 1:
					%SelectionContainer.visible = true
					%MultiSelectionContainer.visible = false
					
					%MultiSelectionList.add_child(multi_select_hud_data)
					selected_group = tower.selection_group_id
				elif Tower.selected_towers.size() > 1:
					%MultiSelectionContainer.visible = true
					
					%MultiSelectionList.add_child(multi_select_hud_data)
					
					redraw_multiselect("", selected_group)
				
				redraw_select(tower)
			)
			
			tower.deselected.connect(func():
				if Tower.selected_towers.size() == 0:
					%SelectionContainer.visible = false
					%MultiSelectionContainer.visible = false
					
					for child in %MultiSelectionList.get_children():
						child.queue_free()
				elif Tower.selected_towers.size() == 1:
					%MultiSelectionContainer.visible = false
					%SelectionContainer.visible = true
					
					var node = %MultiSelectionList.get_node_or_null(NodePath(tower.name))
					if node:
						node.queue_free()
				elif Tower.selected_towers.size() > 1:
					# only remove deselected tower from list
					var node = %MultiSelectionList.get_node_or_null(NodePath(tower.name))
					if node: # when double click selecting in quick succession not found error
						node.queue_free()
				
				if tower.selection_group_id in selection_groups:
					selection_groups[tower.selection_group_id].erase(tower)
					if selection_groups[tower.selection_group_id].is_empty():
						selection_groups.erase(tower.selection_group_id)
			)
			
			tower.selection_group_id_changed.connect(func(previous_id: String):
				#Client.selection.change_selection_group_id([tower], previous_id, tower.selection_group_id)
				
				if not tower.selection_group_id in selection_groups:
					selection_groups[tower.selection_group_id] = []
				selection_groups[tower.selection_group_id].append(tower)
				%MultiSelectionList.get_node(NodePath(tower.name)).add_to_group(tower.selection_group_id)
				
				selection_groups[previous_id].erase(tower)
				if selection_groups[previous_id].is_empty():
					selection_groups.erase(previous_id)
					if previous_id == selected_group:
						selected_group = tower.selection_group_id
				%MultiSelectionList.get_node(NodePath(tower.name)).remove_from_group(previous_id)
				
				if tower.name == selection_groups[tower.selection_group_id][0].name:
					redraw_select(tower)
			)
	)
	
	Client.multi_select_finished.connect(func():
		var keys = get_ordered_group_keys()
		if keys.size() > 0:
			selected_group = keys[0]
	)


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:
		var keys = get_ordered_group_keys()
		var current_idx = keys.find(selected_group)
		selected_group = keys[(current_idx + 1) % keys.size()]


func redraw_select(tower: Tower):
	%TowerData.current_tower = tower


func redraw_multiselect(previous_group: String, new_group: String):
	var idx = 0
	for group_key in get_ordered_group_keys():
		var group = selection_groups[group_key]
		for node in group:
			var selection_node = %MultiSelectionList.get_node_or_null(NodePath(node.name))
			if selection_node: # when double click selecting in quick succession not found error
				%MultiSelectionList.move_child(selection_node, idx)
				idx += 1
	
	for node in get_tree().get_nodes_in_group(previous_group):
		node.modulate = Color(0.5, 0.5, 0.5)
		var tower = Client.current_stage.get_node_or_null("%Towers/" + node.name)
		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 = Client.current_stage.get_node_or_null("%Towers/" + node.name)
		if tower:
			tower.is_highlighted = true


func get_ordered_group_keys() -> Array:
	var keys = selection_groups.keys()
	keys.sort_custom(func(a, b):
		var group_a = selection_groups[a]
		var group_b = selection_groups[b]
		
		return group_a.size() > group_b.size()
	)
	keys.sort_custom(func(a, b):
		var group_a = selection_groups[a]
		var group_b = selection_groups[b]
		var node_a = group_a[0]
		var node_b = group_b[0]
		var level_a = 0
		var level_b = 0
		
		for component in node_a.components.values():
			level_a += component.level
		for component in node_b.components.values():
			level_b += component.level
		
		return level_a > level_b
	)
	keys.sort_custom(func(a, b):
		var group_a = selection_groups[a]
		var group_b = selection_groups[b]
		
		return group_a[0].components.size() > group_b[0].components.size()
	)
	
	return keys


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