-- Project load animator

.define STATE_BIGFISH_FADEIN	= 0
.define STATE_BIGFISH			= 1
.define STATE_BIGFISH_FADEOUT	= 2
.define STATE_MERIDIAN_FADEIN	= 3
.define STATE_MERIDIAN			= 4
.define STATE_MERIDIAN_FADEOUT	= 5
.define STATE_WBP_FADEIN		= 6
.define STATE_WBP				= 7
.define STATE_WBP_FADEOUT		= 8
.define STATE_EXIT				= 9

.define SPEED_FADE				= 500
.define NB_SECONDS_STAY			= 2


function OnAnimatorInit()
	--bulky = OPLoadAnimator.CreateSprite('media/graph/ecrans_additionnels/default.png')
	--wbp = OPLoadAnimator.CreateSprite('media/graph/ecrans_additionnels/loading_start.jpg')
	--lkoa = OPLoadAnimator.CreateSprite('media/graph/ecrans_additionnels/ecran_intro.png')
	--fader = OPLoadAnimator.CreateRect()
	
	if OPLoadAnimator.GetLanguage() == "french" then
		bulky = OPLoadAnimator.CreateSprite('media/common/graph/interface/Splash_Screen/splash_bfg_fre.jpg')
	else
		bulky = OPLoadAnimator.CreateSprite('media/common/graph/interface/Splash_Screen/splash_bfg_eng.jpg')
	end

	meridian = OPLoadAnimator.CreateSprite('media/common/graph/interface/Splash_Screen/M4logo_bluelarge.jpg')
	wbp = OPLoadAnimator.CreateSprite('media/common/graph/interface/Splash_Screen/splash_wbp.jpg')
	fader = OPLoadAnimator.CreateRect()
	OPLoadAnimator.SetParameters(bulky,{show=true})
	OPLoadAnimator.SetParameters(fader,{show=true, color_a=255, width=1024, height=768})
	
	-- loadbar_2 = OPLoadAnimator.CreateSprite('media/common/graph/interface/Loading/loading_2.png')
	-- loadbar_3 = OPLoadAnimator.CreateSprite('media/common/graph/interface/Loading/loading_3.png')
	
	--OPLoadAnimator.SetParameters(loadbar_1,{show=true})
	
	state = STATE_BIGFISH_FADEIN
	cpt = 0
	fade_enabled = true
	fade_reverse = false
end

function OnAnimatorUpdate(dt)

	local next_state = false

	if (fade_enabled) then
		cpt = cpt + dt*SPEED_FADE
		if (cpt > 255) then
			cpt = 255
			next_state = true
		end
		
		if (fade_reverse) then
			OPLoadAnimator.SetParameters(fader,{color_a=cpt})
		else
			OPLoadAnimator.SetParameters(fader,{color_a=255-cpt})
		end
			
		if (next_state) then
			fade_reverse = not fade_reverse
		end
	else
		cpt = cpt + dt
		if (cpt >= NB_SECONDS_STAY) then
			next_state = true
		end
	end
	
	if (next_state) then
		cpt = 0
		state = state + 1
		if (state == STATE_EXIT) then
			OPLoadAnimator.Exit()
		else
			fade_enabled = not (state == STATE_BIGFISH or state == STATE_WBP or state == STATE_MERIDIAN)
		end
		
		if (state == STATE_WBP_FADEIN) then
			OPLoadAnimator.SetParameters(meridian,{show=false})
			OPLoadAnimator.SetParameters(wbp,{show=true})
		end
		
		if (state == STATE_MERIDIAN_FADEIN) then
			OPLoadAnimator.SetParameters(bulky,{show=false})
			OPLoadAnimator.SetParameters(meridian,{show=true})
		end
	end

end
