-
Notifications
You must be signed in to change notification settings - Fork 0
/
particles.lua
98 lines (89 loc) · 2.28 KB
/
particles.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
local Particle, WindParticle, DoorParticle, CraftParticle = require 'particle' ()
local SharedState = require 'sharedstate'
local Particles = {
_particles = {},
_nextParticleIndex = 1,
_foregroundParticles = {},
_nextForegroundParticleIndex = 1,
}
function Particles:add(count, Kind, proto, foreground)
proto = proto or Kind:new()
for index = 0, count - 1 do
local clone = {}
for k, v in pairs(proto) do clone[k] = v end
clone.index = index
if foreground then
self._foregroundParticles[self._nextForegroundParticleIndex] = Kind:new(clone)
self._nextForegroundParticleIndex = self._nextForegroundParticleIndex + 1
else
self._particles[self._nextParticleIndex] = Kind:new(clone)
self._nextParticleIndex = self._nextParticleIndex + 1
end
end
end
function Particles:draw()
for index, particle in pairs(self._particles) do
particle:draw()
end
end
function Particles:drawForeground()
for index, particle in pairs(self._foregroundParticles) do
particle:draw()
end
end
function Particles:update(dt)
for index, particle in pairs(self._particles) do
particle:update(dt)
if particle.ttl <= 0 then
self._particles[index] = nil
end
end
for index, particle in pairs(self._foregroundParticles) do
particle:update(dt)
if particle.ttl <= 0 then
self._foregroundParticles[index] = nil
end
end
end
function Particles:doorOpened(door)
self:add(
3,
DoorParticle,
{
x = door.position.x,
y = door.position.y,
lifespan = 2,
},
false
)
end
function Particles:playerDeath(craft)
self:add(
10,
CraftParticle,
{
x = craft.position.x,
y = craft.position.y,
radius = 3,
lifespan = 0.5,
},
false
)
end
function Particles:wind()
local vx, vy = math.random(-600, -800), math.random(-10, 10)
self:add(
1,
WindParticle,
{
x = math.random(0, SharedState.viewport.width + 100),
y = math.random(0, SharedState.viewport.height),
vx = vx,
vy = vy,
radius = math.random(2, 4),
lifespan = SharedState.viewport.width / -vx,
},
true
)
end
return Particles