-
Notifications
You must be signed in to change notification settings - Fork 0
/
explosives.lua
152 lines (121 loc) · 3.93 KB
/
explosives.lua
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
math = require "math"
utils = require "utils"
sprite = require "sprite"
flammable_module = require "flammable"
flammable = flammable_module.flammable
module(..., package.seeall)
ghost_flag = false
barrel_lock = false
--animations
barrel_burning_sheet = sprite.newSpriteSheet("barrel_burning.png", 147, 200)
barrel_burning_set = sprite.newSpriteSet(barrel_burning_sheet, 1, 8)
--sprite.add(barrel_burning_set, "burning", 1, 8, 400)
--the list of all barrels
barrels = {}
--explosive barrels
barrel = {}
setmetatable(barrel, {__index = flammable})
function barrel:new(x, y)
local barrelImage = sprite.newSprite(barrel_burning_set)
barrelImage.x = x
barrelImage.y = y
mainDisplay.mainDisplay:insert(barrelImage)
local instance = flammable:new(barrelImage, {radius = 70})
instance.body.isFixedRotation = true
instance.body.density = 1
instance.body.friction = 5
--barrels catch fire more easily than crates
instance.flash_point = instance.flash_point - 5
instance.health = 80
instance.max_burn_rate = 4
--ghost stuff
instance.ghost = nil
setmetatable(instance, {__index = barrel})
instance.body:addEventListener("touch", instance)
return instance
end
function barrel:spawn_ghost()
self.ghost = display.newImage(mainDisplay.mainDisplay, "Range.png")
self.ghost.x = self.body.x
self.ghost.y = self.body.y + 25
end
function barrel:kill_ghost()
if self.ghost ~= nil then
self.ghost:removeSelf()
end
end
function kill_ghosts()
table_size = table.getn(barrels)
for i=table_size, 1, -1 do
barrels[i]:kill_ghost()
end
end
function spawn_ghosts()
table_size = table.getn(barrels)
for i=table_size, 1, -1 do
barrels[i]:spawn_ghost()
end
end
function barrel:animate()
if self.current_heat >= self.flash_point then
--[[ghosts[utils.index_of(barrels, self)].x = self.body.x
ghosts[utils.index_of(barrels, self)].y = self.body.y+25]]
self.body.currentFrame = (80-self.health)/10
else
self.body.currentFrame = 1
end
end
--sets off the barrel when it's touched
function barrel:touch(event)
if event.phase == "began" and barrel_lock == false then
barrel_lock = true
self:apply_heat(self.flash_point)
return true
end
end
--makes the barrel explode shortly after catching fire
function barrel:on_enter_frame(elapsed_time)
--update heat
flammable.on_enter_frame(self, elapsed_time)
if self.ghost ~= nil then
self.ghost.x = self.body.x
self.ghost.y = self.body.y + 25
end
--update animation
self:animate()
end
--makes the barrel explode
function barrel:burn_up()
if ghost_flag == true then
self:kill_ghost()
end
table.remove(barrels, utils.index_of(barrels, self))
flammable.burn_up(self)
if levels.reset_lock == false then
spawn_explosion(self.body.x, self.body.y, 200, self.burn_temperature)
end
end
function spawn_barrel(x, y)
table.insert(barrels, barrel:new(x, y))
end
function spawn_explosion(x, y, radius, heat)
--[[local explode_sound = media.newEventSound( "Explosion.mp3" )
local playSound = function() media.playEventSound( explode_sound ) end
timer.performWithDelay(0, playSound, 0)]]
for i, flammable_obj in ipairs(flammable_module.flammable_list) do
local xDist = flammable_obj.body.x - x
local yDist = flammable_obj.body.y - y
local dist_squared = xDist^2 + yDist^2
if dist_squared < radius^2 then
flammable_obj:apply_heat(heat)
local dist = math.sqrt(dist_squared)
local force = (radius - dist) * 20
force = math.min(1500, force)
flammable_obj.body:applyLinearImpulse(
force * xDist / dist,
force * yDist / dist,
flammable_obj.body.x, flammable_obj.body.y)
end
end
media.playSound("Explosion.mp3", false)
end