summaryrefslogtreecommitdiff
path: root/Towers/Tower.gd
blob: 2de8216eaa22bf986ae142e74963fbcafed4cf31 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
class_name Tower
extends StaticBody2D


signal selected
signal deselected

static var selected_towers: Array[Tower]
static var hovered_tower: Tower

var is_selected = false :
	set(value):
		if value:
			if not Tower.selected_towers.has(self):
				Tower.selected_towers.append(self)
				selected.emit()
		else:
			Tower.selected_towers.erase(self)
			deselected.emit()
		is_selected = value
		queue_redraw()

var is_hovered = false :
	set(value):
		if value:
			hovered_tower = self
			Input.set_default_cursor_shape(Input.CURSOR_POINTING_HAND)
		else:
			if hovered_tower == self:
				hovered_tower = null
				Input.set_default_cursor_shape(Input.CURSOR_ARROW)
		is_hovered = value
		queue_redraw()

var mobs_in_range: Array = []

#var selection_area: Area2D

signal selection_group_id_changed(previous_id: String)
@onready var selection_group_id := get_group_id():
	set(value):
		var previous_id = selection_group_id
		selection_group_id = value
		selection_group_id_changed.emit(previous_id)

# rpc owner id
@export var owner_id = 1

signal attack_value_changed(value: int)
@export var attack_range: int = 32
@export var attack_power: int = 1

@export var attack_speed: int = 1
var speed_base: float = 2.0

signal components_changed
@export var components: Array[TowerComponent] = []#:
	#set(value):
		#components = value
		#components_changed.emit()


func _ready():
	$Range/CollisionShape2D.shape.radius = attack_range
	$ShootCooldown.wait_time = speed_base / attack_speed
	
	%Data.text = "Range: %s - Power: %s - Speed: %s" % [
		attack_range, attack_power, attack_speed
	]
	
	%HUD.visible = false
	
	components_changed.connect(func():
		selection_group_id = get_group_id()
	)
	
	#for component in [
		#preload("res://Towers/Components/FrostComponent.gd").new(),
		#preload("res://Towers/Components/BurnComponent.gd").new(),
	#]:
		#add_component(component)


func _draw():
	modulate = Color(1, 1, 1)
	
	if is_hovered:
		if Client.state is StateDefault:
			draw_circle(
				Client.current_stage.map.tile_set.tile_size,
				8 + attack_range,
				Color(1, 1, 1, 0.5),
				false,
				1.0
			)
			modulate = Color(1.25, 1.25, 1.25)
	
	if is_selected:
		modulate = Color(1.5, 1.5, 1.5)


func _process(_delta: float) -> void:
	if $ShootCooldown.is_stopped() and not mobs_in_range.is_empty():
		shoot()
		$ShootCooldown.start()
	
	#if selection_area and is_instance_valid(selection_area):
		#var bodies = selection_area.get_overlapping_bodies()
		#if bodies.size() > 0:
			#selection_area.queue_free()
			#for body in bodies:
				#Client.select_tower(body)


func _on_input_event(_viewport: Node, event: InputEvent, _shape_idx: int):
	# disable remote select for now
	if owner_id != multiplayer.get_unique_id():
		return
	
	if Client.state is StateBuild:
		if event.is_action_pressed("builder_cancel"):
			Client.remove_tower(self)


func _on_range_body_entered(body: Node2D) -> void:
	mobs_in_range.append(body)

func _on_range_body_exited(body: Node2D) -> void:
	mobs_in_range.erase(body)


func _on_selectable_area_hover_enter() -> void:
	is_hovered = true

func _on_selectable_area_hover_exit() -> void:
	is_hovered = false

func _on_selectable_area_select(event: InputEvent) -> void:
	# disable remote select for now
	if owner_id != multiplayer.get_unique_id():
		return
	
	if Client.state is StateDefault:
		if Input.is_action_pressed("select_multiple") and is_selected:
			is_selected = false
		else:
			Client.select_tower(self)
			
			if event.is_double_click():
				Client.multi_select(3)




func add_component(component: TowerComponent):
	components.append(component)
	var sprite = component.sprite
	$ComponentsAnchor.add_child(sprite)
	redraw_components()
	components_changed.emit()

func remove_component(component_name: String):
	for component in components:
		if component.name == component_name:
			components.erase(component)
			$ComponentsAnchor.remove_child($ComponentsAnchor.get_node(NodePath(component.name)))
			break
	redraw_components()
	components_changed.emit()

func redraw_components():
	for idx in range(components.size()):
		var component = components[idx]
		var sprite = $ComponentsAnchor.get_node(NodePath(component.name))
		sprite.position.y = (idx + 1) * -16


func is_melee_range():
	return attack_range <= (Client.current_stage.map.tile_set.tile_size.x * 2)


func shoot():
	if not multiplayer.is_server():
		# TODO: do shoot animation, but don't subtract hp
		return
	
	var target = mobs_in_range[0] as Unit
	
	for component in components:
		if component.has_method("on_shoot"):
			component.on_shoot(target)
	
	if is_melee_range():
		target.set_hp(target.hp - 1)
	else: # TODO
		target.set_hp(target.hp - 1)


func get_hud():
	var data = %HUD.get_child(0).duplicate()
	data.name = name
	
	return data


func get_region():
	var collision_shape := $CollisionShape2D
	var shape = $CollisionShape2D.shape as RectangleShape2D
	
	return Rect2(
		collision_shape.position,
		shape.size
	)


func get_group_id() -> String:
	var string := ""
	
	string += str(attack_range)
	string += str(attack_power)
	string += str(attack_speed)
	
	for component in components:
		string += component.name
	
	return str(components.size()) + string.md5_text()


func _on_tree_exiting() -> void:
	is_selected = false


func _on_h_box_container_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		get_viewport().set_input_as_handled()
		
		var hud: HUD = Client.current_stage.get_node("HUD")
		var towers = hud.selection_groups[hud.selected_group].duplicate()
		for tower in towers:
			var found = false
			for idx in range(tower.components.size()):
				var component = tower.components[idx]
				if component.name == "Frost":
					found = true
					tower.remove_component(component.name)
					break
			
			if not found:
				tower.add_component(preload("res://Towers/Components/FrostComponent.gd").new())
			
			Client.update_tower(tower.get_path(), tower.to_network_data())


func _on_h_box_container_2_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		get_viewport().set_input_as_handled()
		
		var hud: HUD = Client.current_stage.get_node("HUD")
		var towers = hud.selection_groups[hud.selected_group].duplicate()
		for tower in towers:
			var found = false
			for idx in range(tower.components.size()):
				var component = tower.components[idx]
				if component.name == "Burn":
					found = true
					tower.remove_component(component.name)
					break
			
			if not found:
				tower.add_component(preload("res://Towers/Components/BurnComponent.gd").new())
			
			Client.update_tower(tower.get_path(), tower.to_network_data())


func _on_button_1_pressed() -> void:
	if Client.player.money < (attack_range / 8.0) + 10:
		return
	
	var hud: HUD = Client.current_stage.get_node("HUD")
	var towers = hud.selection_groups[hud.selected_group].duplicate()
	for tower: Tower in towers:
		tower.attack_range += 8
		tower.attack_value_changed.emit((attack_range / 8.0) - 1)
		tower.get_node("%Data").text = "Range: %s - Power: %s - Speed: %s" % [
			tower.attack_range, tower.attack_power, tower.attack_speed
		]
		tower.get_node("%Range/CollisionShape2D").shape.radius = attack_range
		tower.selection_group_id = tower.get_group_id()
		Client.update_tower(tower.get_path(), tower.to_network_data())


func _on_button_2_pressed() -> void:
	if Client.player.money < attack_power * 10:
		return
	
	var hud: HUD = Client.current_stage.get_node("HUD")
	var towers = hud.selection_groups[hud.selected_group].duplicate()
	for tower: Tower in towers:
		tower.attack_power += 1
		tower.attack_value_changed.emit(attack_power - 1)
		tower.get_node("%Data").text = "Range: %s - Power: %s - Speed: %s" % [
			tower.attack_range, tower.attack_power, tower.attack_speed
		]
		tower.selection_group_id = tower.get_group_id()
		Client.update_tower(tower.get_path(), tower.to_network_data())


func _on_button_3_pressed() -> void:
	if Client.player.money < attack_speed * 10:
		return
	
	var hud: HUD = Client.current_stage.get_node("HUD")
	var towers = hud.selection_groups[hud.selected_group].duplicate()
	for tower: Tower in towers:
		tower.attack_speed += 1
		tower.attack_value_changed.emit(attack_speed - 1)
		tower.get_node("%Data").text = "Range: %s - Power: %s - Speed: %s" % [
			tower.attack_range, tower.attack_power, tower.attack_speed
		]
		tower.get_node("%ShootCooldown").wait_time = (speed_base / attack_speed)
		tower.selection_group_id = tower.get_group_id()
		Client.update_tower(tower.get_path(), tower.to_network_data())



class NetworkData extends Resource:
	var name: String
	var owner_id: int
	var position: Vector2
	
	var attack_range: int
	var attack_power: int
	var attack_speed: int
	var components: Array
	
	var sprite_modulate: Color
	var components_anchor_modulate: Color

func to_network_data() -> NetworkData:
	var data = NetworkData.new()
	
	data.name = name
	data.owner_id = owner_id
	data.position = global_position
	
	data.attack_range = attack_range
	data.attack_power = attack_power
	data.attack_speed = attack_speed
	data.components = components.map(func(item: TowerComponent):
		return item.name
	)
	
	data.sprite_modulate = $Sprite2D.modulate
	data.components_anchor_modulate = $ComponentsAnchor.modulate
	
	# IMPROVEMENT: check against last update and only set changed values
	
	return data

func update_with_network_data(data: NetworkData):
	for component in components.duplicate():
		remove_component(component.name)
	
	for component_name in data.components:
		add_component(
			load("res://Towers/Components/" + component_name + "Component.gd").new()
		)

static func from_network_data(data: NetworkData) -> Tower:
	var tower: Tower = preload("res://Towers/Tower.tscn").instantiate()
	
	tower.name = data.name
	tower.owner_id = data.owner_id
	tower.global_position = data.position
	
	tower.attack_range = data.attack_range
	tower.attack_power = data.attack_power
	tower.attack_speed = data.attack_speed
	
	return tower