------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Hamza M. Hutchinson
-- Created: 10/25/2008

local ENTITY = Entity
local PLAYER = Player

local PLAYER_gAster                        = Player.gAster
local PLAYER_gLantern                      = Player.gLantern

local GLOBAL_CreateEntity                  = Global.CreateEntity
local GLOBAL_CreateEntityAt                = Global.CreateEntityAt
local GLOBAL_DeleteEntityByPtr             = Global.DeleteEntityByPtr
local GLOBAL_FindEntityByName              = Global.FindEntityByName
local GLOBAL_SetGameMode                   = Global.SetGameMode
local GLOBAL_TimeElapsed                   = Global.TimeElapsed
local GLOBAL_SetFade                       = Global.SetFade
local GLOBAL_ControllerConnected           = Global.ControllerConnected
local GLOBAL_SetCameraViewVector           = Global.SetCameraViewVector
local GLOBAL_SetCameraTargetPositionVector = Global.SetCameraTargetPositionVector

local ENTITY_GetMyPointer                  = Entity.GetMyPointer
local ENTITY_SHR_sPosition                 = Entity.SHR_sPosition
local ENTITY_SHR_gPosition                 = Entity.SHR_gPosition
local ENTITY_SHR_sVelocity                 = Entity.SHR_sVelocity
local ENTITY_SHR_sRotation                 = Entity.SHR_sRotation
local ENTITY_SHR_gRotation                 = Entity.SHR_gRotation
local ENTITY_SPT_GiveNumber                = Entity.SPT_GiveNumber


dofile( "../../Assets/Scripts/key_name_mappings.lua" )

local DreamsideMaroon = Global:CreateEntityAt( "../../Assets/Xml/entities/e_SplashDSM.xml", 10, -10, 0, -28, -7, 6 )  -- logo

local myData = {}
local slides = {}
local viewVecs = {}
local posVecs = {}
local time = {}
local indx = 1
local indxMax = 3

slides[1]   = GLOBAL_CreateEntityAt( "../../Assets/Xml/entities/e_SplashDPLogo.xml", 0.0, 0.0, -1700.0, 0.0, 90.0, 0.0 )
posVecs[1]  = { x = 0.0, y = 0.0, z = -1700.0 }
viewVecs[1] = { x = -20.0, y = 0.0, z = 0.0 }
time[1]     = 4000--1600 --

slides[2]   = GLOBAL_CreateEntityAt( "../../Assets/Xml/entities/e_SplashTerracedLogo.xml", -1000.0, 0.0, -1700.0, 0.0, 90.0, 0.0 )
posVecs[2]  = { x = -1000.0, y = 0.0, z = -1700.0 }
viewVecs[2] = { x = -20.0, y = 0.0, z = 0.0 }
time[2]     = 4000

posVecs[3]  = { x = 8.0, y = 25.06, z = -0.07 }
viewVecs[3] = { x = 12.21, y = -20.56, z = 47.51 }
time[3]     = 0

GLOBAL_SetCameraTargetPositionVector( posVecs[ indx ].x, posVecs[ indx ].y, posVecs[ indx ].z )
GLOBAL_SetCameraViewVector( viewVecs[ indx ].x, viewVecs[ indx ].y, viewVecs[ indx ].z )

local elapsedTime = 0
local tText       = 0
local tFadeIn     = 1000
local tFadeOut    = 1000
local tCleanUp    = 4000

local FADE_AMOUNT = 4.0

local functionPointer


--============================================================================
local function AddText( text )
  local len = string.len( text )

  local CHAR_WIDTH = ( 0.6 / 40.0 ) * len
  local CHAR_TIME = 200 * len
  tText = tText + CHAR_TIME

  Global:AddPopupTextToQueue( text, CHAR_TIME, ( 1 - CHAR_WIDTH ) / 2 + 0.2, .1 )

end

--============================================================================
local function SubmitText()
  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()

  if( elapsedTime >= tText )
  then
    elapsedTime = 0
    tText = 0

    if( GLOBAL_ControllerConnected() )
    then
      AddText( "Press (A) to begin" )
    else
      AddText( "Press SPACE to begin" )
    end
  end
  
end

--============================================================================
local function cleanUp()
  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()

  if( elapsedTime >= tCleanUp )
  then
    GLOBAL_DeleteEntityByPtr( DreamsideMaroon )
    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( slides[ 1 ] ) )
    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( slides[ 2 ] ) )

    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( Entity ) )
  end

end

--============================================================================
local function waitingToStart()
  SubmitText()

  if( Global:IsGameButtonTriggered( 0 ) )
  then
    GLOBAL_CreateEntity( "../../Assets/Xml/entities/game_IntroSequence.xml" ) -- Intro sequence gives camera to player

    elapsedTime = 0
    functionPointer = cleanUp

  end

end

--============================================================================
local function fadeOut()
  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()

  local t = elapsedTime / tFadeIn
  
  GLOBAL_SetFade( 1 )

  if( t < 1 )
  then
    elapsedTime = 0
    functionPointer = waitingToStart

  end

end

--============================================================================
local function slides()
  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()
  
  if( indx == indxMax - 1 ) 
  then
    local t = elapsedTime / tFadeIn
    GLOBAL_SetFade( t )
    
  end

  if( elapsedTime >= time[ indx ] )
  then
    elapsedTime = 0
    indx = indx + 1

    if( indx <= indxMax )
    then
      GLOBAL_SetCameraTargetPositionVector( posVecs[ indx ].x, posVecs[ indx ].y, posVecs[ indx ].z )
      GLOBAL_SetCameraViewVector( viewVecs[ indx ].x, viewVecs[ indx ].y, viewVecs[ indx ].z )

    else
      functionPointer = fadeOut

    end

  end

end

--============================================================================
local function fadeIn()
  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()

  local t = 1 - elapsedTime / tFadeIn
  
  if( t <= 0 )
  then
    GLOBAL_SetFade( 0 )
    elapsedTime = 0
    functionPointer = slides

  else
    GLOBAL_SetFade( t )

  end

end

--============================================================================
functionPointer = fadeIn

local aster = PLAYER_gAster( PLAYER )
--============================================================================
-- Want to set game mode to false on the second frame and losing one frame on
--   the splash screen isn't a big deal
coroutine.yield()
coroutine.yield()
coroutine.yield()
coroutine.yield() -- Why 4?... I don't know, but it's the smallest yields it will work with
GLOBAL_SetGameMode( false )

--============================================================================
while( 1 ) 
do
	coroutine.yield()

	functionPointer()

    -- SUPER HACK TO GET THE LANTERN TO STOP BOUNCING IN THE SPLASH SCREEN!!!
	ENTITY_SHR_sVelocity( aster, 0, 0, 0 )

end
