Created
December 5, 2016 06:22
-
-
Save hamzamu/d357935f6ddb15fa0bd315133366d8d4 to your computer and use it in GitHub Desktop.
First attempt at a smoke particle effect in love2d.
This file contains hidden or 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
function love.load() | |
particles = {} | |
particles.clock = 0 | |
particles.acceleration = {x = love.math.random(-50, 50), y = love.math.random(-50, 50)} | |
end | |
function love.update(dt) | |
local remove = {} | |
for i, particle in ipairs(particles) do | |
particle.clock = particle.clock + dt | |
while particle.clock >= 0.01 do | |
particle.clock = particle.clock - 0.01 | |
particle.position.x = particle.position.x + particle.velocity.x * 0.01 | |
particle.position.y = particle.position.y + particle.velocity.y * 0.01 | |
particle.velocity.x = particle.velocity.x + particles.acceleration.x * 0.01 | |
particle.velocity.y = particle.velocity.y + particles.acceleration.y * 0.01 | |
particle.alive = particle.alive + 0.01 | |
if particle.alive >= 2 then | |
remove[#remove + 1] = i | |
end | |
end | |
end | |
for _, i in ipairs(remove) do | |
table.remove(particles, i) | |
end | |
particles.clock = particles.clock + dt | |
while particles.clock >= 0.01 do | |
particles.clock = particles.clock - 0.01 | |
particles[#particles + 1] = {clock = 0, alive = 0, position = {x = 0, y = 0}, velocity = {x = love.math.random(-50, 50), y = love.math.random(-50, 50)}} | |
end | |
end | |
function love.draw() | |
love.graphics.push() | |
love.graphics.translate(love.graphics.getWidth() / 2, love.graphics.getHeight() / 2) | |
for _, particle in ipairs(particles) do | |
local alpha = 1 - particle.alive / 2 | |
love.graphics.setColor(0xff * alpha, 0xff * alpha, 0xff * alpha, 0xff * alpha) | |
love.graphics.circle("fill", particle.position.x, particle.position.y, 3) | |
end | |
love.graphics.pop() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment