Created
February 15, 2019 22:17
-
-
Save Achie72/afcbe96b23959f332887f6addeae8ae3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- emitter | |
-- emitters have a name and | |
-- contain particles. | |
-- update and draw by name. | |
_e={} | |
-- create emitter ------------ | |
function e_new(target,x,y,lt,loop) | |
emitter={ | |
x=x, | |
y=y, | |
lifetime=lt, | |
p={}, | |
loop=loop | |
} | |
add(_e,emitter) | |
_debug="emitter count:"..#_e | |
return emitter | |
end | |
-- add particle to emitter --- | |
function e_add(emitter,count) | |
for i=1,count do | |
local p = { | |
x=emitter.x, | |
y=emitter.y, | |
t = 0, | |
size = 1, | |
max_size = 1, | |
lifetime=emitter.lifetime+rnd(5), | |
dy = rnd(0.6)+.5-rnd(1), | |
dx = rnd(0.2)+.5-rnd(1), | |
ddy = -0.1, | |
col = 1 | |
} | |
add(emitter.p,p) | |
end | |
end | |
function e_update(emitter) | |
for p in all(emitter.p) do | |
p.y += p.dy | |
p.x += p.dx | |
p.dy+= p.ddy | |
p.t += .5*1/p.lifetime | |
p.size = max(p.size, p.max_size * p.t ) | |
if (p.t > 0.2) p.col = 12 | |
if (p.t > 0.4) p.col = 12 | |
if (p.t > 0.6) p.col = 13 | |
if (p.t > 0.8) p.col = 5 | |
if (p.t > 1) then | |
del(emitter.p,p) | |
if (emitter.loop) then | |
e_add(target,1) | |
else | |
if (#emitter.p==0) del(_e,emitter) | |
end | |
end | |
end | |
end | |
function e_draw(emitter) | |
for p in all(emitter.p) do | |
pset(p.x, p.y, p.col) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment