-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.script
More file actions
120 lines (106 loc) · 3.51 KB
/
base.script
File metadata and controls
120 lines (106 loc) · 3.51 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
--local global = require "main.global" -- Import the module
local function set_base_type(self)
local anim = hash("base-1")
if _G.level == 1 then
anim = hash("base-1")
elseif _G.level == 2 then
anim = hash("base-2")
elseif _G.level > 2 then
anim = hash("base-3")
end
msg.post("#sprite", "play_animation", { id = anim })
end
function init(self)
msg.post(".", "acquire_input_focus")
self.placed = true
self.camera_position = vmath.vector3()
self.global_pos = vmath.vector3()
self.health = 1000
self.alpha = 0.01
self.built = true
_G.base_pos = go.get_position()
self.update_time = 0.1
self.create_person_time = 1
self.fires = 0
self.effects = {}
set_base_type(self)
for i = 1, 20, 1 do
msg.post("initfactory#init_spawner", "create_person", {pos = go.get_position()})
end
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 / 1000)) -- 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 spawn_people(self, dt)
if _G.food >= #_G.persons*50 and _G.water >= #_G.persons*50 and #_G.persons < #_G.houses + 20 then
msg.post("initfactory#init_spawner", "create_person", {pos = go.get_position()})
end
end
function update(self, dt)
local world_pos = go.get_position()
msg.post("#baseUI", "update_gui_position", { pos = world_pos })
msg.post("#baseUI", "update_base_range")
self.update_time = 0.1
self.create_person_time = self.create_person_time - dt
if self.create_person_time < 0 then
spawn_people(self, dt)
self.create_person_time = 1
end
set_base_type(self)
end
function on_message(self, message_id, message, sender)
if message_id == hash("hit") then
self.health = self.health - message.damage
update_health(self)
damage_colour(self)
local firestarter = math.random(1,1000)
if firestarter < 20 and self.fires < 6 then
msg.post("/initfactory#init_spawner", "create_fire", {pos = go.get_position(), build = go.get_id(), big = true})
self.fires = self.fires + 1
end
if self.health < 0 then
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="base"})
for _, effect in ipairs(self.effects) do
msg.post(effect, "delete")
end
go.delete()
end
end
if message_id == hash("build") then --NOT IN USE
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
for _, person in ipairs(_G.persons) do
msg.post(person, "build_done", {r_id = go.get_id()})
end
for _, effect in ipairs(self.effects) do
msg.post(effect, "delete")
end
end
end
if message_id == hash("created_fire") then
table.insert(self.effects, message.fire)
table.insert(self.effects, message.smoke)
end
end