-
Notifications
You must be signed in to change notification settings - Fork 9
/
particle.lua
157 lines (135 loc) · 3.55 KB
/
particle.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
152
153
154
155
156
157
local G = love.graphics
Particle = BatchDrawer(300, {
color = { 1, 1, 1 },
alive = true,
frame_length = 3,
layer = "front",
})
function Particle:init(x, y)
table.insert(self.list, self)
self.x = x
self.y = y
self.tick = 0
end
function Particle:draw()
if self.tick < 0 then return end
local f = math.floor(self.tick / self.frame_length) + 1
if self.quads[f] then
self.quads.batch:setColor(unpack(self.color))
self.quads.batch:add(self.quads[f], self.x, self.y, 0, 4, 4, self.size / 2, self.size / 2)
else
self.alive = false
end
end
function Particle:update()
self.y = self.y + game.walls.speed
self.tick = self.tick + 1
if not self.alive then return "kill" end
end
SparkParticle = Particle:New {
friction = 1
}
SparkParticle:InitQuads("media/spark.png")
function SparkParticle:init(x, y)
table.insert(self.list, self)
self.x = x
self.y = y
local ang = math.random() * 2 * math.pi
local s = math.random() * 2 + 2
self.vx = math.sin(ang) * s
self.vy = math.cos(ang) * s
self.ttl = math.random(3, 7)
end
function SparkParticle:update()
self.x = self.x + self.vx
self.y = self.y + self.vy + game.walls.speed
self.vx = self.vx * self.friction
self.vy = self.vy * self.friction
self.ttl = self.ttl - 1
if self.ttl <= 0 then return "kill" end
end
function SparkParticle:draw()
self.quads.batch:setColor(unpack(self.color))
self.quads.batch:add(self.quads[1], self.x, self.y, 0, 4, 4, 1.5, 1.5)
end
LaserParticle = SparkParticle:New {
color = {0, 0.61, 0.61},
friction = 0.7,
}
ExplosionSparkParticle = SparkParticle:New {
friction = 0.95,
}
function ExplosionSparkParticle:init(x, y)
table.insert(self.list, self)
self.x = x
self.y = y
local ang = math.random() * 2 * math.pi
local s = math.random() * 2 + 2
self.vx = math.sin(ang) * s
self.vy = math.cos(ang) * s
self.ttl = math.random(10, 15)
self.color = {1, math.random(), 0}
end
function ExplosionSparkParticle:draw()
local c = self.color
self.quads.batch:setColor(c[1], c[2], 0, math.min(1, self.ttl * 0.2))
self.quads.batch:add(self.quads[1], self.x, self.y, 0, 4, 4, 1.5, 1.5)
end
SmokeParticle = Particle:New {
size = 8,
friction = 0.8,
layer = "back"
}
SmokeParticle:InitQuads("media/smoke.png")
function SmokeParticle:init(x, y)
table.insert(self.list, self)
self.x = x
self.y = y
local ang = math.random() * 2 * math.pi
local s = math.random() * 2 + 3
self.vx = math.sin(ang) * s
self.vy = math.cos(ang) * s
self.ttl = math.random(20, 25)
end
SmokeParticle.update = SparkParticle.update
function SmokeParticle:draw()
local f = math.max(1, #self.quads - math.floor(self.ttl / 3))
self.quads.batch:setColor(0.12, 0.12, 0.12, 0.59)
self.quads.batch:add(self.quads[f], self.x, self.y, 0, 4, 4, self.size / 2, self.size / 2)
end
ExplosionParticle = Particle:New { size = 16 }
ExplosionParticle:InitQuads("media/explosion.png")
function makeExplosion(x, y)
sound.play("explosion", x, y)
-- heat wave
Boom(x, y)
for i = 1, 10 do
SmokeParticle(x, y)
end
for i = 1, 20 do
ExplosionSparkParticle(
x + math.random(-10, 10),
y + math.random(-10, 10))
end
ExplosionParticle(x, y)
end
SparkleParticle = Particle:New { size = 8 }
SparkleParticle:InitQuads("media/sparkle.png")
function SparkleParticle:update()
self.tick = self.tick + 1
if not self.alive then return "kill" end
end
FastSparkleParticle = Particle:New {
size = 8,
quads = SparkleParticle.quads,
}
function makeFastSparkleParticle(x, y)
local r = math.random() * 10
for i = 0, 2 do
local f = FastSparkleParticle(
x + math.sin(r) * 12,
y + math.cos(r) * 12)
r = r + 2/3 * math.pi
f.tick = i * -5
end
end