import "TD.lib"
import "Speed.lib"
import "Keycodes.lib"

rem Double buffer.
set redraw off

rem Init gl window.
if not TD_Init("TD Example", 200, 20, 800, 600, 65.0, 0.1, 16.0, true)
	end
endif

rem Load a background.
if not TD_LoadBackground("assets_rc/sky.jpg") then end
rem Load map.
if not TD_LoadMap("assets_rc/test.bin") then end
rem Set fog.
proc TD_SetFog 0, 0, 0, 0.5, 10.0

playerX# = 9.0
playerY# = 0.0
playerZ# = 8.0
playerDirection# = 180.0
playerPitch# = 0.0
playerRoll# = 0.0
playerBobAngle# = 0.0
shouldDraw = true

do
	rem Need to call this.
	proc TD_Update

	rem Update player.
	plyDY# = -0.05
	plyDX# = 0.0
	plyDZ# = 0.0
	playerRoll = 0.95*playerRoll

	rem Look up/down with page up/down.
	if keydown(104)
		playerPitch = max#(playerPitch - 2.0, -30.0)
	endif
	if keydown(105)
		playerPitch = min#(playerPitch + 2.0, 45.0)
	endif
	rem Walk and turn.
	if keydown(VK_UP)
		plyDX = cos(playerDirection)*0.03
		plyDZ = sin(playerDirection)*0.03
		playerBobAngle = playerBobAngle + 10.0
	endif
	if keydown(VK_DOWN)
		plyDX = -cos(playerDirection)*0.03
		plyDZ = -sin(playerDirection)*0.03
		playerBobAngle = playerBobAngle - 10.0
	endif
	if keydown(VK_LEFT)
		playerDirection = playerDirection - 2.5
		playerRoll = min#(playerRoll + 0.5, 10.0)
	endif
	if keydown(VK_RIGHT)
		playerDirection = playerDirection + 2.5	
		playerRoll = max#(playerRoll - 0.5, -10.0)
	endif
	proc TD_Move playerX, playerY, playerZ, plyDX, plyDY, plyDZ, 0.3, 1.0, 0.2


	rem Draw?
	if shouldDraw
		proc TD_SetCamPosition playerX, playerY + 0.6 + sin(playerBobAngle)*0.075, playerZ
		proc TD_SetCamOrientation playerDirection, playerPitch, playerRoll
		proc TD_Render
	endif
	shouldDraw = SPD_HoldFrameDraw(60)
until not TD_Running()
