3 function particle
:spawn (x
,y
,vx
,vy
,maxage
)
4 local p
= {x
=x
,y
=y
,vx
=vx
,vy
=vy
,age
=0,maxage
= maxage
or 10}
5 self
.list
[#self
.list
+1] = p
8 local lut
= {{0,0,0,0.3,5,t
=0},{1,1,1,0.2,10,t
=0.2},{0,0,1,0,50,t
=1}}
9 table.sort(lut
,function(a
,b
) return a
.t
<b
.t
end)
12 local simtime
= glfw
.GetTime()
13 local updateinterval
= .01
14 function particle
:step(catchup
)
15 catchup
= catchup
or 0
16 local now
= glfw
.GetTime()
17 if simtime
+updateinterval
>now
then return end
18 simtime
= simtime
+ updateinterval
20 if math
.random()>.9 then
21 local p
= particle
:spawn(200,200,(math
.random()-.5),(math
.random()-.4),math
.random(1000,1800))
23 p
.vx
,p
.vy
= p
.vx
*.99,p
.vy
*.99-.002
24 local m
,a
= self
.maxage
,self
.age
27 local mincol
,maxcol
= lut
[1],lut
[#lut
]
29 if lut
[i
].t
> mincol
.t
and lut
[i
].t
< p
then mincol
= lut
[i
] end
30 if lut
[i
].t
>= p
then maxcol
= lut
[i
] break end
32 local p
= (p
- mincol
.t
) / (maxcol
.t
- mincol
.t
)
33 local r1
,g1
,b1
,a1
,s1
= unpack(mincol
)
34 local r2
,g2
,b2
,a2
,s2
= unpack(maxcol
)
36 self
.r
,self
.g
,self
.b
,self
.a
,self
.s
= r1
*q
+r2
*p
, g1
*q
+g2
*p
, b1
*q
+b2
*p
, a1
*q
+a2
*p
, s1
*q
+s2
*p
40 self
.vx
= math
.max (-0.2, math
.min (0.2, self
.vx
* 1.03))
44 local x
,y
= self
.x
,self
.y
45 local m
,a
= self
.maxage
,self
.age
49 draw
.rect(x
-s
, y
-s
, s
*2, s
*2, self
.r
,self
.g
,self
.b
,self
.a
)
54 for i
=#self
.list
,1,-1 do
55 local p
= self
.list
[i
]
59 if p
.think
then p
:think() end
60 if p
.age
> p
.maxage
then
61 table.remove(self
.list
,i
)
64 local now
= glfw
.GetTime()
65 if simtime
+updateinterval
<now
then
66 return self
:step(catchup
+ 1)
68 --print("catched up",catchup)
71 function particle
:draw()
72 local rect
= draw
.rect
73 for i
=#self
.list
,1,-1 do
74 local p
= self
.list
[i
]
75 if p
.draw
then p
:draw() else
76 rect(p
.x
-2, p
.y
-2, 4, 4, 1, 1, 1, 1)