------ 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_gFFDist                 = Player.gFFDist
local PLAYER_AddToLightCount         = Player.AddToLightCount

local VEC3_construct                 = vec3.construct
local VEC3_assign3                   = vec3.assign3
local VEC3_copyThis                  = vec3.copyThis
local VEC3_addThis                   = vec3.addThis
local VEC3_subtract                  = vec3.subtract
local VEC3_subtractThis              = vec3.subtractThis
local VEC3_subtractTwo               = vec3.subtractTwo
local VEC3_multiplyThis              = vec3.multiplyThis
local VEC3_dot                       = vec3.dot
local VEC3_normalize                 = vec3.normalize
local VEC3_magnitude                 = vec3.magnitude

local MATH_random                    = math.random

local STRING_find                    = string.find

local GLOBAL_CreateEntityAt          = Global.CreateEntityAt
local GLOBAL_DeleteEntityByPtr       = Global.DeleteEntityByPtr
local GLOBAL_IncFFCombo              = Global.IncFFCombo
local GLOBAL_FireflySound            = Global.FireflySound
local GLOBAL_GameFireflyCollided     = Global.GameFireflyCollided
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 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_sVelocity           = Entity.SHR_sVelocity
local ENTITY_PHY_Refresh             = Entity.PHY_Refresh
local ENTITY_SPT_AddNumber           = Entity.SPT_AddNumber 
local ENTITY_SPT_RefreshNumbers      = Entity.SPT_RefreshNumbers
local ENTITY_SPT_RefreshEntities     = Entity.SPT_RefreshEntities

--------------------------------------------------------------------------------
-- Vars
--------------------------------------------------------------------------------

--moved up to access mydata in the function below
local myData = {}
ENTITY_SPT_RefreshNumbers( ENTITY, myData )
ENTITY_SPT_RefreshEntities( ENTITY, myData )
local pos = { }
local vel = { }
local collided = false

local LANTERN = PLAYER_gLantern( PLAYER )

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

local function WithPlayer( theKey, theValue )
  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 )
  
  --inform spawner
  ENTITY_SPT_AddNumber( myData.parent, tostring( myData.id ), myData.id )
  ENTITY_SPT_AddNumber( LANTERN, "boost", 1 )
  
  --delete firefly
  GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( ENTITY ) )
end


--------------------------------------------------------------------------------
-- Loop
--------------------------------------------------------------------------------
    
-- set a really, really low velocity because Justin said so.
Entity:SHR_sVelocity( .000001, .000001, .000001 )
while( 1 ) 
do
  coroutine.yield()
  
  ENTITY = Entity
  Entity:SHR_Refresh( myData, true, false, false, false )

  pos = myData.pos
  vel, collided = Global:UpdateStandardFirefly( pos.x, pos.y, pos.z, myData.id )
  Entity:SHR_sPosition( vel.x, vel.y, vel.z )
    
  if( collided ) then
    WithPlayer()
    
  end

end
