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

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

local myPtr  = Entity:GetMyPointer()
local myData = Entity:Data()
Entity:SHR_Refresh( myData, true, false, false, true )
Entity:SPT_RefreshNumbers( myData )
myData.respawnFF = 0

local OFFSETS = { [ 0 ] = { x =  0, y =  0, z =  0 },
                  [ 1 ] = { x =  3, y =  3, z =  3 },
                  [ 2 ] = { x =  3, y =  3, z = -3 },
                  [ 3 ] = { x = -3, y =  3, z = -3 },
                  [ 4 ] = { x = -3, y =  3, z =  3 },
                  [ 5 ] = { x =  3, y = -3, z =  3 },
                  [ 6 ] = { x =  3, y = -3, z = -3 },
                  [ 7 ] = { x = -3, y = -3, z = -3 },
                  [ 8 ] = { x = -3, y = -3, z =  3 }
                }
local OFFMAX = table.maxn( OFFSETS )
local ids = { }


--------------------------------------------------------------------------------
-- Spawn a firefly
--------------------------------------------------------------------------------

local function SpawnFF( toSpawn )

  local ff = Global:CreateEntity( "../../Assets/Xml/entities/e_Firefly.xml" )
  local off = math.random( 0, OFFMAX )
  off = OFFSETS[ off ]
  
  ff:SHR_sPosition( myData.pos.x + off.x, myData.pos.y + off.y, myData.pos.z + off.z )
  if( ids[ toSpawn ] ) 
  then
    Global:CreateStandardFirefly( myData.pos.x + off.x,
                                  myData.pos.y + off.y,
                                  myData.pos.z + off.z,
                                              ids[ toSpawn ] )
                                              
  else
    ids[ toSpawn ] = Global:CreateStandardFirefly( myData.pos.x + off.x,
                                             myData.pos.y + off.y,
                                             myData.pos.z + off.z,
                                                                0 )
                                                                
  end
  
  ff:SPT_GiveNumber( "id", ids[ toSpawn ] )
  ff:SPT_GiveEntity( "parent", myPtr )
  
end


--------------------------------------------------------------------------------
-- Spawn all fireflies
--------------------------------------------------------------------------------

local function SpawnAll( toSpawn )
  local i = 0 
  while( i < toSpawn )
  do
    SpawnFF( i )
    
    i = i + 1
  end
  
end


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

local timer    = 0
local timerMax = 100
local toSpawn  = 0
SpawnAll( myData.numberFF )
while( 1 ) 
do
  coroutine.yield()
  
  Entity:SPT_RefreshNumbers( myData )
  
  timer = timer + 1
  if( timer >= timerMax )
  then
    local incr = 0
    while( incr < myData.numberFF )
    do
      local string = tostring( ids[ incr ] )
      if( myData[ string ] ) 
      then
        SpawnFF( incr )
        myData[ string ] = nil
        
      end
    
      incr = incr + 1
    end
  
    timer = 0
  end
  

end
