:NAME opening

:GLOBAL float32			state_start_time
:GLOBAL uint16			state_index
:GLOBAL c_object*		logo_3ec
:GLOBAL c_object*		override
:GLOBAL boolean			everything_loaded


-- ------------------------------ constants ------------------------------

k_state_moonshot_logo= 0
k_state_gameblyr_logo= 1
k_state_everything_loaded= 2
k_state_moonshot_presents= 3
k_state_3ec_logo= 4
k_state_base_fade= 5
k_state_logo_fade= 6

k_state_skip=99
k_state_skip_end=100


-- ------------------------------ initialization ------------------------------

function global_initialize(g)
	g.state_index= k_state_moonshot_logo
	g.state_start_time= 0
	g.logo_3ec= nil
	g.override= nil
	g.everything_loaded= false
end



-- ------------------------------ BASE SYSTEM ------------------------------


function start_the_start_script()
	local ws= world_state_get()
	local start_script= ws.global_script("start")
	if (start_script) then
		start_script.paused= false
	end
end

function hide_and_delete(plate_state)
	plate_state.hide_all(true)
	plate_state.delete_all()
end

function start()
	local campaign= campaign_get()
	if (campaign.game_level_played_this_session()) then
		local ws= world_state_get()
		-- fade immediately and start the start menu
		hide_and_delete(ws.plate_state_get("base"))
		hide_and_delete(ws.plate_state_get("focus"))
		hide_and_delete(ws.plate_state_get("titles"))
		start_the_start_script()
		sleep()
	else
		local focus= object_place("focus0")
		if (focus) then
			focus.tween_animation_play("start0")
		end
	
	end
end

function delete_plate(ws, name)
	local ps= ws.plate_state_get(name)
	if (ps) then
		ps.delete_all()
	end
end

function load_everything()
	-- we now go ahead and load at the beginning
	--local w= world_get()
	--w.zone_load("main_menu_art")
	--global.everything_loaded= true
end


function update()

	local ws= world_state_get()
	local state= global.state_index
	
	-- check for the override
	if (state <= k_state_base_fade) then
		local touch_interface= touch_get()
		if (touch_interface and touch_interface.touch_started()) then
			local touch_index= touch_interface.touch_start_index()
			local touch= touch_interface.touch[touch_index]
			local touched_object= touch.touch_handler_object
			if (touched_object) then
				local placement= touched_object.placement
				if (placement) then
					if (placement.name=="base") then
						global.override= ws.place_by_plate("override", "override")
						state= k_state_skip
						global.state_start_time= ws.time.time
					end
				end
			end
		end
	end
	
	local time= ws.time.time - global.state_start_time
	
	-- note, transition in + delay + transition out = 5.5 seconds
	-- this should be the same as the delays of k_state_gameblyr_logo+k_state_everything_loaded
	-- note ALSO, there is an unreliable delay in there because we are loading the art zone right in the middle
	
	if (state==k_state_moonshot_logo and time > 7.2) then	-- 2s (intro), 2s (delay), outro (2.5s)
	
		delete_plate(ws, "titles")
		ws.place_by_plate("titles", "gameblyr_logo")
		state= k_state_gameblyr_logo
		
		local focus= object_get("focus0")
		if (focus) then
			focus.restore_initial_transform()
			local tween= focus.tween_get()
			tween.stop_all()
			tween.play_animation("start_gameblyr")
		end
	
	elseif (state==k_state_gameblyr_logo and time > 6) then

		delete_plate(ws, "titles")
		delete_plate(ws, "focus")

		ws.place_by_plate("titles", "moonshot")
		ws.place_by_plate("titles", "presents")
		local focus= object_place("focus1")
		if (focus) then
			focus.tween_animation_play("start1")
		end
		state= k_state_moonshot_presents
		
	elseif (state==k_state_moonshot_presents and time > 6.5) then		-- transition in (2s) + delay (2s) + transition out (2.5s) = 5.5s
		
		delete_plate(ws, "titles")
		delete_plate(ws, "focus")
		
		global.logo_3ec= ws.place_by_plate("titles", "3ec_logo")
		
		state=k_state_3ec_logo
		
		local focus= object_place("focus2")
		if (focus) then
			focus.tween_animation_play("start2")
		end
		
	elseif (state==k_state_3ec_logo and time > 3) then
	
		-- start fading out base layer

		local ps= ws.plate_state_get("base")
		if (ps) then
			local base= ps.object_get("base")
			if (base) then
				base.tween_animation_play("end")
			end
		end
		state= k_state_base_fade
	elseif (state==k_state_base_fade and time > 2) then

		if (global.logo_3ec) then
			global.logo_3ec.tween_animation_play("end")
			global.logo_3ec= nil
		end
		state= k_state_logo_fade

	elseif (state==k_state_logo_fade and time > 2) then
	
		start_the_start_script()		
		sleep()
	
	elseif (state==k_state_skip and time > 1) then
	
		-- fade-in of the override scrim is complete
		ws.plate_state_get("base").delete_all()
		ws.plate_state_get("focus").delete_all()
		ws.plate_state_get("titles").delete_all()
		if (global.override) then
			global.override.tween_animation_play("end")
		end
		
		if (not global.everything_loaded) then
			load_everything()
		end
		
		state= k_state_skip_end
	elseif (state==k_state_skip_end and time > 1) then
		--fade-out of the scrim is complete
		start_the_start_script()		
		sleep()
	end
	
	if (global.state_index ~= state) then
		global.state_index= state
		global.state_start_time= ws.time.time
	end

end

system_register(start, update, nil, nil)
