-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathturret.script
More file actions
176 lines (159 loc) · 5.56 KB
/
turret.script
File metadata and controls
176 lines (159 loc) · 5.56 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
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.on = true
self.health = 1
self.target_pos = nil
self.target = nil
self.attack_time = 1
self.accuracy = 5
self.scan_time = 1
self.damage = 15
self.alpha = 0.01
self.build_time = 0.1
self.fires = 0
self.effects = {}
table.insert(_G.turrets_to_build, go.get_id())
end
local function scan(self, dt)
self.scan_time = self.scan_time - dt
if self.scan_time < 0 then
msg.post("/initfactory#init_spawner", "scan_turret_radius", {person_pos = go.get_position()})
self.scan_time = 1
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 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_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 fight(self, dt)
self.attack_time = self.attack_time - dt
if self.attack_time < 0 and go.exists(self.target) and self.placed then
if global.distance_to(go.get_position(), go.get_position(self.target)) > 350 then
scan(self, dt)
return nil
end
if _G.rckts <= 0 then
return nil
else
msg.post("/gui#ui", "resource_update", {type = "rckts", amount = -1})
end
local direction = vmath.normalize(go.get_position(self.target) - go.get_position())
local angle = math.atan2(direction.y, direction.x)
local angle2 = math.rad(math.random(-self.accuracy,self.accuracy))
local new_rotation = vmath.quat_rotation_z(angle) * vmath.quat_rotation_z(angle2)
local rocket = factory.create("#rocket_factory", go.get_position(), new_rotation)
msg.post(rocket, "info", {damage = self.damage, life = 5, design = "rocket"})
self.attack_time = 0.5
end
end
function update(self, dt)
if not global.contains(_G.enemys, self.target) then
scan(self, dt)
else
fight(self, dt)
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("build") then
if self.health < 500 and _G.steel > 0 then
self.health = self.health + message.amount
update_health(self)
heal_colour(self)
msg.post("/gui#ui", "resource_update", {type = "steel", amount = -1})
else
if global.contains(_G.turrets_to_build, go.get_id()) then
table.remove(_G.turrets_to_build, global.remove_by_value(_G.turrets_to_build, go.get_id()))
end
for _, person in ipairs(_G.persons) do
msg.post(person, "build_done", {r_id = go.get_id(), type = "Turret"})
end
for _, effect in ipairs(self.effects) do
msg.post(effect, "delete")
end
end
end
if message_id == hash("hit") then
self.health = self.health - message.damage
if not global.contains(_G.turrets_to_build, go.get_id()) then
table.insert(_G.turrets_to_build, go.get_id())
end
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 self.health < 0 then
msg.post("/initfactory#init_spawner", "gone", {r_id = go.get_id(), type="turret"})
table.remove(_G.turrets_to_build, global.remove_by_value(_G.turrets_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("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("toggle") then
self.on = message.r
end
if message_id == hash("enemy") then
self.target_pos = message.pos
self.target = message.target
if not go.exists(self.target) then
self.target = 0
self.target_pos = nil
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