-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstone.script
More file actions
53 lines (47 loc) · 1.45 KB
/
stone.script
File metadata and controls
53 lines (47 loc) · 1.45 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
-- Tree Script
function init(self)
self.stone = math.random(100,500)
self.assigned_to_self = {}
end
local function find_pos(table, value)
for i, v in ipairs(table) do
if v == value then
return i
end
end
return nil
end
local function update_alpha(self)
-- Set sprite color to red
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 0, 0, 1))
-- Reset color after 1 second
timer.delay(0.5, false, function()
sprite.set_constant("#sprite", "tint", vmath.vector4(1, 1, 1, 1)) -- Back to normal
end)
end
-- Respond to mouse click if the tree was clicked
function on_message(self, message_id, message, sender)
if message_id == hash("harvested") then
self.stone = self.stone - message.amount
update_alpha(self)
if find_pos(self.assigned_to_self, message.person_id) == nil then
table.insert(self.assigned_to_self, message.person_id)
end
if self.stone <= 0 then
-- Correctly removing the tree using the game object ID
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="stone"})
for _, person in ipairs(_G.persons) do
msg.post(person, "resource_gone", {type = "Stone", r_id = go.get_id()})
end
go.delete()
end
end
if message_id == hash("leaving") then
if find_pos(self.assigned_to_self, message.person_id) == nil then
table.remove(self.assigned_to_self, find_pos(self.assigned_to_self, message.person_id))
end
end
end
-- Update function (for periodic tasks if needed)
function update(self, dt)
end