-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathparticles.lua
138 lines (113 loc) · 2.79 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
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
-------------------------------------------
-- Sparkle : Particle
-------------------------------------------
Sparkle = {}
Sparkle.__index = Sparkle
local lg = love.graphics
function Sparkle.create(x,y,count,color,time,ysp)
local self = {}
setmetatable(self, Sparkle)
self.alive = true
self.time = time or 1
self.count = count or 5
self.color = color or COLORS.yellow
self.particles = {}
for i=1, self.count do
self.particles[i] = {}
self.particles[i].x = x
self.particles[i].xspeed = math.random(-100,100)
self.particles[i].y = y
self.particles[i].yspeed = math.random(-200,50) + (ysp or 0)
end
return self
end
function addSparkle(...)
table.insert(map.particles, Sparkle.create(...))
end
function Sparkle:update(dt)
self.time = self.time - dt
if self.time < 0 then
self.alive = false
return
end
for i,v in ipairs(self.particles) do
v.x = v.x + v.xspeed*dt
v.yspeed = v.yspeed + 500*dt
v.y = v.y + v.yspeed*dt
end
end
function Sparkle:draw()
lg.setColor(self.color)
for i,v in ipairs(self.particles) do
lg.rectangle("fill", 0.5+v.x, 0.5+v.y, 1, 1)
end
lg.setColor(1,1,1,1)
end
-------------------------------------------
-- Dust : Particle
-------------------------------------------
Dust = {}
Dust.__index = Dust
function Dust.create(x,y)
local self = {}
setmetatable(self, Dust)
self.alive = true
self.time = 0
self.x = x
self.y = y
return self
end
function addDust(...)
table.insert(map.particles, Dust.create(...))
end
function Dust:update(dt)
self.time = self.time + dt
if self.time > 0.25 then
self.alive = false
return
end
end
function Dust:draw()
lg.setColor(COLORS.offwhite)
lg.rectangle("fill", self.x-self.time*16, self.y-self.time*16, 1,1)
lg.rectangle("fill", self.x+self.time*16, self.y-self.time*16, 1,1)
lg.rectangle("fill", self.x-self.time*16, self.y+self.time*16, 1,1)
lg.rectangle("fill", self.x+self.time*16, self.y+self.time*16, 1,1)
lg.setColor(1,1,1)
end
-------------------------------------------
-- Ring : Particle
-------------------------------------------
Ring = {}
Ring.__index = Ring
function Ring.create(x,y,count,radius,color)
local self = {}
setmetatable(self, Ring)
self.alive = true
self.x = x
self.y = y
self.time = 0.25
self.count = count or 8
self.radius = radius or 32
self.color = color or COLORS.yellow
return self
end
function addRing(...)
table.insert(map.particles, Ring.create(...))
end
function Ring:update(dt)
self.time = self.time - dt
if self.time < 0 then
self.alive = false
return
end
end
function Ring:draw()
lg.setColor(self.color)
for i=1,self.count do
local px = self.x + math.cos((i/self.count)*2*math.pi)*(0.25-self.time)*self.radius
local py = self.y + math.sin((i/self.count)*2*math.pi)*(0.25-self.time)*self.radius
lg.rectangle("fill", px, py, 1, 1)
end
lg.setColor(1,1,1,1)
end