-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarracks.script
More file actions
116 lines (106 loc) · 3.99 KB
/
barracks.script
File metadata and controls
116 lines (106 loc) · 3.99 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
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 = {}
table.insert(_G.barracks_to_build, go.get_id())
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
function update(self, dt)
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("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.barracks_to_build, go.get_id()) then
table.insert(_G.barracks_to_build, go.get_id())
end
if self.health < 0 then
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="barrack"})
table.remove(_G.houses_to_build, global.remove_by_value(_G.barracks_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.barracks_to_build, go.get_id()) then
table.remove(_G.barracks_to_build, global.remove_by_value(_G.barracks_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