// File: finish.agc
// Created: 20-08-14


function		SelectTrack(env ref as tEnvironment)
	bg 		as integer
	maps	as integer[]
	buttons	as integer[]
	open	as integer[]
	i 		as integer
	id 		as integer	
	name	as string
	start	as integer
	pos 	as integer[4,2]
	cursor  as integer
	current as integer
	button	as integer
	
	pos.fromJSON("[[17, 32], [47, 32], [17, 53], [47, 53]]")
	
	for i=1 to 10
		name = "level"+str(i)+"/level"+str(i)+".png"
		if GetFileExists(name)
			id = CreateSprite(LoadImage(name))
			SetSpriteVisible(id, 0)
			SetSpriteDepth(id, 80)
			SetSpriteShapeBox( id, 0, 0, 0, 0, 0 ) 
			SetSpriteTransparency(id, 1)
			maps.insert(id)
			if GetFileExists("level"+str(i)+"/open")
				open.insert(1)
			else
				open.insert(0)
				SetSpriteColorAlpha(id, 0x60)
			endif
		else
			exit
		endif
	next

	for i = 0 to 3	
		id = 8000+i
		CreateSprite(id, 0)
		SetSpriteSize(id, 26*(env.scale/64), 20*(env.scale/64))
		SetSpriteDepth(id, 1)
		SetSpritePositionByOffset(id, pos[i,0]*(env.scale/64), pos[i,1]*(env.scale/64))
		buttons.insert(id)
		SetSpriteVisible(id, 0)
	next

	cursor = CreateSprite(LoadImage("texture/cursor.png"))
	current = 0
	SetSpritePositionByOffset(cursor, pos[current,0], pos[current,1])
	
	SetSpriteVisible(env.loading, 0)
	SetSpriteVisible(env.canvas, 0)
	SetSpriteVisible(env.place, 0)
	SetTextVisible(env.place, 0)	
	SetTextVisible(env.lap, 0)	

	bg = CreateSprite(LoadImage("texture/select.png"))
	SetSpriteDepth(bg, 90)
			
	SetGlobal3DDepth(50)	
	
	DisplayPage(start, maps, pos)
	
	Transition(env)
		
	SetMusicVolumeOGG(env.theme, 10)
	env.level = 0
	
	do	
		SetSpritePositionByOffset(cursor, pos[current,0], pos[current,1])
		
		button = AnyJoystickButton(FALSE)
		
		if GetRawKeyPressed(KEY_DOWN) or button = JOYSTICK_DPAD_DOWN
			if current < 2
				PlaySound(env.coin.sound)													
				inc current, 2	
			endif
		endif
		if GetRawKeyPressed(KEY_UP) or button = JOYSTICK_DPAD_UP
			if current > 1
				PlaySound(env.coin.sound)													
				dec current, 2	
			endif
		endif
		if GetRawKeyPressed(KEY_RIGHT) or button = JOYSTICK_DPAD_RIGHT
			if current < 3
				PlaySound(env.coin.sound)									
				inc current,1
			endif
		endif
		if GetRawKeyPressed(KEY_LEFT) or button = JOYSTICK_DPAD_LEFT
			if current > 0
				PlaySound(env.coin.sound)													
				dec current,1
			endif
		endif
		
		if GetRawKeyPressed(KEY_ENTER) or button = JOYSTICK_START or button = JOYSTICK_A or button = JOYSTICK_B or button = JOYSTICK_X or button = JOYSTICK_Y  or GetRawKeyPressed(KEY_SPACE) 
			if current <= maps.length
				if open[current]
					env.level = current+1
					gGameState = GAMESTATE_PLAY		
					PlaySound(env.coin.sound)													
					exit
				endif
			endif
		endif			
		
		if GetPointerPressed()
			for i=0 to buttons.length
				id = GetSpriteHitTest(buttons[i], ScreenToWorldX(GetPointerX()), ScreenToWorldY(GetPointerY()))
				if id
					if i <= maps.length
						if open[i]
							env.level = i+1
							current = i
							gGameState = GAMESTATE_PLAY
							PlaySound(env.coin.sound)															
							exit		
						endif
					endif
				endif
			next
			if env.level > 0
				exit
			endif
		endif

		if GetRawKeyPressed(KEY_ESCAPE) or GetJoystickBack()
			gGameState = GAMESTATE_SPLASH
			exit
		endif

		LowRez(env)		
		sync()
	loop
	
	SetSpritePositionByOffset(cursor, pos[current,0], pos[current,1])		
	LowRez(env)		
	sync()

			
	PlaySound(env.coin.sound)
	
	for i=0 to maps.length
		DestroySprite(maps[i])
	next

	for i=0 to buttons.length
		DestroySprite(buttons[i])
	next
	DestroySprite(bg)
	DestroySprite(cursor)	
	SetMusicVolumeOGG(env.theme, 30)

endfunction

function DisplayPage(start as integer, maps as integer[], pos as integer[][])
	i		as integer
	
	for i=0 to maps.length
		SetSpriteVisible(maps[i], 0)
	next
	
	for i=start to start+3
		if i > maps.length
			exit
		endif
		SetSpriteVisible(maps[i], 1)
		SetSpritePositionByOffset(maps[i], pos[i-start, 0],  pos[i-start, 1])
	next
	
endfunction