-- title:  fireworks test
-- author: pixelbath
-- desc:   description
-- script: lua

t=0
colors = {
	{3,2,1},
	{5,6,7},
	{11,10,9},
	{15,14,13},
	{12,4,2}, -- fuse colors
}

seeds = {}

flashy_color = 1

fps = {
	last_time = time(),
	frames = 0,
	val = 0,
}
-- basic todo:
--	seed
--		explosion shape

-- types:
--		willow - each star trails particles with low inertia
--		grains of wheat / candles - shooting upward, possibly at angles
--		rainbow chrysanthemums - red/white/blue spherical burst, ends with trail of sparks
--		orange peony - similar to mums, but no trail of sparks
--		peacock feather / dragonfly - lots of blue/orange with long tails
--		green bees - spherical burst, curving trajectories with long green tails
--		crossette - multiple large stars that burst into smaller stars with a loud crackling
--		falling leaves - stars twinkle and flutter down

function create_star(xpos, ypos, xvel, yvel, colorgroup)
	local group = colorgroup or math.random(3)+1
	local new_star = {
		vel_x = xvel,
		vel_y = yvel,
		color = colors[group][1],
		color_group = group,
		x = xpos, y = ypos,
		num_children = 0,
		t = 0, det = 200,
		shape = nil,
		is_seed = false,
		is_spark = false,
		has_trail = false,
	}
	return new_star
end

function fireworks_update()
	local delete_seeds = {}
	for i = 1, #seeds do
		local this_seed = seeds[i]
		local will_delete = false
		
		-- friction and gravity
		this_seed.vel_y = this_seed.vel_y + 0.011
		this_seed.x = this_seed.x + (this_seed.vel_x * 0.9)
		this_seed.y = this_seed.y + (this_seed.vel_y * 0.9)

		-- ways to remove particles
		if this_seed.y > 136 then
			will_delete = true
		end
		if this_seed.det > 0 and this_seed.t > this_seed.det then
			spawn_explosion(this_seed)
			will_delete = true
		end
		if this_seed.is_seed then
			if this_seed.vel_y > -0.1 and math.random() > 10 * math.abs(this_seed.vel_y) then
				spawn_explosion(this_seed)
				will_delete = true
			end
		end

		-- spawn a trail
		if this_seed.is_seed then
			local spark = create_star(this_seed.x, this_seed.y, math.random() * 0.5 - 0.25, math.random() * 0.5 - 0.25, 5)
			spark.det = 12
			spark.is_spark = true
			table.insert(seeds, spark)
		end

		if this_seed.has_trail then
			local spark = create_star(this_seed.x, this_seed.y, math.random() * 0.5 - 0.25, math.random() * 0.5 - 0.25, this_seed.color_group)
			spark.det = 6
			table.insert(seeds, spark)
		end

		-- color updates
		if this_seed.det > 0 then
			local perc = this_seed.t / this_seed.det
			if perc > 0.3 then this_seed.color = colors[this_seed.color_group][2] end
			if perc > 0.6 then this_seed.color = colors[this_seed.color_group][3] end
		end

		-- draw this guy
		if not this_seed.is_seed then pix(this_seed.x, this_seed.y, this_seed.color) end

		this_seed.t = this_seed.t + 1
		if will_delete then table.insert(delete_seeds, i) end
	end

	for i = #delete_seeds, 1, -1 do
		table.remove(seeds, delete_seeds[i])
	end
end

function spawn_explosion(star)
	if star.num_children == 0 then return end
	if star.shape == nil then star.shape = 'peony' end
	
	if star.shape == 'peony' then
		local angle = 2 * math.pi / star.num_children
		for i = 1, star.num_children do
			local xvel = math.cos(angle * i) * ((math.random() * 0.7) + 0.05)
			local yvel = math.sin(angle * i) * ((math.random() * 0.7) + 0.05)
			local child_star = create_star(star.x, star.y, xvel, yvel, star.color_group)
			child_star.det = math.random() * 30 + 50
			child_star.has_trail = true
			table.insert(seeds, child_star)
		end
	end
end

function TIC()
	if btnp(4) then
		local seed_yvel = -1 + (math.random() * -1)
		local seed = create_star(math.random(240), 136, 0, seed_yvel)
		seed.shape = 'peony'
		seed.num_children = 150
		seed.is_seed = true
		table.insert(seeds, seed)
	end

	cls(0)
	fireworks_update()

	if t % 3 == 0 then flashy_color = math.floor(math.random(14)) + 1 end

	print("WELL DONE!", 240/2-35, 10, 12, false, 1, false)
	print("Time: 30:00", 240/2-26, 28, 12, false, 1, true)
	print("Deaths: 10", 240/2-26, 36, 12, false, 1, true)
	print("Cat memes: 25/25", 240/2-38, 44, 12, false, 1, true)

	print("More motivational text!", 240/2-48, 60, flashy_color, false, 1, true)

	print("Press A to continue", 240/2-42, 90, 12, false, 1, true)
	print("Press B to go to the main menu", 240/2-58, 98, 12, false, 1, true)

	local fps_val = 0
	if (time()-fps.last_time <= 1000) then
		fps.frames=fps.frames+1
	else
		fps.val=fps.frames
		fps.frames = 0
		fps.last_time = time()
	end

	print("fps: "..fps.val..", particles: "..#seeds, 2, 128, 12, false, 1, true)
	t=t+1
end

-- <WAVES>
-- 000:00000000ffffffff00000000ffffffff
-- 001:0123456789abcdeffedcba9876543210
-- 002:0123456789abcdef0123456789abcdef
-- </WAVES>

-- <SFX>
-- 000:000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000304000000000
-- </SFX>

-- <PALETTE>
-- 000:000004610018da3038fa484ceec2612cce50108540083c2404366f042c79045dda1085fff4f4f42c0038690071a508b2
-- </PALETTE>

