-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.lua
140 lines (118 loc) · 2.64 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
local Particle = {
x = 0,
y = 0,
radius = 0,
ttl = 0,
lifespan = 0
}
function Particle:new(p)
p = p or {}
setmetatable(p, { __index = self })
p.ttl = p.lifespan
return p
end
function Particle:update(dt)
self.ttl = self.ttl - dt
end
function Particle:draw(dt)
if self.ttl > 0 then
love.graphics.setColor(1, 1, 1, (self.ttl / self.lifespan))
love.graphics.circle('fill', self.x, self.y, self.radius)
end
end
-- CraftParticle
local CraftParticle = {
x = 0,
y = 0,
vx = 0,
vy = 0,
radius = 0,
ttl = 0,
lifespan = 0
}
function CraftParticle:new(p)
p = p or {}
setmetatable(p, { __index = self })
p.ttl = p.lifespan
p.x = p.x + math.random(-12, 12)
p.y = p.y + math.random(-12, 12)
local angle = math.random() * math.pi * 2
local magnitude = math.random(250, 500)
p.vx = magnitude * math.cos(angle)
p.vy = magnitude * math.sin(angle)
return p
end
function CraftParticle:update(dt)
self.ttl = self.ttl - dt
self.x = self.x + self.vx * dt
self.y = self.y + self.vy * dt
self.vx = self.vx * 0.98
self.vy = self.vy * 0.98
end
function CraftParticle:draw(dt)
if self.ttl > 0 then
love.graphics.setColor(1, 1, 1, (self.ttl / self.lifespan))
love.graphics.circle('fill', self.x, self.y, self.radius)
end
end
-- DoorParticle
local DoorParticle = {
x = 0,
y = 0,
r = 0,
vr = 0,
ttl = 0,
lifespan = 0
}
function DoorParticle:new(p)
p = p or {}
setmetatable(p, { __index = self })
p.ttl = p.lifespan
p.vr = 150 + p.index * 25
return p
end
function DoorParticle:update(dt)
self.ttl = self.ttl - dt
self.r = self.r + self.vr * dt
self.vr = self.vr - (75 * dt)
if self.vr < 0 then self.vr = 0 end
end
function DoorParticle:draw(dt)
if self.ttl > 0 then
love.graphics.setColor(1, 1, 1, (self.ttl / self.lifespan))
love.graphics.circle('line', self.x, self.y, self.r)
end
end
-- WindParticle
local WindParticle = {
x = 0,
y = 0,
yInit = 0,
vx = 0,
vy = 0,
radius = 0,
ttl = 0,
lifespan = 0
}
function WindParticle:new(p)
p = p or {}
setmetatable(p, { __index = self })
p.ttl = p.lifespan
p.yInit = p.y - p.vy
return p
end
function WindParticle:update(dt)
self.ttl = self.ttl - dt
self.vy = self.vy + (self.yInit - self.y) * 0.1
self.x = self.x + self.vx * dt
self.y = self.y + self.vy * dt
end
function WindParticle:draw(dt)
if self.ttl > 0 then
love.graphics.setColor(0, 0, 0, 0.3)
love.graphics.circle('fill', self.x, self.y, self.radius)
end
end
return function()
return Particle, WindParticle, DoorParticle, CraftParticle
end