aboutsummaryrefslogtreecommitdiff
path: root/scripts/control.gd
blob: ff2d8daaa8613e30123abe606dd8bd22980e776f (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
extends Node

var ineditor = false
var state = Globals.PlayerState.PICK
var statetype = Globals.StateType.TURRET
var selected = ""
var editing_turret = ""

var gui : Node
var wave : Node
var player : Node
var placer : Node
var resources : Node
var pointer : Node
var turret_holder : Node
var enemies_holder : Node
var load_turrets : Node
var saveload_map : Node
var path : Node
var world : VoxelMesh

func fetch ():
	if load_turrets != null: return
	var root = get_tree().root.get_node("world")
	player = root.get_node("player")
	resources = player.get_node("resources")
	placer = player.get_node("placer")
	
	wave = root.get_node("wave")
	pointer = root.get_node("pointer")
	gui = root.get_node("gui")
	world = root.get_node("world")
	path = root.get_node("path")
	turret_holder = root.get_node("turrets")
	enemies_holder = root.get_node("enemies")
	
	var saveload = root.get_node("saveload")
	load_turrets = saveload.get_node("load_turrets")
	saveload_map = saveload.get_node("saveload_map")
	if !load_turrets.loaded: yield(load_turrets, "done_loading")
	
func _ready ():
	fetch()
	_refresh()
	build_option (state, statetype)
	
func build_option (st, sttype):
	var opts = []
	match st:
		Globals.PlayerState.PICK, Globals.PlayerState.PLACE:
			match sttype:
				Globals.StateType.TURRET:
					for t in load_turrets.get_base_turrets():
						opts += [ { "type": "turret buy", "name": t.name } ]
				Globals.StateType.ATTACH:
					opts += [ { "type": "text", "name": "attach" } ]
				Globals.StateType.PATH:
					opts += [ { "type": "text", "name": "start path" } ]
					opts += [ { "type": "text", "name": "path" } ]
					opts += [ { "type": "text", "name": "end path" } ]
				Globals.StateType.VOXEL:
					for i in world.voxel_set.size():
						var details = world.voxel_set.get_voxel(i)
						var color = Color(1, 0, 1)
						if details.has("color"): color = details.color
						opts += [ { "type": "color", "name": str(i), "color": color} ]
						
		Globals.PlayerState.EDIT:
			match sttype:
				Globals.StateType.TURRET:
					var tinfo = turret_holder.get_node(editing_turret).info
					for t in load_turrets.get_upg_turrets(tinfo.name):
						opts += [ { "type": "turret upg", "name": t.name } ]
					if tinfo.has("projectile"):
						opts += [ { "type": "text", "name": "targeting" } ]
					if tinfo.get("modules_max", 0) > 0:
						opts += [ { "type": "text", "name": "modules" } ]
					opts += [ { "type": "text", "name": "sell" } ]
					opts += [ { "type": "text", "name": "back" } ]
					
				Globals.StateType.TARGETING:
					opts += [ { "type": "text", "name": "back" } ]
					
				Globals.StateType.MODULES:
					opts += [ { "type": "text", "name": "add" } ]
					opts += [ { "type": "text", "name": "back" } ]
					
				Globals.StateType.MODULES_PICK:
					for k in load_turrets.modules:
						var mod = load_turrets.modules[k]
						opts += [ { "type": "text", "name": mod.name } ]
					opts += [ { "type": "text", "name": "back" } ]
					
	gui.bottom_bar.picker.build(opts)
	
func buy (pos, rot, turr_name):
	var info = load_turrets.info[turr_name]
	if ineditor or resources.greater_than(info.cost):
		if !ineditor: resources.sub(info.cost)
		var obj = placer.inst_turret(pos, rot, turr_name)
		editing_turret = obj.name
		state = Globals.PlayerState.EDIT
		build_option(state, statetype)
	else:
		pass
		## TODO feedback
		
func upgrade (turr_inst_name, upg_name):
	var info = load_turrets.info[upg_name]
	if ineditor or resources.greater_than(info.cost):
		if !ineditor: resources.sub(info.cost)
		var prv = turret_holder.get_node(turr_inst_name)
		var pos = prv.transform.origin
		var rot = prv.transform.basis.get_rotation_quat()
		placer.delete(statetype, pos, rot)
		var obj = placer.inst_turret(pos, rot, upg_name)
		editing_turret = obj.name
		state = Globals.PlayerState.EDIT
		build_option(state, statetype)
	else:
		pass
		## TODO feedback
	
func sell (turr_name):
	var turr = turret_holder.get_node(turr_name)
	var info = turr.info
	
	resources.add(info.cost)
	
	placer.delete(Globals.StateType.TURRET, 
		turr.transform.origin, turr.transform.basis.get_rotation_quat())

func _refresh ():
	gui.refresh()
	pointer.refresh(state, statetype, selected)
	
func to_pick ():
	selected = ""
	editing_turret = ""
	state = Globals.PlayerState.PICK
	build_option(state, statetype)

func do (action, par = {}):
	print(str(action) + " " + str(par))
	fetch()
	match state:
		Globals.PlayerState.PICK:
			match action:
				Globals.PlayerActions.PICK:
					selected = par.name
					state = Globals.PlayerState.PLACE
					
				Globals.PlayerActions.SELECT:
					match statetype:
						Globals.StateType.TURRET:
							selected = ""
							editing_turret = par.selected
							state = Globals.PlayerState.EDIT
							build_option(state, statetype)
						_: to_pick()
					
				Globals.PlayerActions.CHANGE_TYPE:
					selected = ""
					editing_turret = ""
					state = Globals.PlayerState.PICK
					statetype = par.statetype
					build_option(state, statetype)
					
		Globals.PlayerState.PLACE:
			match action:
				Globals.PlayerActions.PLACE:
					match statetype:
						Globals.StateType.TURRET:
							buy(par.pos, par.rot, selected)
						Globals.StateType.ATTACH:
							placer.inst_attach(par.pos, par.rot)
						Globals.StateType.PATH:
							match selected:
								"start path": placer.inst_path_start(par.pos, par.rot)
								"path": placer.inst_path(par.pos, par.rot)
								"end path": placer.inst_path_end(par.pos, par.rot)
						Globals.StateType.VOXEL:
							placer.inst_voxel(par.pos, par.rot, selected)
							
				Globals.PlayerActions.PICK:
					selected = par.name
							
				Globals.PlayerActions.DELETE: 
					placer.delete(statetype, par.pos, par.rot)
					
				Globals.PlayerActions.CANCEL: to_pick()
					
				Globals.PlayerActions.CHANGE_TYPE:
					selected = ""
					editing_turret = ""
					state = Globals.PlayerState.PICK
					statetype = par.statetype
					build_option(state, statetype)
					
		Globals.PlayerState.EDIT:
			match action:
				Globals.PlayerActions.PICK:
					match statetype:
						Globals.StateType.TURRET:
							selected = par.name
							match par.type:
								"turret upg":
									upgrade(editing_turret, par.name)
								_ :
									match par.name:
										"targeting": 
											statetype = Globals.StateType.TARGETING
											build_option(state, statetype)
										"modules": 
											statetype = Globals.StateType.MODULES
											build_option(state, statetype)
										"sell": 
											sell(editing_turret)
											to_pick()
										"back": to_pick()
									
						Globals.StateType.TARGETING:
							selected = par.name
							match par.name:
								"back": 
									statetype = Globals.StateType.TURRET
									build_option(state, statetype)
									
						Globals.StateType.MODULES:
							selected = par.name
							match par.name:
								"add": 
									statetype = Globals.StateType.MODULES_PICK
									build_option(state, statetype)
								"back": 
									statetype = Globals.StateType.TURRET
									build_option(state, statetype)
									
						Globals.StateType.MODULES_PICK:
							selected = par.name
							match par.name:
								"back": 
									statetype = Globals.StateType.MODULES
									build_option(state, statetype)
								_: 
									statetype = Globals.StateType.MODULES
									var turr = turret_holder.get_node(editing_turret)
									turr.modules += [par.name]
									turr.refresh_info(turr.info)
									build_option(state, statetype)
								
						_: to_pick() 
							
				Globals.PlayerActions.CANCEL: to_pick()
					
				Globals.PlayerActions.CHANGE_TYPE:
					selected = ""
					editing_turret = ""
					state = Globals.PlayerState.PICK
					statetype = par.statetype
					build_option(state, statetype)
	_refresh()
	

func gui_editor_toggle_event ():
	ineditor = !ineditor
	if !ineditor: 
		statetype = Globals.StateType.TURRET
		to_pick()
	
	gui.refresh()
	path.refresh_path(ineditor)
	
func gui_start_wave_event ():
	if wave.ongoing: return
	path.refresh_path(ineditor)
	wave.start()
	gui.refresh()
	
func end_wave_event ():
	wave.end()
	
	for turr in turret_holder.get_children():
		if turr.info.has("resource_per_wave"):
			resources.add(turr.info.resource_per_wave)
	
	_refresh()
	
func gui_save_map_event (): saveload_map.map_save()

func gui_save_as_map_event (mapname : String):
	saveload_map.mapname = mapname
	saveload_map.map_save()
	
func gui_delete_map_event (mapname : String):
	saveload_map.map_delete(mapname)
	
func gui_change_map_event (mapname : String):
	saveload_map.mapname = mapname
	saveload_map.map_load()
	gui.load_map.visible = false
	
	path.refresh_path(ineditor)
	gui.refresh()