'Endless Tunnel Race Game Sample
'Originally Created by 29 games
'Ported over to Basic2AGKTier2
'*******************
'All Credit goes to 29 games for the original
'source code.  This is being used only a demo
'of how easy it is to port AGK Tier 1 games
'to Tier2 using Basic2AGKTier2.  It also 
'show the slight differences.

Raw As float max_speed = 5.0f
Raw As float acceleration = 0.005f
Raw As float turn_speed = 4.0f

Raw As integer game_state = 0
Raw As float speed = 0.0f
Raw As integer score = 0
Raw As integer high_score = 0

Raw As integer input_method

Raw As integer screen_left
Raw As integer screen_right
Raw As integer screen_top

Sub AppBegin()
	SetFolder("/media")
	'initial set up
	Call _screen_setup
	Call _set_input_method
	Call _load_media
	game_state=0
End Sub

Function AppLoop()
	If GetRawKeyPressed(27) Then End
	
	Select Case game_state
		Case 0
			Call _initiate
		Case 1
			Call _start
		Case 2
			Call _play_game
		Case 3
			Call _game_over
	End Select
	
	Sync()
	Function = 0
End Function

Sub AppEnd()
	
End Sub

Sub _screen_setup()
	SetWindowTitle("Tunnel Rush")
	SetWindowSize(1280,720,0)
	
	SetVirtualResolution(1280,720)
	SetOrientationAllowed(0,0,1,0)
	SetSyncRate(30,0)
	
	SetScissor(0,0,0,0)
	
	screen_left = GetScreenBoundsLeft()
	screen_right = GetScreenBoundsRight()
	screen_top = GetScreenBoundsTop()
	
	SetClearColor(255,255,0)
End Sub

Sub _set_input_method()
	CompleteRawJoystickDetection()
	If GetJoyStickExists() = 1 Then
		input_method = 3
	Else
		If GetKeyBoardExists() = 1 Then
			input_method = 1
		Else
			If GetMultiTouchExists() = 1 Then
				input_method = 2
			EndIf
		EndIf
	EndIf
End Sub

Sub _load_media()
	LoadImage(1,"tunnel_1.png")
	LoadImage(2,"block_1.png")
	For integer i = 1 to 5
		LoadObject(i, "tunnel_1.obj")
		SetObjectImage(i,1,0)
		SetObjectFogMode(i,1)
		CreateObjectBox(i+10,20,20,1)
		SetObjectImage(i+10,2,0)
		SetObjectFogMode(i+10,1)
		SetObjectCollisionMode(i+10,1)
	Next
	SetFogMode(1)
	SetFogColor(255,255,0)
	SetFogRange(250,400)
	SetSunActive(1)
	SetAmbientColor(150,150,150)
	SetTextDefaultFontImage(LoadImage("font_002.png"))
	for integer i = 1 to 4
		CreateText(i,"")
		SetTextSize(i,50)
		SetTextColor(i,0,0,0,255)
	Next
	SetTextPosition(1,640,300)
	SetTextAlignment(1,1)
	SetTextPosition(2,640,380)
	SetTextAlignment(2,1)
	SetTextPosition(3,screen_left+5,screen_top+5)
	SetTextString(3,Str(score))
	SetTextPosition(4,screen_right-5,screen_top+5)
	SetTextAlignment(4,2)
	SetTextString(4,Str(high_score))
	LoadMusicOgg(1,"music.ogg")
	PlayMusicOgg(1,1)
End Sub

Sub _initiate()
	for integer i = 1 to 5
		SetObjectPosition(i,0,0,(i-1)*100)
		SetObjectRotation(i,0,0,0)
		SetObjectPosition(i+10,0,0,GetObjectZ(i))
		SetObjectRotation(i+10,0,0,GetObjectAngleZ(i))
		RotateObjectLocalZ(i+10,Random(0,7)*45)
		MoveObjectLocalY(i+10,10)
	Next
	SetCameraPosition(1,0,-8,0)
	SetCameraRotation(1,0,0,0)
	game_state = 1
End Sub

Sub _start()
	Select Case input_method
		Case 1
			SetTextString(1,"left and right key to move")
			SetTextString(2,"press left or right key to start")
			If GetRawKeyState(37) = 1 or GetRawKeyState(39) = 1 Then
				PlayMusicOgg(1,1)
				game_state = 2
			EndIf
			
		Case 2
			SetTextString(1,"touch left or right of screen to move")
			SetTextString(2,"touch left or right of screen to start")
			Dim Raw touch = 0
			touch = GetRawFirstTouchEvent(1)
			Dim Raw touch_x = GetRawTouchCurrentX(touch)
			If touch > 0 Then
				If touch_x < screen_left + 300 or touch_x > screen_right - 300 Then
					game_state = 2
				EndIf
			EndIf
			
		Case 3
			SetTextString(1,"left joystick to move")
			SetTextString(2,"move left joystick to start")
			If Abs(GetRawJoystickX(1)) > 0.1 Then
				game_state = 2
			EndIf
	End Select
	
	If game_state = 2 Then
		SetTextVisible(1,0)
		SetTextVisible(2,0)
		score = 0
		speed! = 0.1
	EndIf
End Sub

Sub _play_game()
	Incr score,10
	Dim Raw rotate!
	
	Select Case input_method
		Case 1
			rotate! = (GetRawKeyState(37)-GetRawKeyState(39))*turn_speed!
		Case 2
			rotate! = 0.0
			Dim Raw touch = 0
			touch = GetRawFirstTouchEvent(1)
			Dim Raw touch_x = GetRawTouchCurrentX(touch)
			If touch > 0 Then
				If touch_x < screen_left + 300 Then
					rotate! = turn_speed!
				EndIf
				if touch_x > screen_right - 300 Then
					rotate! = -1*turn_speed!
				EndIf
			EndIf
		Case 3
			rotate! = -1*GetRawJoystickX(1)*turn_speed#
	End Select
	
	speed! = speed! + acceleration!
	
	If speed! > max_speed! Then
		speed! = max_speed!
	EndIf
	
	for integer i = 1 to 5
		MoveObjectLocalZ(i,-speed!)
		RotateObjectLocalZ(i,rotate!)
		Dim Raw z_pos! = GetObjectZ(i)
		SetObjectPosition(i+10,0,0,z_pos!)
		RotateObjectLocalZ(i+10,rotate!)
		MoveObjectLocalY(i+10,10.0)
		If z_pos! < -100.0 Then
			SetObjectPosition(i,0,0,z_pos!+500)
			SetObjectPosition(i+10,0,0,z_pos!+500)
			RotateObjectLocalZ(i+10,Random(0,7)*45)
			MoveObjectLocalY(i+10,10.0)
		EndIf
	Next
	SetTextString(3,Str(score))
	If ObjectSphereCast(0,0,-8,0,0,-8,max_speed!,0.25)>0 Then
		game_state = 3
		If score> high_score Then
			high_score = score
			SetTextString(1,"!! new high score !!")
			SetTextString(4,Str(high_score))
		Else
			SetTextString(1,"! crashed !")
		EndIf
	EndIf
End Sub

Sub _game_over()
	SetTextVisible(1,1)
	SetTextVisible(2,1)
	Select Case input_method
		Case 1
			SetTextString(2,"press space to replay")
			If GetRawKeyPressed(32) = 1 Then
				game_state = 0
			EndIf
		Case 2
			SetTextString(2,"touch centre of screen to replay")
			Dim Raw touch = 0
			touch = GetRawFirstTouchEvent(1)
			Dim Raw touch_x = GetRawTouchCurrentX(touch)
			If touch > 0 Then
				if touch_x > screen_left + 400 and touch_x < screen_right - 400 Then
					game_state = 0
				EndIf
			EndIf
		Case 3
			SetTextString(2,"press start to replay")
			If GetRawJoystickButtonPressed(1,8) = 1 Then
				game_state = 0
			EndIf
	End Select
	
	For integer i = 1 to 5
		MoveObjectLocalZ(i,0.5)
		RotateObjectLocalZ(i,1.0)
		Dim Raw z_pos! = GetObjectZ(i)
		SetObjectPosition(i+10,0,0,z_pos!)
		RotateObjectLocalZ(i+10,1.0)
		MoveObjectLocalY(i+10,10.0)
		If z_pos! > 400.0 Then
			SetObjectPosition(i,0,0,z_pos!-500)
			SetObjectPosition(i+10,0,0,z_pos!-500)
			MoveObjectLocalY(i+10,10.0)
		EndIf
	Next
End Sub
