------ 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 VEC3_construct                 = vec3.construct
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_gCameraPosition         = Global.gCameraPosition
local GLOBAL_gCameraDirection        = Global.gCameraDirection
local GLOBAL_SetCameraPositionVector = Global.SetCameraPositionVector

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 pos = ENTITY_SHR_gPosition( ENTITY )
local vector = VEC3_normalize( VEC3_construct( 0, 0, 0 ) )

local done = false


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

  ENTITY = Entity

  pos = ENTITY_SHR_gPosition( ENTITY )
  local toAster = ENTITY_SHR_gPosition( Aster )
  
  if( not done )
  then
    toAster = VEC3_subtractThis( toAster, pos )
    if( VEC3_magnitude( toAster ) < .7 )
    then 
      --inform spawner
      done = true
      ENTITY_SPT_AddNumber( Aster, "reviverFly", 1 )
      
    end
    
    local offSet = 0
    if( toAster.y < .5 ) then
      offSet = 5.5
      
    elseif( toAster.y > .5 ) then
      offSet = -5.5
      
    end
  
    vector = VEC3_multiplyThis( VEC3_normalize( toAster ), MAX_SPEED )
    vector = VEC3_multiplyThis( vector, MAX_MOVE )
    local asterVec  = ENTITY_SHR_gVelocity( Aster )
    vector.y = asterVec.y - offSet
    
    ENTITY_SHR_sVelocity( ENTITY, vector.x, vector.y, vector.z )
    
  else
    ENTITY_SHR_sPosition( ENTITY, toAster.x, toAster.y, toAster.z )
  
  end

end
