import "Mode7.lib"
import "Speed.lib"
import "Keycodes.lib"

constant:
	PLAYER_IMG	0
	BULLET_IMG	1

	MAX_BULLETS 32

hidden:

set window 16, 16, 320, 240, false, 2
set redraw off

if javac() then load font 0, "courier.txt", "courier.png"

rem Mode 7 setup =====================================================
proc M7_Init width(primary), height(primary), 8.0
flags[][] = M7_LoadMap("m7_data/map.txt")
if sizeof(flags) = 0 then end
viewX# = 4.0
viewZ# =	8.0
viewY# = 0.5
proc M7_SetFog 64, 0, 0, 1.0, 4.0

rem Player setup =====================================================
load image PLAYER_IMG, "m7_data/small_ship.bmp"
set image colorkey PLAYER_IMG, 0, 128, 192
playerX# = 64.0
playerY# = 100.0
playerDX# = 0.0
playerDY# = 0.0
shootTimer = 0
rem Bullets.
load image BULLET_IMG, "m7_data/bullet.bmp"
set image colorkey BULLET_IMG, 0, 128, 192
bullets[MAX_BULLETS][4]

do
	rem Update player ===================================================

	rem Slow down.
	playerDX = playerDX*0.95
	playerDY = playerDY*0.95
	rem Speed up.
	if keydown(VK_LEFT) then playerDX = max#(playerDX - 0.25, -2.0)
	if keydown(VK_RIGHT) then playerDX = min#(playerDX + 0.25, 2.0)
	if keydown(VK_UP) then playerDY = max#(playerDY - 0.25, -3.0)
	if keydown(VK_DOWN) then playerDY = min#(playerDY + 0.25, 3.0)
	rem Move.
	playerX = playerX + playerDX
	playerY = playerY + playerDY
	rem Stay on screen.
	playerX = min#(max#(playerX, 0.0), float(width(primary) - width(PLAYER_IMG)))
	playerY = min#(max#(playerY, 0.0), float(height(primary) - height(PLAYER_IMG)))

	rem Shoot.
	shootTimer = max(shootTimer - 1, 0)
	if keydown(VK_SPACE) and shootTimer = 0
		rem Find empty bullet slot.
		for i = 0 to MAX_BULLETS - 1
			if bullets[i][0] = false then break
		next
		rem Setup bullet.
		if i < MAX_BULLETS
			shootTimer = shootTimer + 10
			bullets[i][0] = true
			bullets[i][1] = int(playerX) + width(PLAYER_IMG)
			bullets[i][2] = int(playerY) + (height(PLAYER_IMG) - height(BULLET_IMG))/2
			bullets[i][3] = 6  + int(playerDX*0.5)
		endif
	endif

	rem Update bullets ==================================================
	for i = 0 to MAX_BULLETS - 1
		if bullets[i][0] = true
			bullets[i][1] = bullets[i][1] + bullets[i][3]
			if bullets[i][1] > width(primary) then bullets[i][0] = false
		endif
	next

	rem Update background ===============================================

	rem Scroll, wrap after one unit.
	viewX = viewX + 0.01
	if viewX >= 5.0 then viewX = viewX - 1.0

	rem Draw ============================================================

	rem Clear screen and render mode 7.
	set color 64, 0, 0
	cls
	proc M7_Render viewX, viewZ, viewY, 270.0, -50, 70.0
	
	rem Draw player.
	set color 255, 255, 255
	draw image PLAYER_IMG, int(playerX), int(playerY)

	rem Draw bullets.
	for i = 0 to MAX_BULLETS - 1
		if bullets[i][0] = true
			draw image BULLET_IMG, bullets[i][1], bullets[i][2]
		endif
	next

	rem Instructions.
	set caret width(primary)/2, 4
	center "Move with arrow keys"
	center "Shoot with the space bar"
	center "Exit with Esc."

	rem Redraw and wait.
	redraw

	proc SPD_HoldFrame 60
until keydown(27) or not running()
