------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------

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

--------------------------------------------------------------------------------
-- Fuctions!
-- This might look redundant, but avoids costly global lookup
--------------------------------------------------------------------------------
local PLAYER_gLantern                = Player.gLantern

local GLOBAL_TimeElapsed             = Global.TimeElapsed
local GLOBAL_LaunchTutorial          = Global.LaunchTutorial
local GLOBAL_SetGameMode             = Global.SetGameMode
local GLOBAL_CreateNewOldVine        = Global.CreateNewOldVine
local GLOBAL_CreateEntity            = Global.CreateEntity
local GLOBAL_DeleteEntityByPtr       = Global.DeleteEntityByPtr
local GLOBAL_SetCameraViewVector     = Global.SetCameraViewVector
local GLOBAL_FindEntityByName        = Global.FindEntityByName
local GLOBAL_gVineEndPoint           = Global.gVineEndPoint

local ENTITY_SHR_gPosition           = Entity.SHR_gPosition
local ENTITY_SHR_sPosition           = Entity.SHR_sPosition
local ENTITY_SPT_GiveNumber          = Entity.SPT_GiveNumber
local ENTITY_SPT_GiveString          = Entity.SPT_GiveString
local ENTITY_GetMyPointer            = Entity.GetMyPointer

local VEC3_subtractThis              = vec3.subtractThis
local VEC3_normalize                 = vec3.normalize
local VEC3_multiplyThis              = vec3.multiplyThis

--------------------------------------------------------------------------------
-- Initialization
--------------------------------------------------------------------------------
local slideshow = GLOBAL_CreateEntity( "../../Assets/Xml/entities/game_Slideshow.xml" )
local tSlide = 8000
local tTransition = 1500
local slideCount = 4
local totalTime = slideCount * tSlide + ( slideCount + 1 ) * tTransition
local trulyTotalTime = totalTime + 4000;

ENTITY_SPT_GiveString( slideshow, "ShowName", "IntroSlides0" )
ENTITY_SPT_GiveNumber( slideshow, "TimePerSlide", tSlide )
ENTITY_SPT_GiveNumber( slideshow, "TransitionTime", tTransition )
ENTITY_SPT_GiveNumber( slideshow, "SlideCount", slideCount )

ENTITY_SPT_GiveString( slideshow, "Text1", "A journey inspired by the Moon's reflection" )
ENTITY_SPT_GiveNumber( slideshow, "Height1", .3 ) -- Height does not have to be specified
ENTITY_SPT_GiveString( slideshow, "Text2", "Aided by a trusty vine" )
ENTITY_SPT_GiveNumber( slideshow, "Height2", .8 ) -- Height does not have to be specified
ENTITY_SPT_GiveString( slideshow, "Text3", "Aster guides the plant's growth" )
ENTITY_SPT_GiveNumber( slideshow, "Height3", .2 ) -- Height does not have to be specified
ENTITY_SPT_GiveString( slideshow, "Text4", "To fly to the Moon" )
ENTITY_SPT_GiveNumber( slideshow, "Height4", .5 ) -- Height does not have to be specified

local twisty = GLOBAL_CreateNewOldVine( 40.0, -100.0, -51.0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, twisty, "../../Assets/Xml/entities/e_OldVine.xml" )

--------------------------------------------------------------------------------
-- Loop
--------------------------------------------------------------------------------
local MOON_LOOK_TIME
local elapsedTime = 0
while( 1 ) 
do
  coroutine.yield()

  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()
  if( elapsedTime > trulyTotalTime )
  then
    GLOBAL_LaunchTutorial( )

    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( Entity ) )

  elseif( elapsedTime > totalTime )
  then
    -- Start looking down at the moon
    local lantern = PLAYER_gLantern( Player )
    local moon = GLOBAL_FindEntityByName( "Moon", false )
    local mPos = ENTITY_SHR_gPosition( moon )
    local lPos = ENTITY_SHR_gPosition( lantern )

    VEC3_subtractThis( mPos, lPos )
    VEC3_normalize( mPos )
    VEC3_multiplyThis( mPos, 20 )

    GLOBAL_SetCameraViewVector( mPos.x, mPos.y, mPos.z )

  elseif( elapsedTime > totalTime / 2 )
  then
    Global.SetFade( 0.0 )

    GLOBAL_SetGameMode( true )

    local lantern = PLAYER_gLantern( Player )
    ENTITY_SPT_GiveNumber( lantern, "TakeCamera", 1.0 )

    -- Start Aster on the vine
    local aster = GLOBAL_FindEntityByName( "Aster", false )
    local vPos = GLOBAL_gVineEndPoint( )
    ENTITY_SHR_sPosition( aster, vPos.x, vPos.y, vPos.z )

    -- Start looking down at the moon
    local mPos = {x = 11.786, y = 28.379, z = 22.758}
    local lPos = {x = 11.495, y = 30.662, z = 2.745}

    VEC3_subtractThis( mPos, lPos )
    VEC3_normalize( mPos )
    VEC3_multiplyThis( mPos, 20 )

    GLOBAL_SetCameraViewVector( mPos.x, mPos.y, mPos.z )
	
	-- Fade camera view vector from mPos to nPos

  end

end
