-
Notifications
You must be signed in to change notification settings - Fork 68
/
fire.lua
97 lines (83 loc) · 2.55 KB
/
fire.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
Fire = { max_health = 0.4 }
Fire.__index = Fire
local REGEN_RATE = 0.05
local SCORE = 20
function Fire.create(x,y,map)
local self = setmetatable({}, Fire)
self.alive = true
self.health = Fire.max_health/4
self.cx, self.cy = x,y
self.x, self.y = x*16, y*16
self.frame = math.random()*5
self.flframe = 0
self.MIN_SPREAD_WAIT = math.round(8 - map.section*(6/LAST_SECTION))
self.MAX_SPREAD_WAIT = math.round(14 - map.section*(10/LAST_SECTION))
self.nextSpread = math.random(self.MIN_SPREAD_WAIT, self.MAX_SPREAD_WAIT)
self.bbox = {x=self.x+4, y=self.y+4, w=8, h=8}
self.ceiling = map:collideCell(self.cx, self.cy-1)
self.ground = map:collideCell(self.cx, self.cy+1)
return self
end
function Fire:update(dt)
if self.health < Fire.max_health then
self.health = math.min(self.health+dt*REGEN_RATE, Fire.max_health)
else
self.nextSpread = self.nextSpread - dt
end
if self.nextSpread <= 0 then
self.nextSpread = math.random(self.MIN_SPREAD_WAIT, self.MAX_SPREAD_WAIT)
local cx,cy = self.cx, self.cy
while (cx == self.cx and cy == self.cy) do
if math.random(0,1) == 0 then -- vertically
if math.random(0,1) == 0 then -- up
cx, cy = self.cx, self.cy-1
if cy > 0 and map:canBurnCell(cx,cy) == false and math.random(0,4) == 0 then
cy = cy-1 -- burn through ceiling
end
else
cx, cy = self.cx, self.cy+1 -- down
if cy < 14 and map:canBurnCell(cx,cy) == false and math.random(0,4) == 0 then
cy = cy+1 -- burn through floor
end
end
else
if math.random(0,1) == 0 then -- horizontally
cx, cy = self.cx-1, self.cy -- left
else
cx, cy = self.cx+1, self.cy -- right
end
end
end
map:addFire(cx,cy)
end
self.frame = self.frame + 12*dt
end
function Fire:shot(dt,dir)
self.health = self.health - dt
if self.health < 0 then
self.alive = false
score = score + SCORE
end
end
function Fire:drawFront()
if self.ground == true then
love.graphics.drawq(img.fire_floor, quad.fire_floor[self.flframe%4], self.x, self.y+1)
end
if self.ceiling == true then
love.graphics.drawq(img.fire_floor, quad.fire_floor[self.flframe%4], self.x, self.y-1, 0,1,-1,0,16)
end
end
function Fire:drawBack()
self.flframe = math.floor(self.frame)
if self.health < Fire.max_health/2 then
love.graphics.drawq(img.fire_wall_small, quad.fire_wall[self.flframe%5], self.x, self.y, 0,1,1,4,16)
else
love.graphics.drawq(img.fire_wall, quad.fire_wall[self.flframe%5], self.x, self.y, 0,1,1,4,16)
end
end
function Fire:collideBox(box)
return collideBoxes(self.bbox,box)
end
function Fire:getBBox()
return self.bbox
end