------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
--------------------------------------------------------------------------------
-- Fuctions!
-- This might look redundant, but avoids costly global lookup
--------------------------------------------------------------------------------

local GLOBAL_TimeElapsed             = Global.TimeElapsed
local GLOBAL_SetSeason               = Global.SetSeason
local GLOBAL_UpdateCameraThird       = Global.UpdateCameraThird
local GLOBAL_ClampCameraZoomMinMax   = Global.ClampCameraZoomMinMax
local GLOBAL_CreateEntity            = Global.CreateEntity
local GLOBAL_DeleteEntityByPtr       = Global.DeleteEntityByPtr

local ENTITY_GetMyPointer            = Entity.GetMyPointer
local ENTITY_SPT_RefreshStrings      = Entity.SPT_RefreshStrings

--------------------------------------------------------------------------------
-- Variables
--------------------------------------------------------------------------------
local myData = {}
local summer = { 1.0, 0.0, 0.0,
                 0.0, 1.0, 0.0,
                 0.272, 0.234, 0.294,
                 0.468, 0.588, 0.544,
                 0.1, 0.2, 0.3 }
local fall   = { 0.0, 1.0, 0.0,
                 1.0, 0.0, 0.0,
                 0.468, 0.588, 0.544,
                 0.294, 0.272, 0.234,
                 0.14, 0.177, 0.258 }
local winter = { 0.4, 0.35, 0.4,
                 1.2, 1.2, 1.2,
                 .287, 0.234, 0.234,
                 0.6, 0.6, 0.6,
                 0.19, 0.17, 0.18 }
local from
local to
local TRANSITION_TIME = 3000
local ZOOM = 300

--------------------------------------------------------------------------------
-- Functions
--------------------------------------------------------------------------------
local function Clamp(value, min, max)
  if value < min then value = min end
  if value > max then value = max end
  return value
end

local function Interp(indx, t)
  return (1 - t) * from[indx] + t * to[indx]
end

--------------------------------------------------------------------------------
-- Initialization
--------------------------------------------------------------------------------
ENTITY_SPT_RefreshStrings( Entity, myData )

if( myData.Transition == "SummerFall" )
then
  from = summer
  to   = fall
else
  from = fall
  to   = winter
end

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

  elapsedTime = elapsedTime + GLOBAL_TimeElapsed()

  --move the camera to a reasonable location
  GLOBAL_ClampCameraZoomMinMax(ZOOM, ZOOM)
  GLOBAL_UpdateCameraThird(0, -3, 1)

  --update the season
  local t = Clamp( elapsedTime / TRANSITION_TIME, 0, 1 )

  GLOBAL_SetSeason( Interp( 1, t), Interp( 2, t), Interp( 3, t),
                    Interp( 4, t), Interp( 5, t), Interp( 6, t),
                    Interp( 7, t), Interp( 8, t), Interp( 9, t),
                    Interp(10, t), Interp(11, t), Interp(12, t),
                    Interp(13, t), Interp(14, t), Interp(15, t) )

  --kill the entity when we're done
  if( elapsedTime >= TRANSITION_TIME )
  then
    if( myData.Transition ~= "SummerFall" )
    then
      GLOBAL_CreateEntity( "../../Assets/Xml/entities/game_WinterFFSea.lua" )

    end

    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( Entity ) )

  end

end
