------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------
-- Author: Ian Eller
-- Updated: 04/13/2008

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


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

--moved up to access mydata in the function below
local myData = Entity:Data()
Entity:SHR_Refresh( myData, true, false, false, false )
Entity:SPT_RefreshNumbers( myData )
Entity:SPT_RefreshEntities( myData )


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

local function WithPlayer( theKey, theValue )
  Player:AddToFFDist( 2 )
  
  --spawn particle explosion at location
  --takes entity file, position x, y, z, rotation x, y, z
  Global:CreateEntityAt( "../../Assets/XML/entities/e_ParticleFlash.XML", myData.pos.x, myData.pos.y, myData.pos.z, 0, 0, 0 )
  
  --delete firefly
  Global:RemoveColoredFirefly( myData.id )
  Global:DeleteEntityByPtr( Entity:GetMyPointer() )
end


--------------------------------------------------------------------------------
-- Loop
--------------------------------------------------------------------------------

myData.id = Global:CreateColoredFirefly( myData.pos.x, myData.pos.y, myData.pos.z, 0 )

local pos = {}
local vel = {}
local collided = false
while( 1 ) 
do
  coroutine.yield()
  
  ENTITY = Entity
  Entity:SHR_Refresh( myData, true, false, false, false )

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

end
