--[[
End of Quest completion sequence
--]]

function SCENEQUESTCOMPLETE_PlayCongratulationParticle(aY)
	local congratulationsTitle = scene:child("CONGRATULATIONS_TITLE")
	if congratulationsTitle ~= nil then
		local particle = scene:newChildSprite({class = "ParticleEffect", x = congratulationsTitle:x(), y = aY, library = "PARTICLE_LEVEL_COMPLETE", section = "default", name = "CongratulationsParticle", play = true})	
		particle:moveBehind(congratulationsTitle)
		particle:play()
	end
end

function SCENEQUESTCOMPLETE_FadeInText()
	local playerName = scene:child("PLAYER_NAME")
	local beatGameText = scene:child("BEAT_GAME_TEXT")
	
	playerName:animate({alpha=1.0, duration=1000})
	beatGameText:animate({alpha=1.0, duration=1000})
end

function SCENEQUESTCOMPLETE_StartSequence(aAvatarEndY) 
	
	local avatar = scene:child("AVATAR")
	local congratulationsTitle = scene:child("CONGRATULATIONS_TITLE")
	local playerName = scene:child("PLAYER_NAME")
	local beatGameText = scene:child("BEAT_GAME_TEXT")
	
	local firework1 = scene:newChildSprite({class = "ParticleEffect", x = 192, y = 265, library = "PARTICLE_FIREWORKS_01", section = "fireworks", name = "Firework1", play = false})	
	local firework2 = scene:newChildSprite({class = "ParticleEffect", x = 760, y = 90, library = "PARTICLE_FIREWORKS_02", section = "fireworks", name = "Firework2", play = false})	
	local firework3 = scene:newChildSprite({class = "ParticleEffect", x = 340, y = 101, library = "PARTICLE_FIREWORKS_03", section = "fireworks", name = "Firework3", play = false})	
	local firework4 = scene:newChildSprite({class = "ParticleEffect", x = 728, y = 240, library = "PARTICLE_FIREWORKS_01", section = "fireworks", name = "Firework4", play = false})				
	local firework5 = scene:newChildSprite({class = "ParticleEffect", x = 435, y = 155, library = "PARTICLE_FIREWORKS_03", section = "fireworks", name = "Firework5", play = false})	
	local firework6 = scene:newChildSprite({class = "ParticleEffect", x = 460, y = 195, library = "PARTICLE_FIREWORKS_02", section = "fireworks", name = "Firework6", play = false})	
	local firework7 = scene:newChildSprite({class = "ParticleEffect", x = 650, y = 130, library = "PARTICLE_FIREWORKS_03", section = "fireworks", name = "Firework7", play = false})	
	local firework8 = scene:newChildSprite({class = "ParticleEffect", x = 700, y = 56, library = "PARTICLE_FIREWORKS_01", section = "fireworks", name = "Firework8", play = false})	

	congratulationsTitle:animate({visible=false, y=-500})
	playerName:animate({visible=true, alpha=0.0})
	beatGameText:animate({visible=true, alpha=0.0})
	
	local congratulationsEndY = congratulationsTitle:y()
		
	local ufoController = scene:child("UFO_CONTROLLER")
	local beamOverlay = ufoController:child("BEAM_OVERLAY")
	local ufoAnim = ufoController:child("UFO_ANIM")
	local boss1 = ufoController:child("BOSS_1")
	local boss2 = ufoController:child("BOSS_2")	
	local boss3 = ufoController:child("BOSS_3")		
	local boss4 = ufoController:child("BOSS_4")		
	local boss5 = ufoController:child("BOSS_5")		
	local boss6 = ufoController:child("BOSS_6")		
	local boss7 = ufoController:child("BOSS_7")	
	local boss8 = ufoController:child("BOSS_8")			
	
	local ufoBeamParticle = ufoAnim:newChildSprite({class = "ParticleEffect", x = 0, y = 175, library = "PARTICLE_ALIENSHIPBEAM", section = "scenequestcomplete", name = "UfoBeamParticle", play = true})	
	if ufoBeamParticle ~= nil then
		ufoBeamParticle:play()
		ufoBeamParticle:addInitialTime(5000)
	end
	
	ufoController:setX(940)
	ufoController:setY(310)
	
	beamOverlay:animate({x=0, y=120, alpha=0})
	
	ufoAnim:animate({visible=true, x=0, y=-45, startFrame=0, endFrame=7, frameTime = 100, loops="forever"})
	boss1:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss2:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss3:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss4:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss5:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss6:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss7:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})
	boss8:animate({x=0, y=660, scaleX=1.25, scaleY=1.25, visible=false})						
	
	ufoController:animate(
			
		{call = function() ghf.loopSound("SOUND_UFO_FLYING:scenequestcomplete") end},
		{x=460, y=104, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss1, x=0, y=660, visible=true}, -- TURN ON BOSS 1 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss1, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 1
		},
		{sprite=boss1, x=17, y=6, duration=500, style="easeInOut"}, -- PARK BOSS 1
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 2 LOCATION 
		{x=660, y=80, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss2, x=0, y=660, visible=true}, -- TURN ON BOSS 2 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss2, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 2
		},
		{sprite=boss2, x=-20, y=-10, duration=500, style="easeInOut"}, -- PARK BOSS 2
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 3 LOCATION 
		{x=492, y=118, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss3, x=0, y=660, visible=true}, -- TURN ON BOSS 2 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss3, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 3
		},
		{sprite=boss3, x=-23, y=14, duration=500, style="easeInOut"}, -- PARK BOSS 3
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 4 LOCATION 
		{x=600, y=90, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss4, x=0, y=660, visible=true}, -- TURN ON BOSS 4 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss4, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 4
		},	
		{sprite=boss4, x=16, y=24, duration=500, style="easeInOut"}, -- PARK BOSS 4
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 5 LOCATION 
		{x=410, y=107, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss5, x=0, y=660, visible=true}, -- TURN ON BOSS 5 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss5, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 5
		},
		{sprite=boss5, x=-17, y=36, duration=500, style="easeInOut"}, -- PARK BOSS 5
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 6 LOCATION 
		{x=620, y=108, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss6, x=0, y=660, visible=true}, -- TURN ON BOSS 6 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss6, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 6
		},
		{sprite=boss6, x=29, y=-10, duration=500, style="easeInOut"}, -- PARK BOSS 6
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 7 LOCATION 
		{x=440, y=110, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss7, x=0, y=660, visible=true}, -- TURN ON BOSS 7 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss7, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 7
		},
		{sprite=boss7, x=6, y=-15, duration=500, style="easeInOut"}, -- PARK BOSS 7
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		
		-- MOVE UFO AND CONTROLLER TO BOSS 8 LOCATION 
		{x=510, y=95, duration=1500, style="easeInOut"},
		{sprite=beamOverlay, alpha=1.0, duration = 100},
		{sprite=boss8, x=0, y=660, visible=true}, -- TURN ON BOSS 7 VISIBILITY
		{
			{play = "SOUND_UFO_SUCKING_UP:scenequestcomplete"},	
			{sprite=boss8, y=100, duration=1500, scaleX=0.25, scaleY=0.25, style="easeInOut"}, -- PULL UP BOSS 8
		},
		{sprite=boss8, x=0, y=20, duration=500, style="easeInOut"}, -- PARK BOSS 8
		{sprite=beamOverlay, alpha=0.0, duration=100},  -- TURN OFF BEAM
		{
			{call=function() ghf.stopLoop("SOUND_UFO_FLYING:scenequestcomplete") end},
			{play = "SOUND_UFO_FLYING_OFF:scenequestcomplete"},	
			{x=1424, y=78, duration=2500, style="easeInOut"}, -- MOVE UFO CONTROLLER AND CHILDREN OFFSCREEN
		},
		{play = "SOUND_CHARACTER_DROP_IN:default"},	
		{sprite=avatar, y=aAvatarEndY, duration = 700},
		{command="SQUISH_AVATAR", target = scene},	
		{delay = 1200},
		
		{play = "SOUND_MAINMENU_LOGO_FALLING:default"},	
		{sprite=congratulationsTitle, visible=true},
		{sprite=congratulationsTitle, y=congratulationsEndY, duration = 600}, -- DROP THE CONGRATULATIONS TEXT 
		{call=function() SCENEQUESTCOMPLETE_PlayCongratulationParticle(congratulationsEndY) end},
		{sprite=congratulationsTitle, y=congratulationsEndY + 12, duration = 250}, 		
		{
			{play = "SOUND_MAINMENU_LOGO_HIT:default"},	
			{sprite=congratulationsTitle, y=congratulationsEndY - 8, duration = 250}, 
		},
		{sprite=congratulationsTitle, y=congratulationsEndY + 4, duration = 250}, 	
		{sprite=congratulationsTitle, y=congratulationsEndY - 2, duration = 250}, 
		{sprite=congratulationsTitle, y=congratulationsEndY, duration = 250}, 	
		{call=function() SCENEQUESTCOMPLETE_FadeInText() end},
		{delay=1000},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework1:play() end},
		},
		{delay=400},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework2:play() end},
		},
		{delay=400},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework3:play() end},
		},
		{delay=200},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework4:play() end},
		},
		{delay=500},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework5:play() end},
		},
		{delay=200},	
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework6:play() end},
		},
		{delay=200},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework7:play() end},
		},
		{delay=400},
		{
			{play = "SOUND_FIREWORK:fireworks"},	
			{call=function() firework8:play() end},
		},
		{delay=2000},
		{
			{sprite = playerName, alpha=0.0, duration=1000},
			{sprite = beatGameText, alpha=0.0, duration=1000},
			{alpha=0.0, duration=1000},
			{sprite=congratulationsTitle, alpha=0.0, duration=1000},
			{sprite=avatar, alpha=0.0, duration=1000},
		},
		{command="ROLL_CREDITS", target = scene}	
	)
end
