-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenemy_base.script
More file actions
91 lines (81 loc) · 2.72 KB
/
enemy_base.script
File metadata and controls
91 lines (81 loc) · 2.72 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
local global = require "main.global" -- Import the module
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
self.type = "person"
self.spawn_time = 1
self.charges = 15
self.charge_time = 10
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(self, dt)
self.spawn_time = self.spawn_time - dt
if self.spawn_time < 0 and #_G.enemys < #_G.enemy_bases*5 + (_G.level * _G.level) and self.charges > 0 then
msg.post("/initfactory#init_spawner", "create_enemy", {pos = go.get_position(), base = go.get_id()})
self.spawn_time = 5
self.charges = self.charges - 1
end
self.charge_time = self.charge_time - dt
if self.charge_time < 0 and self.charges < 15 then
self.charges = 15
self.charge_time = 10
end
end
function update(self, dt)
spawn(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)
if self.health < 0 then
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="enemy_base"})
for _, person_id in ipairs(_G.persons) do
msg.post(person_id, "enemy_destroyed", { enemy_id = go.get_id() })
end
msg.post("/gui#ui", "progress", {amount = 100})
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 + 10
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
end
end
end