-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgen.lua
120 lines (99 loc) · 3.12 KB
/
pgen.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
require("class")
-- This particle generator is a generator which can be used for H to play with.
ParticleGenerator = class()
function ParticleGenerator:_init()
-- Spawn point, or just the origin of the generator
self.origin = { 0, 0 }
-- The 'spread' or distributions in the x (left, right)
-- and y (top, bottom) axes.
self.delta = {
x = { -80, 80 },
y = { -80, 80 },
}
-- 'Gravity' modifier. 0 will disable gravity
self.gravity = 0
-- Set to true to loop the generator.
self.continuous = false
-- Life of each particle, in seconds.
self.maxlife = 10
-- Maximum radius in pixels, per particle.
self.maxradius = 8
self.particles = {}
self.particleCount = 1000
-- The colorfunction determines what color each particle gets during
-- their lifetime. The default is
self.colorfunction = function(lifepercentage)
return {
1,
lifepercentage,
lifepercentage,
1
}
end
end
--[[
-- Initializes the particle generator with all the parameters involved. This will
-- re-create the particles table (with zero particles), then initializes all particles
-- with the given properties.
--]]
function ParticleGenerator:init(origin)
self.origin = origin
self.particles = {}
for i = 1, self.particleCount do
local p = {}
self:resetParticle(p)
table.insert(self.particles, p)
end
end
-- Resets a particle's parameters, by using the properties of the
-- parent generator.
function ParticleGenerator:resetParticle(p)
local maxlife = love.math.random() * self.maxlife
p.x = self.origin[1]
p.y = self.origin[2]
p.color = self.colorfunction(1)
p.radius = self.maxradius
p.maxradius = self.maxradius
p.dx = love.math.random(self.delta.x[1], self.delta.x[2])
p.dy = love.math.random(self.delta.y[1], self.delta.y[2])
p.life = maxlife -- life in seconds
p.maxlife = maxlife -- life maximum
end
--[[
-- Updates the particle generator's particle positions, color, radius and whatever,
-- every frame.
--]]
function ParticleGenerator:update(dt)
for k, p in ipairs(self.particles) do
if p and p.life >= 0 then
local lifepercentage = p.life / p.maxlife
p.x = p.x + p.dx * dt
p.y = p.y + p.dy * dt
p.dy = p.dy - self.gravity
if p.x <= 0 or p.x >= love.graphics.getWidth() then
p.dx = -p.dx
end
p.color = self.colorfunction(lifepercentage)
p.life = p.life - dt
-- Change the radius. The radius must decline to zero in proportion
-- to the lifetime of the particle. Meaning, if the lifetime of the
-- particle is 4.8 seconds, after 4.8 seconds we must have reached
-- a radius of 0. We do that by just setting by calculating the
-- percentage of the lifetime, multiplied by the original max radius.
p.radius = p.maxradius * (lifepercentage)
elseif p.life < 0 and self.continuous then
-- If we need to emit continuously, reset the particle.
self:resetParticle(p)
end
end
end
function ParticleGenerator:draw()
-- love.graphics.setPointSize(1)
for k, p in ipairs(self.particles) do
if p and p.life > 0 then
love.graphics.setColor(p.color)
love.graphics.circle('fill', p.x, p.y, p.radius)
-- love.graphics.points(p.x, p.y)
end
end
end