-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresources.gui_script
More file actions
373 lines (336 loc) · 12.9 KB
/
resources.gui_script
File metadata and controls
373 lines (336 loc) · 12.9 KB
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
local resource_types = {"wood", "stone", "iron", "copper", "gold", "steel", "food", "water", "arrows", "rckts", "clubs", "swords", "slingshots", "bows"}
local lazy = {"Wood", "Stone", "Iron", "Copper", "Gold", "Steel", "Food", "Water", "Arrows", "Rockets", "Clubs", "Swords", "Slingshots", "Bows"}
local lazy2 = {"people", "selected", "moving", "harvesting", "fighting", "building", "training", "smithing", "unloading"}
local lazy3 = {"people", "selected", "moving", "harvesting", "fighting", "building", "training", "smithing", "housing", "unloading"}
local global = require "main.global" -- Import the module
local buildings = {"house", "farm", "barrack", "smith", "refinery", "turret"}
_G.wood = 10000
_G.stone = 10000
_G.iron = 0
_G.copper = 0
_G.gold = 0
_G.food = 500
_G.water = 1000
_G.steel = 0
_G.arrows = 0
_G.rckts = 0
_G.housing = 0
local function hide_building_ui(self, available, tf)
gui.set_visible(self.left_arrow_node, tf)
gui.set_visible(self.right_arrow_node, tf)
for _, building in ipairs(available) do
local node = building .. "_node"
local text_node = building .. "_node_text"
if self[node] then
gui.set_visible(self[node], tf)
end
if self[text_node] then
gui.set_visible(self[text_node], tf)
end
end
end
function init(self)
self.test_values = true
-- Find the label node you want to animate
self.wood_node = gui.get_node("wood")
self.stone_node = gui.get_node("stone")
self.iron_node = gui.get_node("iron")
self.copper_node = gui.get_node("copper")
self.gold_node = gui.get_node("gold")
self.food_node = gui.get_node("food")
self.water_node = gui.get_node("water")
self.steel_node = gui.get_node("steel")
self.arrows_node = gui.get_node("arrows")
self.rckts_node = gui.get_node("rckts")
-- Get the initial size of the label (to shrink back to)
self.initial_size = gui.get_scale(self.wood_node)
self.build_open = false
msg.post(".", "acquire_input_focus")
self.house_node = gui.get_node("house")
self.house_node_text = gui.get_node("house_mats")
self.farm_node = gui.get_node("farm")
self.farm_node_text = gui.get_node("farm_mats")
self.barrack_node = gui.get_node("barracks")
self.barrack_node_text = gui.get_node("barracks_mats")
self.smith_node = gui.get_node("smith")
self.smith_node_text = gui.get_node("smith_mats")
self.refinery_node = gui.get_node("refinery")
self.refinery_node_text = gui.get_node("refinery_mats")
self.turret_node = gui.get_node("turret")
self.turret_node_text = gui.get_node("turret_mats")
self.left_arrow_node = gui.get_node("left")
self.right_arrow_node = gui.get_node("right")
hide_building_ui(self, buildings, false)
self.slot1 = vmath.vector3(300, 748, 0)
self.slot2 = vmath.vector3(520, 748, 0)
self.slot3 = vmath.vector3(740, 748, 0)
self.slot4 = vmath.vector3(960, 748, 0)
self.lbutton = vmath.vector3(210, 748, 0)
self.rbutton = vmath.vector3(1220, 748, 0)
self.house_cost = {200, 200, 100}
self.farm_cost = {250, 250}
self.barrack_cost = {1, 1}
self.smith_cost = {300, 300}
self.refinery_cost = {100, 500, 500, 500, 500}
self.turret_cost = {0, 0, 1000, 250, 250, 250}
self.people = 0
self.people_node = gui.get_node("people")
self.selected = 0
self.selected_people_node = gui.get_node("selected")
self.moving = 0
self.moving_people_node = gui.get_node("moving")
self.harvesting = 0
self.harvesting_people_node = gui.get_node("harvesting")
self.fighting = 0
self.fighting_people_node = gui.get_node("fighting")
self.training = 0
self.training_people_node = gui.get_node("training")
self.smithing = 0
self.smithing_people_node = gui.get_node("smithing")
self.unloading = 0
self.unloading_people_node = gui.get_node("unloading")
self.housing_space_node = gui.get_node("housing")
self.select_box_node = gui.get_node("select_box")
self.camera_position = vmath.vector3()
self.global_pos = vmath.vector3()
self.update_people_time = 1
self.progress_bar_node = gui.get_node("progress_fill")
self.pgt = 2
self.unlocked_nodes = {"house", "farm", "barrack", "smith", "refinery", "turret"}
self.visible_nodes = global.slice(self.unlocked_nodes, 1, 4)
end
local function animate_expansion_and_shrinking(self, node)
msg.post("/initfactory#init_spawner", "steel", {steel = self.steel})
-- Define the expanded size (e.g., 1.5x the original size)
local expanded_size = vmath.vector3(self.initial_size.x * 1.2, self.initial_size.y * 1.2, 0)
gui.animate(node, gui.PROP_SCALE, expanded_size, gui.EASING_LINEAR, 0.1, 0, function()
gui.animate(node, gui.PROP_SCALE, self.initial_size, gui.EASING_LINEAR, 0.1)
end)
end
local function adjust(self, name, var, amount)
if self[var] + amount >= 0 then
self[var] = self[var] + amount
local node = gui.get_node(var) -- Get the actual node reference
gui.set_text(node, name .. ": " .. tostring(self[var]))
if not global.contains(lazy3, var) then
animate_expansion_and_shrinking(self, node) -- Pass the node reference
end
end
end
local function adjust_g(self, name, var, amount)
if _G[var] + amount >= 0 then
_G[var] = _G[var] + amount
local node = gui.get_node(var) -- Get the actual node reference
gui.set_text(node, name .. ": " .. tostring(_G[var]))
if not global.contains(lazy3, var) then
animate_expansion_and_shrinking(self, node) -- Pass the node reference
end
end
end
local function change_val(self, name, var, value)
local node = gui.get_node(var) -- Get the actual node reference
if self[var] ~= value and not global.contains(lazy3, var) then
animate_expansion_and_shrinking(self, node) -- Pass the node reference
end
self[var] = value
gui.set_text(node, name .. ": " .. tostring(self[var]))
end
function update(self, dt)
self.update_people_time = self.update_people_time - dt
if self.update_people_time < 0 then
for i = 1, #lazy2, 1 do
change_val(self, global.capitalize(lazy2[i]), lazy2[i], 0)
end
for _, person in ipairs(_G.persons) do
msg.post(person, "get_state")
end
end
if self.test_values then
for i = 1, #resource_types, 1 do
_G[resource_types[i]] = 1000000
local node = gui.get_node(resource_types[i]) -- Get the actual node reference
gui.set_text(node, lazy[i] .. ": " .. tostring(_G[resource_types[i]]))
end
self.test_values = false
end
end
function on_message(self, message_id, message, sender) --Rewrite the inputs for update
if message_id == hash("state") then
local lower
adjust(self, "People", "people", 1)
if message.state ~= "Idle" then
lower = string.lower(message.state)
adjust(self, tostring(message.state), lower, 1)
end
end
if message_id == hash("resource_update") then
local type = message.type
local text = global.capitalize(type)
if type == "cave" then
local picker = math.random(1, 3)
if picker == 1 then
adjust_g(self, "Iron", "iron", message.amount)
elseif picker == 2 then
adjust_g(self, "Copper", "copper", message.amount)
elseif picker == 3 then
adjust_g(self, "Gold", "gold", message.amount)
end
else
adjust_g(self, text, type, message.amount)
end
elseif message_id == hash("refine") then
if _G.iron > 0 then
adjust_g(self, "Iron", "iron", -1)
adjust_g(self, "Steel", "steel", 1)
end
elseif message_id == hash("enemy_killed") then
local mult = message.mult
elseif message_id == hash("offset_ui") then
self.camera_position = message.pos
local node_pos = gui.get_position(self.select_box_node) -- Get owner's new position
self.global_pos = global.convert_click_pos(node_pos, self.camera_position)
elseif message_id == hash("hide_select_box") then
gui.set_visible(self.select_box_node, false)
elseif message_id == hash("select_box") then
gui.set_visible(self.select_box_node, true)
-- Convert world positions to GUI screen positions
local start_pos, size = global.get_rect_from_drag(message.start_pos, message.end_pos)
-- Convert world position to screen position for GUI placement
local screen_start_pos = start_pos
-- Adjust the position to be at the center of the selection box
screen_start_pos.x = screen_start_pos.x + size.x / 2
screen_start_pos.y = screen_start_pos.y + size.y / 2
-- Apply the corrected position and size to the GUI node
gui.set_position(self.select_box_node, screen_start_pos)
gui.set_size(self.select_box_node, size)
elseif message_id == hash("progress") then
local size = gui.get_size(self.progress_bar_node)
size = vmath.vector3(size.x + message.amount, size.y, 0)
local pos = vmath.vector3(-647.0 + size.x/2, 0, 0)
if size.x <= 1300 then
gui.set_size(self.progress_bar_node, size)
gui.set_position(self.progress_bar_node, pos)
else
gui.set_size(self.progress_bar_node, vmath.vector3(1, size.y, 0))
gui.set_position(self.progress_bar_node, vmath.vector3(-647.0, 0, 0))
_G.level = _G.level + 1
msg.post("#manufacturing", "level_up")
_G.base_range = _G.base_range + 100
end
end
end
local function check_cost(self, needed)
for i = 1, #needed, 1 do
if _G[resource_types[i]] == nil then
return false
end
if needed[i] > _G[resource_types[i]] then
return false
end
end
return true
end
local function take_resources(self, needed)
for i = 1, #needed, 1 do
if _G[resource_types[i]] >= needed[i] and needed[i] ~= 0 then
_G[resource_types[i]] = _G[resource_types[i]] - needed[i]
local node = gui.get_node(resource_types[i]) -- Get the actual node reference
gui.set_text(node, lazy[i] .. ": " .. tostring(_G[resource_types[i]]))
animate_expansion_and_shrinking(self, node)
end
end
end
local function slide(self, nodes, direction)
local duration = 1 -- Default animation time
local dir
if direction == "Left" then
dir = 220
else
dir = -220
end
for _, node in ipairs(nodes) do
local node_path = self[node .. "_node"]
local target_x = gui.get_position(node_path).x - dir
local target_pos = vmath.vector3(target_x, gui.get_position(node_path).y, 0)
gui.animate(node_path, "position", target_pos, gui.EASING_OUTQUAD, duration, 0)
end
end
local function leftrightbuttonsclicked(self, button)
-- Create a copy of current visible nodes for comparison
local previous_visible = {}
for i, v in ipairs(self.visible_nodes) do
previous_visible[i] = v
end
if button == "Left" then
-- Move left (show earlier nodes)
if not (self.visible_nodes[#self.visible_nodes] == "turret") then
local current_index = global.remove_by_value(self.unlocked_nodes, self.visible_nodes[1])
if current_index and current_index > 0 then
-- Get previous 4 nodes
self.visible_nodes = global.slice(self.unlocked_nodes, current_index + 1, current_index + 4)
slide(self, self.unlocked_nodes, "Left")
end
end
elseif button == "Right" then
-- Move right (show later nodes)
if not (self.visible_nodes[1] == "house") then
local current_index = global.remove_by_value(self.unlocked_nodes, self.visible_nodes[#self.visible_nodes])
if current_index and current_index < #self.unlocked_nodes + 1 then
-- Get next 4 nodes
slide(self, self.unlocked_nodes, "Right")
self.visible_nodes = global.slice(self.unlocked_nodes, current_index-4, current_index-1)
for _, node in ipairs(self.visible_nodes) do
end
end
end
end
-- Only update UI if nodes changed
if not global.tables_equal(previous_visible, self.visible_nodes) then
hide_building_ui(self, self.unlocked_nodes, false)
hide_building_ui(self, self.visible_nodes, true)
end
end
function on_input(self, action_id, action)
if action_id == hash("click") and action.pressed and self.build_open and not _G.build_in_hand then
self.mouse_pos = vmath.vector3(action.x, action.y, 0)
local type
-- Detect button presses
local lbutton = global.is_mouse_inside_collision(self.mouse_pos, self.lbutton, 25, 25)
local rbutton = global.is_mouse_inside_collision(self.mouse_pos, self.rbutton, 25, 25)
if (lbutton or rbutton) and action.pressed and #self.unlocked_nodes > 4 then
if lbutton then
leftrightbuttonsclicked(self, "Right")
else
leftrightbuttonsclicked(self, "Left")
end
end
local slots = {self.slot1, self.slot2, self.slot3, self.slot4}
local clicked = nil
type = nil
for _, slot in ipairs(slots) do
if global.is_mouse_inside_collision(self.mouse_pos, slot, 100, 100) then
clicked = slot
end
end
if clicked ~= nil and self.visible_nodes[global.remove_by_value(slots, clicked)] ~= nil then
type = self.visible_nodes[global.remove_by_value(slots, clicked)]
local cost = self[type .. "_cost"]
if check_cost(self, cost) then
take_resources(self, cost)
msg.post("/initfactory#init_spawner", "build_button", {type = type})
msg.post("/initfactory#init_spawner", "release_input_focus")
hide_building_ui(self, self.visible_nodes, false)
end
end
end
if action_id == hash("b_button") and action.pressed and not _G.build_in_hand and not _G.M_menu_open then
self.build_open = not self.build_open
hide_building_ui(self, self.visible_nodes, self.build_open)
print("Showing")
if not self.build_open then
msg.post("/initfactory#init_spawner", "acquire_input_focus")
else
msg.post("/initfactory#init_spawner", "release_input_focus")
end
end
end