------ All content © 2008 DigiPen (USA) Corporation, all rights reserved. ------

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

local ENTITY = Entity

local GLOBAL_GetLastXVineNodes       = Global.GetLastXVineNodes
local GLOBAL_TimeElapsed             = Global.TimeElapsed
local GLOBAL_DeleteEntityByPtr       = Global.DeleteEntityByPtr

local ENTITY_SHR_sPosition           = Entity.SHR_sPosition

local ENTITY_SPT_RefreshNumbers      = Entity.SPT_RefreshNumbers

local ENTITY_GetMyPointer            = Entity.GetMyPointer



--------------------------------------------------------------------------------
-- Var Par
--------------------------------------------------------------------------------
local Nodes  = GLOBAL_GetLastXVineNodes( 20 ) --The last 20 nodes
local indx1  = 0
local indx2  = 1
local x = 0
local y = 0
local z = 0
local t = 1

local TRAVEL_TIME = 2000 --milliseconds per segment



--------------------------------------------------------------------------------
-- What's your function, conjunction junction...
--------------------------------------------------------------------------------
local function Lerp(x1, y1, z1, x2, y2, z2, t)
  local T = 1 - t
  return ( T * x1 + t * x2 ),
         ( T * y1 + t * y2 ),
         ( T * z1 + t * z2 )

end



--------------------------------------------------------------------------------
-- Loop
--------------------------------------------------------------------------------
while( 1 ) 
do
  coroutine.yield()

  ENTITY = Entity

  t = t + GLOBAL_TimeElapsed() / TRAVEL_TIME

  if( t >= 1 )
  then
    t = 0
    indx1 = indx1 + 1
    indx2 = indx2 + 1

  end

  if( indx2 > Nodes.Count )
  then
    GLOBAL_DeleteEntityByPtr( ENTITY_GetMyPointer( ENTITY ) )

  else
    local p1 = Nodes[indx1]
    local p2 = Nodes[indx2]
    x, y, z = Lerp( p1.x, p1.y, p1.z,
                    p2.x, p2.y, p2.z,
                    t )

    ENTITY_SHR_sPosition( ENTITY, x, y, z )

  end

end
