-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsmith.script
More file actions
181 lines (168 loc) · 5.94 KB
/
smith.script
File metadata and controls
181 lines (168 loc) · 5.94 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
local global = require "main.global" -- Import the module
function init(self)
msg.post(".", "acquire_input_focus")
self.placed = false
self.camera_position = vmath.vector3()
self.global_pos = vmath.vector3()
self.local_pos = vmath.vector3()
self.health = 1
self.alpha = nil
self.fires = 0
self.effects = {}
self.queue = {}
self.current_item_type = nil
self.progress = 0
self.people_smithing = {}
table.insert(_G.smiths_to_build, go.get_id())
msg.post("#item", "disable")
end
local function damage_colour(self)
-- Set sprite color to red
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 0, 0, self.alpha))
-- Reset color after 1 second
timer.delay(0.2, false, function()
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 1, 1, self.alpha)) -- Back to normal
end)
end
local function update_health(self)
self.alpha = math.max(0.2, math.max(0, self.health / 500)) -- Clamp between 0 and 1
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 1, 1, self.alpha)) -- Back to normal
end
local function heal_colour(self)
-- Set sprite color to red
sprite.set_constant("#sprite", "tint", vmath.vector4(0, 1, 0, self.alpha))
-- Reset color after 1 second
timer.delay(0.2, false, function()
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 1, 1, self.alpha)) -- Back to normal
end)
end
local function update_item_alpha(self, progress)
local alpha = math.max(0.2, math.max(0, self.progress / 100)) -- Clamp between 0 and 1
sprite.set_constant("#item", "tint", vmath.vector4(1, 1, 1, alpha)) -- Back to normal
end
local function animate_item(self)
msg.post("#item", "enable")
update_item_alpha(self, 0)
local anim = hash("club")
if self.current_item_type == "arrow" then
anim = hash("arrow")
elseif self.current_item_type == "slingshot" then
anim = hash("slingshot")
elseif self.current_item_type == "bow" then
anim = hash("bow")
elseif self.current_item_type == "club" then
anim = hash("club")
elseif self.current_item_type == "sword" then
anim = hash("sword")
end
msg.post("#item", "play_animation", {id = anim})
end
function update(self, dt)
if #self.queue > 0 then
animate_item(self)
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("offset") then
self.camera_position = message.pos
end
if message_id == hash("new_item") then
heal_colour(self)
table.insert(self.queue, message.type)
if #self.queue == 1 then
self.current_item_type = self.queue[1]
end
end
if message_id == hash("smithed") and self.current_item_type then
if not global.contains(self.people_smithing, message.r_id) and #self.people_smithing < 10 then
table.insert(self.people_smithing, message.r_id)
end
if global.contains(self.people_smithing, message.r_id) then
self.progress = self.progress + 1
else
msg.post(sender, "task_done", {r_id = go.get_id()})
end
if self.progress > 99 then
print("Finished", self.current_item_type)
self.people_smithing = {}
self.progress = 0
msg.post("/gui#manufacturing", "item_made", {type = self.current_item_type})
table.remove(self.queue, 1)
if #self.queue > 0 then
self.current_item_type = self.queue[1]
else
self.current_item_type = nil
msg.post("#item", "disable")
for _, person in ipairs(_G.persons) do
msg.post(person, "task_done", {r_id = go.get_id()})
end
end
end
end
if message_id == hash("hit") then
self.health = self.health - message.damage
update_health(self)
damage_colour(self)
local firestarter = math.random(1,100)
if firestarter < 20 and self.fires < 2 then
msg.post("/initfactory#init_spawner", "create_fire", {pos = go.get_position(), build = go.get_id(), big = false})
self.fires = self.fires + 1
end
if not global.contains(_G.smiths_to_build, go.get_id()) then
table.insert(_G.smiths_to_build, go.get_id())
end
if self.health < 0 then
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="smith"})
table.remove(_G.smiths_to_build, global.remove_by_value(_G.smiths_to_build, go.get_id()))
for _, effect in ipairs(self.effects) do
msg.post(effect, "delete")
end
go.delete()
end
end
if message_id == hash("build") then
if self.health < 500 and _G.wood > 0 and _G.stone > 0 then
self.health = self.health + message.amount
update_health(self)
heal_colour(self)
msg.post("/gui#ui", "resource_update", {type = "wood", amount = -1})
msg.post("/gui#ui", "resource_update", {type = "stone", amount = -1})
else
if global.contains(_G.smiths_to_build, go.get_id()) then
table.remove(_G.smiths_to_build, global.remove_by_value(_G.smiths_to_build, go.get_id()))
end
for _, person in ipairs(_G.persons) do
msg.post(person, "build_done", {r_id = go.get_id()})
end
end
end
if message_id == hash("try_place") then
if message.bool then
self.placed = true
msg.post(".", "release_input_focus")
msg.post("/initfactory#init_spawner", "acquire_input_focus")
timer.delay(0.1, false, function()
_G.build_in_hand = false
end)
end
end
if message_id == hash("created_fire") then
table.insert(self.effects, message.fire)
table.insert(self.effects, message.smoke)
end
end
function on_input(self, action_id, action)
-- While mouse is held down, follow mouse position
if (action_id == hash("touch") or action_id == hash("mouse")) and not self.placed then
-- Convert screen position to world position for following the mouse
local pos = global.convert_screen_pos(vmath.vector3(action.x, action.y, 0), self.camera_position)
go.set_position(pos) -- Update position of the object to follow mouse
update_health(self)
end
-- When mouse is released, determine the final world position
if action_id == hash("mouse") and action.released and not self.placed then
-- Convert screen position to world position for correct placement
self.global_pos = global.convert_screen_pos(vmath.vector3(action.x, action.y, 0), self.camera_position)
msg.post("/initfactory#init_spawner", "try_place", { pos = self.global_pos })
end
end