-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanufacturing.gui_script
More file actions
105 lines (98 loc) · 3.17 KB
/
manufacturing.gui_script
File metadata and controls
105 lines (98 loc) · 3.17 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
local resource_types = {"wood", "stone", "iron", "copper", "gold", "steel", "food", "water", "arrows", "rckts"}
local smith_types = {"club", "sword", "slingshot", "bow", "arrow"}
local global = require "main.global" -- Import the module
_G.clubs = 0
_G.swords = 0
_G.slingshots = 0
_G.bows = 0
_G.M_menu_open = false
function init(self)
self.smith_menu = gui.get_node("smith_menu")
self.club_node = gui.get_node("club")
self.sword_node = gui.get_node("sword")
self.slingshot_node = gui.get_node("slingshot")
self.bow_node = gui.get_node("bow")
self.arrow_node = gui.get_node("arrow")
self.club_cost = {5, 5}
self.sword_cost = {10, 0, 25}
self.slingshot_cost = {10}
self.bow_cost = {25}
self.arrow_cost = {1, 1}
--msg.post(".", "release_input_focus")
gui.set_enabled(self.smith_menu, false)
self.club_position = vmath.vector3(1434, 567, 0)
self.sword_position = vmath.vector3(1434, 394, 0)
self.slingshot_position = vmath.vector3(1434, 482, 0)
self.bow_position = vmath.vector3(1434, 309, 0)
self.arrow_position = vmath.vector3(1434, 220, 0)
self.showing = false
self.current_smith = nil
self.unlocked_items = {"club","slingshot", "bow", "sword", "arrow"}
end
function on_message(self, message_id, message, sender)
if message_id == hash("toggle") then
self.showing = not self.showing
_G.M_menu_open = self.showing
gui.set_enabled(self.smith_menu, self.showing)
for _, node in ipairs(smith_types) do
local node_s = gui.get_node(node)
if global.contains(self.unlocked_items, node) then
gui.set_enabled(node_s, true)
else
gui.set_enabled(node_s, false)
end
end
if self.showing then
self.current_smith = message.smith
else
self.current_smith = nil
end
end
if message_id == hash("item_made") then
msg.post("#ui", "resource_update", {type = message.type .. "s", amount = 1})
end
if message_id == hash("level_up") then
if _G.level == 2 then
self.unlocked_items = {"club", "slingshot", "bow", "sword", "arrow"}
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
msg.post("#ui", "resource_update", {type = resource_types[i], amount = -needed[i]})
end
end
end
function on_input(self, action_id, action)
if action_id == hash("click") and action.pressed and _G.M_menu_open then
local positions = {self.club_position, self.sword_position, self.slingshot_position, self.bow_position, self.arrow_position}
local mouse_pos = vmath.vector3(action.x, action.y, 0)
print(mouse_pos)
local type
for _, position in ipairs(positions) do
if global.is_mouse_inside_collision(mouse_pos, position, 45, 45) then
type = smith_types[global.remove_by_value(positions, position)]
print(type)
end
end
if type then
local cost = self[type .. "_cost"]
if check_cost(self, cost) and global.contains(self.unlocked_items, type) then
take_resources(self, cost)
msg.post(self.current_smith, "new_item", {type = type})
end
end
end
end