summaryrefslogtreecommitdiff
path: root/Towers/Tower.gd
blob: 54687fd51668347e50acd1f254ec030ecfc537eb (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
class_name Tower
extends StaticBody2D


signal selected
signal deselected

static var selected_towers: Array[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()
			Client.stage.hud.tower.text = ""
		is_selected = value
		queue_redraw()

var is_hovered = false :
	set(value):
		is_hovered = value
		queue_redraw()

var mobs_in_range: Array = []

#var selection_area: Area2D

# rpc owner id
var owner_id = 1

@export var attack_range: int = 32
@export var attack_power: int = 1
@export var attack_speed: int = 1


func _ready():
	$Range/CollisionShape2D.shape.radius = attack_range
	$ShootCooldown.wait_time = attack_speed


func _draw():
	if is_selected:
		draw_circle(
			Client.stage.map.tile_set.tile_size,
			8 + attack_range,
			Color(1, 1, 1, 0.75),
			false,
			1.0
		)
		modulate = Color(1.5, 1.5, 1.5)
	elif is_hovered:
		if Client.state is StateDefault:
			draw_circle(
				Client.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)
	else:
		modulate = Color(1, 1, 1)


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:
		Client.select_tower(self)
		
		if event.is_double_click():
			var selection_area = preload("res://Game/Selection/MultiSelectArea.tscn").instantiate()
			selection_area.set_collision_mask_value(3, true)
			selection_area.select.connect(func(nodes):
				for node in nodes:
					Client.select_tower(node)
			)
			get_tree().current_scene.add_child(selection_area)




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


func shoot():
	if get_multiplayer_authority() != multiplayer.get_unique_id():
		# TODO: do shoot animation, but don't subtract hp
		return
	
	var target = mobs_in_range[0] as Unit
	
	if is_melee_range():
		target.set_hp(target.hp - 1)
	else: # TODO
		target.set_hp(target.hp - 1)


func show_ui():
	# todo: show ui
	pass


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


func _on_tree_exiting() -> void:
	is_selected = false


func get_rpc_properties() -> Dictionary:
	return {
		"name": null,
		"global_position": null,
		"owner_id": null,
	}