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

Global:StatTrack( 1, true )
Global:StatTrack( 1, true )

local ASSETS_XML = "../../Assets/Xml/"
local Aster = Player:gAster()
local myData = Entity:Data()
local file


local longArch = Global:FindEntityByName( "longarch01", false )
local longArchPos = longArch:SHR_gPosition()

file = "island_parts/circ05.xml"
local circ5_1   = Global:CreateEntityAt( ASSETS_XML .. file, longArchPos.x + 20, longArchPos.y + -60, longArchPos.z +  95, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, circ5_1:GetMyPointer(), ASSETS_XML .. file )

local circ5_2   = Global:CreateEntityAt( ASSETS_XML .. file,  longArchPos.x + 15, longArchPos.y + -60, longArchPos.z + -90, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, circ5_2:GetMyPointer(), ASSETS_XML .. file )

local circ5_1Pos = circ5_1:SHR_gPosition()
local circ5_1Rot = circ5_1:SHR_gRotation()
local circ5_2Pos = circ5_2:SHR_gPosition()
local circ5_2Rot = circ5_2:SHR_gRotation()


file = "island_parts/rod02.xml"
local rod2_1 = Global:CreateEntityAt( ASSETS_XML .. file, circ5_1Pos.x, circ5_1Pos.y + 10, circ5_1Pos.z, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, rod2_1:GetMyPointer(), ASSETS_XML .. file )

local rod2_2 = Global:CreateEntityAt( ASSETS_XML .. file, circ5_2Pos.x, circ5_2Pos.y + 10, circ5_2Pos.z, 0, 0, 0 ) 
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, rod2_2:GetMyPointer(), ASSETS_XML .. file )

local rod2_1Pos = rod2_1:SHR_gPosition()
local rod2_1Rot = rod2_1:SHR_gRotation()
local rod2_2Pos = rod2_2:SHR_gPosition()
local rod2_2Rot = rod2_2:SHR_gRotation()
           
                                        
local HIGH_DIF = 35
local RAD_DIF  = HIGH_DIF + 55
local ROTATION_DIF = 360 / HIGH_DIF
local OK_GO1 = -1
local OK_GO2 = -1
local PLAYER_DIST = 150


local circ5_1AltPos = circ5_1Pos
local circ5_1AltRot = circ5_1Rot
local circ5_2AltPos = circ5_2Pos
local circ5_2AltRot = circ5_2Rot
local rod2_1AltPos = rod2_1Pos
local rod2_1AltRot = rod2_1Rot
local rod2_2AltPos = rod2_2Pos
local rod2_2AltRot = rod2_2Rot


file = "entities/e_DefaultLight.xml"
local light_1 = Global:CreateEntityAt( ASSETS_XML .. file, circ5_1Pos.x, circ5_1Pos.y, circ5_1Pos.z, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, light_1:GetMyPointer(), ASSETS_XML .. file )

local light_2 = Global:CreateEntityAt( ASSETS_XML .. file, circ5_2Pos.x, circ5_2Pos.y, circ5_2Pos.z, 0, 0, 0 ) 
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, light_2:GetMyPointer(), ASSETS_XML .. file )

light_1:VIS_SetLightColor( .3, 0.0, 1.0 )
light_2:VIS_SetLightColor( .3, 0.0, 1.0 )


local arrays = 
  { [ 1 ] = { ok          = OK_GO1,
              circ        = circ5_1,
              circPos     = circ5_1Pos,
              circAltPos  = circ5_1AltPos,
              circRot     = circ5_1Rot,
              circAltRot  = circ5_1AltRot,
              rod         = rod2_1,
              rodPos      = rod2_1Pos,
              rodAltPos   = rod2_1AltPos,
              rodRot      = rod2_1Rot,
              rodAltRot   = rod2_1AltRot,
              light       = light_1
            },
    [ 2 ] = { ok          = OK_GO2,
              circ        = circ5_2,
              circPos     = circ5_2Pos,
              circAltPos  = circ5_2AltPos,
              circRot     = circ5_2Rot,
              circAltRot  = circ5_2AltRot,
              rod         = rod2_2,
              rodPos      = rod2_2Pos,
              rodAltPos   = rod2_2AltPos,
              rodRot      = rod2_2Rot,
              rodAltRot   = rod2_2AltRot,
              light       = light_2
            }
  }
                

while( 1 ) 
do
  circ5_1:SHR_sPosition( circ5_1AltPos.x, circ5_1AltPos.y, circ5_1AltPos.z )
  circ5_2:SHR_sPosition( circ5_2AltPos.x, circ5_2AltPos.y, circ5_2AltPos.z )
  rod2_1:SHR_sPosition( rod2_1AltPos.x, rod2_1AltPos.y, rod2_1AltPos.z )
  rod2_2:SHR_sPosition( rod2_2AltPos.x, rod2_2AltPos.y, rod2_2AltPos.z )
  coroutine.yield()
  
  local i = 1
  while( i <= 2 )
  do
  
    if( arrays[ i ].ok < 0 ) and
      ( vec3.magnitude( vec3.subtract( Aster:SHR_gPosition(), arrays[ i ].circPos ) ) < PLAYER_DIST ) 
    then
      arrays[ i ].ok = 0
      Global:StatTrack( 1, false )
    
    end
  
    if( arrays[ i ].ok >= 0 and arrays[ i ].ok < HIGH_DIF )
    then
      local time = Global:TimeElapsed() / 100
      arrays[ i ].ok = arrays[ i ].ok + ( time )
      
      arrays[ i ].circAltRot.y = arrays[ i ].circAltRot.y + ( ROTATION_DIF * time )
      arrays[ i ].circAltPos.y = arrays[ i ].circAltPos.y + ( time )
      arrays[ i ].circ:SHR_sRotation( arrays[ i ].circAltRot.x, arrays[ i ].circAltRot.y, arrays[ i ].circAltRot.z )
      
      arrays[ i ].rodAltRot.y = arrays[ i ].rodAltRot.y + ( ROTATION_DIF * -time )
      arrays[ i ].rodAltPos.y = arrays[ i ].rodAltPos.y + ( -time )
      arrays[ i ].rod:SHR_sRotation( arrays[ i ].rodAltRot.x, arrays[ i ].rodAltRot.y, arrays[ i ].rodAltRot.z )
    
    else
      arrays[ i ].circ:SHR_sRotation( arrays[ i ].circRot.x, arrays[ i ].circRot.y, arrays[ i ].circRot.z )
      arrays[ i ].rod:SHR_sRotation( arrays[ i ].rodRot.x, arrays[ i ].rodRot.y, arrays[ i ].rodRot.z )
    
    end
    
    if( arrays[ i ].ok >= HIGH_DIF and arrays[ i ].ok < RAD_DIF )
    then
      local time = Global:TimeElapsed() / 30
      arrays[ i ].ok = arrays[ i ].ok + ( time )
      
      arrays[ i ].light:SHR_sPosition( arrays[ i ].circAltPos.x, arrays[ i ].circAltPos.y - 10, arrays[ i ].circAltPos.z )
      arrays[ i ].light:VIS_SetLightRadius( arrays[ i ].light:VIS_GetLightRadius() + time )
    
    end
  
    i = i + 1
  end
  
  if( arrays[ 1 ].ok >= RAD_DIF and arrays[ 2 ].ok >= RAD_DIF ) then
    Global:DeleteEntityByPtr( Entity:GetMyPointer() )
    
  end

end