Skip to content

Instantly share code, notes, and snippets.

@Caaz
Created June 19, 2017 20:20
Show Gist options
  • Save Caaz/71220739d2ebb08e302714ede648ccfb to your computer and use it in GitHub Desktop.
Save Caaz/71220739d2ebb08e302714ede648ccfb to your computer and use it in GitHub Desktop.
Fancy object oriented way to load stuff into pico games
function particle_module()
local particles, particle_default =
{}, { position = {0,0}, decay = .5, color = 7, size = 4 }
return {
add = function(particle)
for k,v in pairs(particle_default) do particle[k] = particle[k] or v end
if particle.jitter then
particle.position[1] += rnd()*particle.jitter-particle.jitter/2
particle.position[2] += rnd()*particle.jitter-particle.jitter/2
end
add(particles, particle)
end,
update = function()
for particle in all(particles) do
if particle.velocity then
particle.position[1] += particle.velocity[1]
particle.position[2] += particle.velocity[2]
end
if(particle.size and (rnd() > particle.decay)) then particle.size -= 1 end
if particle.life then particle.life -= 1 end
if particle.size < 0 or (particle.life and particle.life < 0) then
del(particles, particle)
end
end
end,
draw = function()
for particle in all(particles) do
if(particle.sprites) then
for k, sprite in pairs(particle.sprites) do
spr(sprite,particle.position[1]+((k-1)*8)-4, particle.position[2]-4)
end
else
circfill(particle.position[1],particle.position[2],particle.size/2,particle.color)
end
end
end
}
end
modules = {}
function mods(action)
for _, module in pairs(modules) do
if module[action] then module[action]() end
end
end
function _init()
modules.particles = particle_module()
modules.particles.add({
position = {64,64}
})
end
function _update()
mods('update')
-- for _, m in pairs(modules) do m.update() end
end
function _draw()
cls()
mods('draw')
-- for _, m in pairs(modules) do m.draw() end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment