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

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

var gui : Node
var player : Node
var placer : Node
var resources : Node
var pointer : Node
var turret_holder : Node
var load_turrets : 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")
	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")
	var saveload = root.get_node("saveload")
	load_turrets = saveload.get_node("load_turrets")
	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": i, "color": color} ]
						
		Globals.PlayerState.EDIT:
			match sttype:
				Globals.StateType.TURRET:
					var tname = turret_holder.get_node(editing_turret).info.name
					for t in load_turrets.get_upg_turrets(tname):
						opts += [ { "type": "turret upg", "name": t.name } ]
					opts += [ { "type": "text", "name": "targeting" } ]
					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": "back" } ]
					
	gui.bottom_bar.picker.build(opts)
	
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(ineditor)
	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.selected
					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:
							var obj = placer.inst_turret(
								par.pos, par.rot, selected)
							editing_turret = obj.name
							state = Globals.PlayerState.EDIT
							build_option(state, statetype)
						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)
							
				Globals.PlayerActions.PICK:
					selected = par.selected
							
				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.selected
							match par.selected:
								"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.selected
							match par.selected:
								"back": 
									statetype = Globals.StateType.TURRET
									build_option(state, statetype)
									
						Globals.StateType.MODULES:
							selected = par.selected
							match par.selected:
								"back": 
									statetype = Globals.StateType.TURRET
									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
	gui.refresh(ineditor)
	path.refresh_path(ineditor)
	
func gui_start_wave_event ():
	path.refresh_path(ineditor)
	#_enemies.spawn()
	gui.refresh(ineditor)