-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParticleManager.lua
138 lines (103 loc) · 3.58 KB
/
ParticleManager.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
local ParticleManager = {}
function RandomFloat(min, max, precision)
local range = max - min
local offset = range * math.random()
local unrounded = min + offset
if not precision then
return unrounded
end
local powerOfTen = 10 ^ precision
return math.floor(unrounded * powerOfTen + 0.5) / powerOfTen
end
ParticleManager.particles = {}
CreateParticleStruct = function()
local pstruct = {
color = {
r = 1,
g = 1,
b = 1,
a = 1
},
pos = {
x = 0,
y = 0
},
gravity = {
x = 0,
y = 0
},
shape = 0,
angle = 0.0,
spread = 0.0,
minSpeed = 0.0,
maxSpeed = 0.0,
lifetime = 0.0,
startSize = 0.0,
endSize = 0.0,
fadeSpeed = 0.0
}
return pstruct
end
function copy(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
--use "struct" from CreateParticleStruct()
ParticleManager.SpawnParticle = function(self,particleStruct, count, pos)
for index = 1, count do
local newParticle = copy(particleStruct)
if pos ~= nil then
newParticle.pos = copy(pos)
end
newParticle.angle = newParticle.angle + RandomFloat(-newParticle.spread, newParticle.spread)
newParticle.speed = RandomFloat(newParticle.minSpeed,newParticle.maxSpeed)
newParticle.vel = {
x = math.cos(newParticle.angle) * math.max(newParticle.speed,00000.1),
y = math.sin(newParticle.angle) * math.max(newParticle.speed,00000.1)
}
newParticle.timer = 0
table.insert(self.particles,newParticle)
end
end
ParticleManager.Update = function(self,dt)
for index = #self.particles, 1, -1 do
local currentParticle = self.particles[index]
currentParticle.timer = currentParticle.timer + dt
currentParticle.color.a = currentParticle.color.a - currentParticle.fadeSpeed
if currentParticle.timer < currentParticle.lifetime then
currentParticle.vel.x = currentParticle.vel.x + currentParticle.gravity.x * dt
currentParticle.vel.y = currentParticle.vel.y + currentParticle.gravity.y * dt
currentParticle.pos.x = currentParticle.pos.x + currentParticle.vel.x * dt
currentParticle.pos.y = currentParticle.pos.y + currentParticle.vel.y * dt
else
table.remove(self.particles,index)
end
end
end
ParticleManager.Draw = function(self)
for index = #self.particles, 1, -1 do
local particle = self.particles[index]
love.graphics.push()
love.graphics.setColor(particle.color.r, particle.color.g, particle.color.b, particle.color.a)
love.graphics.translate(particle.pos.x, particle.pos.y)
local angle = math.atan2(particle.vel.y,particle.vel.x)
love.graphics.rotate(angle - 3.14/4)
love.graphics.translate(-particle.pos.x, -particle.pos.y)
local T = particle.timer / math.max(particle.lifetime,0.00001)
local size = T * particle.endSize + (1-T) * particle.startSize
love.graphics.setColor(0.2, 0.3, 0.4, particle.color.a)
-- if particle.timer < particle.lifetime then
--love.graphics.print(particle.vel.x, particle.pos.x + 50, particle.pos.y)
--end
love.graphics.setColor(particle.color.r, particle.color.g, particle.color.b, particle.color.a)
love.graphics.circle("fill", particle.pos.x, particle.pos.y, size, particle.shape)
love.graphics.pop()
end
love.graphics.setColor(1, 1, 1, 1)
end
return ParticleManager