-- title:  untitled shmup
-- author: game developer
-- desc:   something like Gradius/Nemesis/Salamander/Life Force
-- script: lua

t=0

local bullet_speed = 6
local bullet_timer = 0
local bullet_timer_start = 10

-- props: x, y, vx, vy, type
local player_bullets = {}

local player = {
	x=20,y=60,
	vx=0,vy=0,
	speed=1.5,
	sp=256,
	fire_t=0,
	fire_held=false,
	gun_type=1,
	gun_level=1,
	gunpods={},
}

local enemies = {}

function add_enemy(en_type, xpos, ypos)
	table.insert(enemies, {
		x=xpos, y=ypos,
		type=en_type,
		t=0,
	})
end

function mod(a, b)
	return math.floor(a-(math.floor(a/b)*b))
end

-- TODO: various types
function add_bullet()
	sfx(0, 'D-6', 10, 0, 11, 4)
	table.insert(player_bullets, {
		x=player.x + 10,
		y=player.y,
		vx = 0,
		vy = 0,
	})
	if #player.gunpods > 0 then
		for _,pod in ipairs(player.gunpods) do
			table.insert(player_bullets, {
				x=pod.x + 4,
				y=pod.y,
				vx = 0,
				vy = 0,
			})
		end
	end
end

function add_gunpod()
	if #player.gunpods < 5 then
		table.insert(player.gunpods, {
			x=0,y=0,vx=0,vy=0,
			sprs={ 259,260,261,262,263 },
			fr=1,t=0,frametime=5,
		})
	end
end

function draw_player()
	local engine_spr = 274
	if player.vx > 0 then
		engine_spr = 306
	elseif player.vx < 0 then
		engine_spr = 290
	end

	if player.vy > 0 then
		player.sp = 272
	elseif player.vy < 0 then
		player.sp = 288
	else
		player.sp = 256
	end
	-- draw engine fire then player
	spr(engine_spr, player.x-8, player.y+1)
	spr(player.sp, player.x, player.y, 0, 1, 0, 0, 2, 1)

	-- draw gunpods
	if #player.gunpods > 0 then
		for key,pod in ipairs(player.gunpods) do
			pod.t = pod.t + 1
			if mod(pod.t, pod.frametime) == 0 then
				pod.fr = pod.fr + 1
				if pod.fr > #pod.sprs then pod.fr = 1 end
			end
			spr(pod.sprs[pod.fr], pod.x, pod.y)

			-- TODO: move this into an update
			local targx = player.x + 4 - pod.x
			local targy = player.y - 10 - pod.y
			pod.vx = targx / 3
			pod.vy = targy / 3
			pod.x = pod.x + pod.vx
			pod.y = pod.y + pod.vy
		end
	end
end

add_gunpod()

function draw_bullets()
	for key,val in ipairs(player_bullets) do
		spr(258, val.x, val.y, 0)
	end
end

-- also handle powerups since they're colliders
function draw_enemies()

end

function handle_input()
	player.vx = 0
	player.vy = 0
	if btn(0) then
		player.vy=-player.speed
	end
	if btn(1) then
		player.vy=player.speed
	end
	if btn(2) then
		player.vx=-player.speed
	end
	if btn(3) then
		player.vx=player.speed
	end

	-- slow down diagonal movements
	if player.vx * player.vy ~= 0 then
		player.vx = player.vx * 0.707
		player.vy = player.vy * 0.707
	end

	player.x = player.x + player.vx
	player.y = player.y + player.vy

	if player.x < 5 then player.x = 5 end
	if player.x > 220 then player.x = 220 end
	if player.y < 5 then player.y = 5 end
	if player.y > 128 then player.y = 128 end

	player.fire_held = false
	if btn(4) then
		if player.fire_t <= 0 then
			player.fire_t = bullet_timer_start
			add_bullet()
		else
			player.fire_t = player.fire_t - 1
		end
		player.fire_held = true
	else
		player.fire_t = 0
	end

	-- debug
	if btnp(5) then
		player.gun_type = mod(player.gun_type + 1, 3) + 1
	end
end

function check_aabb(x1, y1, w1, h1, x2, y2, w2, h2)
	return x1 < x2+w2 and
	x2 < x1+w1 and
	y1 < y2+h2 and
	y2 < y1+h1
end

function TIC()
	handle_input()

	for key,val in ipairs(player_bullets) do
		val.x = val.x + bullet_speed
		if val.x > 240 then
			table.remove(player_bullets, key)
		end
	end

	cls(0)
	draw_player()
	draw_bullets()


	dbg("type:"..tostring(player.gun_type), 0)
	dbg(tostring(player.fire_held), 1)
	dbg(tostring(mod(t/3, 5) + 1), 2)
	-- dbg(tostring(player.vy), 3)
	t=t+1
end

function dbg(msg, line_no)
	print(msg, 5, line_no * 7, 5, true, 1, true)
end

-- <SPRITES>
-- 000:00000000eccef0000edddefa0fcccc8bcf9deededfb8cddd00db8cdd00008cce
-- 001:000000000000000090000000aa9deecc99eedcddddcccdf0ccf0000000000000
-- 002:0000000000000000000000004040440030303300000000000000000000000000
-- 003:00dd00000eeedddd023eeee02344320023443200023eddd00feefeee00ff0000
-- 004:000000000f1eeddd02331ee023443200234eddd00feefeee00f1000000000000
-- 005:000000000022000002332eef23dd31f0231ddddd0fe1eee00022000000000000
-- 006:000000000022000002dd2000231ddddd23e1eeee02ee20000022000000000000
-- 007:0000000000de00000f1ddddd2341eee02344320002331ee00f1eeddd00000000
-- 008:000000000000000000000000aa99aabb99aabbaa000000000000000000000000
-- 016:dccefeed0ddddefa0fcccc8acf9deededfbbcddd0000bcdd00008ccc00000dd0
-- 017:d00000009eedd000aaaaeecc9aa9dccdddcccdf0ccd00000d000000000000000
-- 018:0000000000000880000889ab08899abc000889ab000008800000000000000000
-- 019:0ee000dcedce0deeecde0eaa0eeeaa98000a98880eea8888eea98888dea88888
-- 020:0ee000dcedbe0deeebde0eaa0eeeaa98000a98880eea8888eea98888dea88888
-- 021:0ee000dcedce0deeecde0e550eee44ff0004ffff0ee4ffffee5fffffde5fffff
-- 022:0022220002223320122344321223443212223322112222210112221000111100
-- 023:00aaaa000aaabb609aabccba9aabccba9aaabbaa99aaaaa9099aaa9000999900
-- 024:0066660006665560766544567665445676665566776666670776667000777700
-- 025:00bccb000ab00ba00ab000000abccb0000bccba000000ba00ab00ba000bccb00
-- 026:0000000023200232233cc332233cc332230cc032230000322300003200000000
-- 032:ecdf00000edddf0000edddc00bbcfeeddcc99feeee8888880bc99f99000bfeee
-- 033:000000000000000009aa0000cdc99e00eeedddcc88888eef9eeeedd0df000000
-- 034:00000000000000000000089a000089ab0000089a000000000000000000000000
-- 035:0ee000dcedce0deeecde0e330eee3321000321110ee31111ee321111de311111
-- 036:0ee000dced3e0deee3de0e330eee3321000321110ee31111ee321111de311111
-- 037:00cccc000cccccc0c000000c0000000000000000000000000000000000000000
-- 038:000000000000000000ccccc00ccccccc0ccccccc00ccccc00000000000000000
-- 039:00000000000000000000000000000000c000000c0cccccc000cccc0000000000
-- 050:00000000000099a000889abb8889accc00889abb000099a00000000000000000
-- </SPRITES>

-- <WAVES>
-- 000:123345667889abbcdeff1fedcba86642
-- </WAVES>

-- <SFX>
-- 000:00d050808060c040d020e020f010f010f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000f000502000000000
-- </SFX>

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

