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

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


--------------------------------------------------------------------------------
-- Fuctions!
-- This might look redundant, but avoids costly global lookup
--------------------------------------------------------------------------------

local PLAYER = Player
local ENTITY = Entity

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

local VEC3_construct                 = vec3.construct
local VEC3_negateThis                = vec3.negateThis
local VEC3_assign3                   = vec3.assign3
local VEC3_copyThis                  = vec3.copyThis
local VEC3_addThis                   = vec3.addThis
local VEC3_subtractThis              = vec3.subtractThis
local VEC3_multiplyThis              = vec3.multiplyThis
local VEC3_normalize                 = vec3.normalize
local VEC3_magnitude                 = vec3.magnitude

local MATH_abs                       = math.abs

local STRING_find                    = string.find

local GLOBAL_CreateEntityAt          = Global.CreateEntityAt
local GLOBAL_DeleteEntityByPtr       = Global.DeleteEntityByPtr
local GLOBAL_IncFFCombo              = Global.IncFFCombo
local GLOBAL_gRangeState             = Global.gRangeState
local GLOBAL_gCameraPosition         = Global.gCameraPosition
local GLOBAL_gCameraDirection        = Global.gCameraDirection
local GLOBAL_SetCameraPositionVector = Global.SetCameraPositionVector
local GLOBAL_CameraPullBack          = Global.CameraPullBack
local GLOBAL_GameFireflyCollided     = Global.GameFireflyCollided

local ENTITY_GetMyPointer            = Entity.GetMyPointer
local ENTITY_GetName                 = Entity.GetName
local ENTITY_SHR_gPosition           = Entity.SHR_gPosition
local ENTITY_SHR_sPosition           = Entity.SHR_sPosition
local ENTITY_SHR_gVelocity           = Entity.SHR_gVelocity
local ENTITY_SHR_sVelocity           = Entity.SHR_sVelocity
local ENTITY_PHY_Refresh             = Entity.PHY_Refresh
local ENTITY_SPT_AddNumber           = Entity.SPT_AddNumber


--------------------------------------------------------------------------------
-- Vars
--------------------------------------------------------------------------------
local myData  = {}
local ASTER   = PLAYER_gAster( PLAYER )
local LANTERN = PLAYER_gLantern( PLAYER )

local pos = ENTITY_SHR_gPosition( ENTITY )
local vector = VEC3_normalize( VEC3_construct( 0, 0, 0 ) )

local done = false


--------------------------------------------------------------------------------
-- What happens when we've collided?
--------------------------------------------------------------------------------

local function WithPlayer( theKey, theValue )
  if( STRING_find( ENTITY_GetName( theValue ), "Lantern" ) ~= nil ) 
  then
    local lightCount = GLOBAL_IncFFCombo()
    PLAYER_AddToLightCount( PLAYER, 20 + ( 5 * lightCount ) )
    
    local vGrowth = GLOBAL_gRangeState( 2 )
    if( vGrowth.x > 0 )
    then
      local newPos = GLOBAL_gCameraPosition()
      newPos = VEC3_subtractThis( newPos, VEC3_normalize( GLOBAL_gCameraDirection() ) )
      GLOBAL_SetCameraPositionVector( newPos.x, newPos.y, newPos.z )
      GLOBAL_CameraPullBack()
      
    end
    
    --spawn particle explosion at location
    GLOBAL_GameFireflyCollided( pos.x, pos.y, pos.z, lightCount )
    
    ENTITY_SPT_AddNumber( LANTERN, "boost", 1 )
    
    --delete firefly
    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( ENTITY ) )
    
    
  end
end


--------------------------------------------------------------------------------
-- Loop
--------------------------------------------------------------------------------
    
local MAX_SPEED = 15
while( 1 ) 
do
  coroutine.yield()

  ENTITY = Entity

  pos = ENTITY_SHR_gPosition( ENTITY )
  local toAster = VEC3_negateThis( VEC3_subtractThis( pos, ENTITY_SHR_gPosition( LANTERN ) ) )

  vector = VEC3_multiplyThis( VEC3_normalize( toAster ), MAX_SPEED )
  
  ENTITY_SHR_sVelocity( ENTITY, vector.x, vector.y, vector.z )

  ENTITY_PHY_Refresh( ENTITY, myData, true )
  table.foreach( myData.collisions, WithPlayer )
  
end
