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


function		GameFinish(env ref as tEnvironment)
	cup as integer
	flag as integer
	place as integer
	text as string
	rays as integer
	rays1 as integer	
	img as integer
	id as integer
	
	s as integer
	
	SetMusicVolumeOGG(env.theme, 0)
	
	SetSpriteVisible(env.loading, 0)
	SetSpriteVisible(env.pickups, 0)	
	SetSpriteVisible(env.canvas, 0)
	//SetSpriteVisible(env.place, 0)
	SetTextVisible(env.place, 0)	
	SetTextVisible(env.lap, 0)	

	if env.position = 0
		env.position = 1
	endif
	
	if env.position > 3
		s = LoadSound("sounds/lose.wav")
		img = LoadImage("texture/poop.png")
		cup = LoadObject("global/poop.3ds")
	else
		s = LoadSound("sounds/finish.wav")
		img = LoadImage("texture/cup.png")
		cup = LoadObject("global/cup.3ds")

	endif  
	
	if env.position = 1
		id = OpenToWrite("level"+str(env.level+1)+"/open")
		CloseFile(id)
	endif
	
	SetObjectImage(cup, img, 0)	
	
	SetObjectScalePermanent(cup, 2.5, 2.5, 2.5)
	SetObjectPosition(cup, 0,0, 0)
	SetCameraPosition(1, 0, 5, -10)
	SetCameraLookAt(1, 0, 2, 0, 0)
	SetObjectLightMode(cup, 0)

	
	select env.position
		case 1
			text = "1st"
			//#FF9900
			SetObjectColor(cup, 0xff, 0x99, 0x00, 0xff)
			SetObjectColorEmissive(cup, 0x80, 0x64, 0x00)
		endcase
		case 2
			text = "2nd"
			//#FF9900
			SetObjectColor(cup, 0xb0, 0xb0, 0xf0, 0xff)
			SetObjectColorEmissive(cup, 0x50, 0x50, 0x70)
		endcase
		case 3
			text = "3rd"
			// #E38902 cd7f32
			SetObjectColor(cup, 0xcd, 0x7f, 0x32, 0xff)
			SetObjectColorEmissive(cup, 0x60, 0x34, 0x15)
		endcase
		case default
			// #6B3925
			SetObjectColor(cup, 0x6b, 0x39, 0x25, 0x40)
			text = str(env.position)+"th"
		endcase
	endselect
		
	place = CreateText(text)
	SetTextFont(place, env.fonts[0])
	SetTextSize(place, 10)
	SetTextDepth(place, 40)
	//SetTextAlignment(place, 1)
	SetTextColor(place, 0x99, 0, 0, 0xff)
	
	f as float 
	f = (64 - (GetTextTotalWidth(place)+GetSpriteWidth(env.place)+2)) / 2
	SetTextPosition(place, f + GetSpriteWidth(env.place) + 1, 8)
	
	SetSpriteDepth(env.place, 44)
	SetSpritePosition(env.place, f - 1, 9)
		
	flag = CreateSprite(LoadImage("texture/finish.png"))
	SetSpriteDepth(flag, 90)
	
	rays = CreateSprite(LoadImage("texture/rays.png"))
	SetSpriteTransparency(rays, 1)
	SetSpriteDepth(rays, 88)
	SetSpritePosition(rays, 0, 12)
	SetSpriteColorAlpha(rays, 0xd0)
	
	rays1 = CreateSprite(LoadImage("texture/rays1.png"))
	SetSpriteTransparency(rays1, 1)
	SetSpriteDepth(rays1, 87)
	SetSpritePosition(rays1, 0, 12)
	setspriteangle(rays1, 15)
	SetSpriteColorAlpha(rays1, 0xd0)
	//SetSpriteColor(rays1, GetObjectColorRed(cup),  GetObjectColorGreen(cup),  GetObjectColorBlue(cup), 0xff)
		
	if env.position > 3
		SetSpriteVisible(rays, 0)
		SetSpriteVisible(rays1, 0)
	endif
			
	SetGlobal3DDepth(50)	
	
	Transition(env)
	
	SetMusicVolumeOGG(env.theme, 10)
	PlaySound(s)
		
	repeat
		SetSpriteAngle(rays, GetSpriteAngle(rays) + 0.2)
		SetSpriteAngle(rays1, GetSpriteAngle(rays1) + 0.4)		
		RotateObjectLocalY(cup, 1)
		LowRez(env)
		sync()
	until GetRawKeyPressed(KEY_ESCAPE) or GetJoystickBack()
	
	gGameState = GAMESTATE_SELECT
	
	DestroySprite(flag)
	DestroySprite(rays)
	DestroySprite(rays1)	
	DeleteText(place)
	DeleteObject(cup)
	DeleteImage(img)
	env.position = 0
	SetMusicVolumeOGG(env.theme, 30)
	DeleteSound(s)
endfunction
