rem ThreeDee library test, loading a raycaster map.
rem By Marcus Johansson.

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

visible:
	rem Player.
	vPlayerX#
	vPlayerY#
	vPlayerZ#
	plyDX#
	plyDZ#
	plyDY#
	vPlayerDirection#
	vPlayerPitch#
	vPlayerRoll#
	vPlayerBobAngle#

hidden:

rem Init library, window and view.
if not TD_Init("ThreeDee Test", 16, 16, 1024, 600, 60.0, 0.1, 16.0, false)
	wln "ERROR: Could not init TD library!"
	wln "Press any key to exit ..."
	wait keydown
	end
endif
wln "Initialized TD library."

rem Load RC map, but don't build yet.
if not TD_LoadRCMap("assets_rc/map4_1.txt", vPlayerX, vPlayerZ, vPlayerDirection, false)
	wln "ERROR: Could not load RC map!"
	wln "Press any key to exit ..."
	wait keydown
	end
endif
rem Change brightness for walls.
proc TD_SetWallmapColor 2, 128, 128, 128
proc TD_SetWallmapColor 3, 128, 128, 128
rem Adjust height a little.
for z = 6 to 9
	for x = 4 to 10
		proc TD_PullCeiling x, z, 2.0
		proc TD_PullFloor x, z, -0.5
	next
next
proc TD_PullFloor 12, 2, 0.5
proc TD_PullCeiling 12, 2, 2.0
proc TD_PullFloor 2, 13, 0.5
proc TD_PullCeiling 2, 13, 2.0

proc TD_BuildMap


proc TD_SetFog 64, 64, 64, 0.5, 10.0
wln "RC map loaded."
wln
wln "Controls"
wln "  Move with arrow keys."
wln "  Look around with mouse."
wln "  Hover up with mouse button."
wln "  Exit with Esc"

rem Set player's initial position.
shouldDraw = true
while TD_Running() and not keydown(VK_ESC, true)
	rem Need to call this once per frame.
	proc TD_Update

	rem Update player.
	proc UpdatePlayer

	rem Draw?
	if shouldDraw
		proc TD_SetCamPosition vPlayerX, vPlayerY + 0.6 + sin(vPlayerBobAngle)*0.075, vPlayerZ
		proc TD_SetCamOrientation vPlayerDirection, vPlayerPitch, vPlayerRoll
		proc TD_Render
	endif

	shouldDraw = SPD_HoldFrameDraw(60)
wend

rem Update player.
procedure UpdatePlayer()
	plyDY = max#(plyDY - 0.01, -0.1)
	plyDX#
	plyDZ
	vPlayerRoll = 0.95*vPlayerRoll
	
	rem Walk and strafe.
	if keydown(VK_UP)
		vPlayerX = vPlayerX
		vPlayerZ = vPlayerZ
		plyDX = cos(vPlayerDirection)*0.05
		plyDZ = sin(vPlayerDirection)*0.05
		vPlayerBobAngle = vPlayerBobAngle + 10.0
	endif
	if keydown(VK_DOWN)
		vPlayerX = vPlayerX
		vPlayerZ = vPlayerZ
		plyDX = -cos(vPlayerDirection)*0.05
		plyDZ = -sin(vPlayerDirection)*0.05
		vPlayerBobAngle = vPlayerBobAngle - 10.0
	endif
	if keydown(VK_LEFT)
		vPlayerX = vPlayerX
		vPlayerZ = vPlayerZ
		plyDX = cos(vPlayerDirection - 90.0)*0.05
		plyDZ = sin(vPlayerDirection - 90.0)*0.05
		vPlayerRoll = min#(vPlayerRoll + 0.5, 5.0)
	endif
	if keydown(VK_RIGHT)
		vPlayerX = vPlayerX
		vPlayerZ = vPlayerZ
		plyDX = cos(vPlayerDirection + 90.0)*0.05
		plyDZ = sin(vPlayerDirection + 90.0)*0.05
		vPlayerRoll = max#(vPlayerRoll - 0.5, -5.0)

	endif
	proc TD_Move vPlayerX, vPlayerY, vPlayerZ, plyDX, plyDY, plyDZ, 0.3, 1.0, 0.2

	rem Look around with mouse.
	w = TD_GetWidth()
	h = TD_GetHeight()
	dx = mousex() - w/2
	dy = mousey() - h/2
	if dx < -32
		dxf# = float(dx + 32)/float(w/2 - 32)
		vPlayerDirection = vPlayerDirection + dxf*4.0		
	elseif dx > 64
		dxf# = float(dx - 32)/float(w/2 - 32)
		vPlayerDirection = vPlayerDirection + dxf*4.0		
	endif

	vPlayerPitch = 20.0*float(dy)/float(h/2 - 64)
endproc
