-- title:  atmos
-- author: pixelbath
-- desc:   some atmospheric-type particle effects

-- PARTICLE/VFX STUFF
particletype = 'rain' -- snow|rain|stars
particles = {}
local starcolors = { 9, 10, 11, 12}
t = 0

function particleinit(numparticles)
    particles = {}
    if particletype == 'stars' then
        for i = 0, numparticles do
            table.insert(particles, {
                x = math.random() * 240,
                y = math.random() * 136,
                twinkle = math.random() > 0.49, -- randomly pick true/false
                color = starcolors[math.random(#starcolors)],
                t = 0,
                vel = 0,
            })
        end
	end
	if particletype == 'rain' or particletype == 'snow' then
		for i = 0, numparticles do
            table.insert(particles, {
                x = math.random() * 240,
				y = math.random() * 136,
				lastx = 0,
				lasty = -3,
				t = 0,
				vel = (math.random() * 3) + 1,
			})
        end
	end
end

function particleupdate()
    if particletype == 'stars' then
        for i = 1,#particles do
            local p = particles[i]
            if not p.twinkle then goto continueTwinkle end
            ::continueTwinkle::
            if t % 15 == 0 and math.random() > 0.1 then
                p.color = starcolors[math.random(#starcolors)]
            end
        end
    end

    if particletype == 'rain' then
        for i = 1,#particles do
            local p = particles[i]
                if p.lasty and p.lasty > 136 then
                    p.y = 0
                    p.lasty = 0
                    p.color = 9
                    p.x = math.random() * 240
            end
            p.lasty = p.y
            p.lastx = p.x
            p.y = p.y + p.vel
        end
    end
	
	if particletype == 'snow' then
		for i = 1,#particles do
			local p = particles[i]
			if p.y and p.y > 136 then
				p.y = 0
				p.lasty = 0
                p.color = 12
				p.x = math.random() * 240
			end
            p.x = p.x - (math.random() - 0.4)
            p.y = p.y + p.vel
        end
    end
end

function particledraw()
	if particletype == 'rain' then
		for i = 1,#particles do
			local p = particles[i]
			line(p.x, p.y, p.lastx, p.lasty, p.color)
		end
    else
        for i = 1,#particles do
            local p = particles[i]
            pix(p.x, p.y, p.color)
        end
	end	
end

particleinit(100)

function TIC()
    if btnp(4) then
        particletype='rain'
        particleinit(100)
    end
    if btnp(5) then
        particletype='stars'
        particleinit(80)
    end
    if btnp(6) then
        particletype='snow'
        particleinit(100)
    end

    cls(0)
    particleupdate()
    particledraw()
    t = t + 1
end


-- <PALETTE>
-- 000:1a1c2c5d275db13e53ef7d57ffcd75a7f07038b76425717929366f3b5dc941a6f673eff7f4f4f494b0c2566c86333c57
-- </PALETTE>
