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


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


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


--------------------------------------------------------------------------------
-- Pads data
--------------------------------------------------------------------------------

local p_HIGH_DIF = 3
local p_HIGH_DIF2 = p_HIGH_DIF + p_HIGH_DIF
local p_HIGH_DIF3 = p_HIGH_DIF2 + p_HIGH_DIF
local p_HIGH_DIF4 = p_HIGH_DIF3 + 1
local p_OK_GO = -1
local p_OFF_COUNTER = 0
local p_PLAYER_DIST = 100
local p_ABOVE_EACH1 = 100
local p_ABOVE_EACH2 = 50

local macaroni2 = Global:FindEntityByName( "macaroni02", false )
local macaroni2Pos = macaroni2:SHR_gPosition()

file = "island_parts/pad01.xml"
local pad1_1    = Global:CreateEntityAt( ASSETS_XML .. file, macaroni2Pos.x + 141, macaroni2Pos.y + p_ABOVE_EACH2 * 1, macaroni2Pos.z +  -56, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, pad1_1:GetMyPointer(), ASSETS_XML .. file )

local pad1_2    = Global:CreateEntityAt( ASSETS_XML .. file, macaroni2Pos.x + 234, macaroni2Pos.y + p_ABOVE_EACH2 * 2, macaroni2Pos.z +  -85, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, pad1_2:GetMyPointer(), ASSETS_XML .. file )

local pad1_3    = Global:CreateEntityAt( ASSETS_XML .. file, macaroni2Pos.x + 201, macaroni2Pos.y + p_ABOVE_EACH2 * 3, macaroni2Pos.z + -170, 0, 0, 0 )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, pad1_3:GetMyPointer(), ASSETS_XML .. file )

local pad1_1FinPos = pad1_1:SHR_gPosition()
local pad1_2FinPos = pad1_2:SHR_gPosition()
local pad1_3FinPos = pad1_3:SHR_gPosition()

local pad1_1AltPos   = vec3.construct( pad1_1FinPos.x, pad1_1FinPos.y - p_ABOVE_EACH1, pad1_1FinPos.z )
local pad1_2AltPos   = vec3.construct( pad1_1FinPos.x, pad1_2FinPos.y - p_ABOVE_EACH1, pad1_1FinPos.z )
local pad1_3AltPos   = vec3.construct( pad1_1FinPos.x, pad1_3FinPos.y - p_ABOVE_EACH1, pad1_1FinPos.z )

local pads =
  { 
    [ 1 ] = { pad = pad1_1,
              padAltPos = pad1_1AltPos
            },
    [ 2 ] = { pad = pad1_2,
              padAltPos = pad1_2AltPos,
            },
    [ 3 ] = { pad = pad1_3,
              padAltPos = pad1_3AltPos,
            }
  }
  
local offer
local offerMult


local function updatePads( off, time )
  off = vec3.multiply( off, time )
  
  local i = 1
  local j = table.maxn( pads )
  while( i <= j )
  do
    if( pads[ i ] )
    then
      pads[ i ].padAltPos = vec3.add( pads[ i ].padAltPos, off )
      
    end
    
    i = i + 1
  end
    
end


--------------------------------------------------------------------------------
-- Sphere data
--------------------------------------------------------------------------------

local s_HIGH_DIF = 7
local s_HIGH_MOVE = 50 / s_HIGH_DIF
local s_OK_GO = -1
local s_PLAYER_DIST = 100

local sphere1 = Global:FindEntityByName( "sphere01", false )
local sphere1Pos = sphere1:SHR_gPosition()

local sphereLight = nil
local lightRedAdj = 5
local lightBluAdj = 9
local lightRed = 10
local lightBlu = 150
local lightOffs = { [ 1 ] = { color = lightRed,
                              adj   = lightRedAdj },
                    [ 2 ] = { color = lightBlu,
                              adj   = lightBluAdj }
                  }


--------------------------------------------------------------------------------
-- Update
--------------------------------------------------------------------------------

while( 1 ) 
do
  
  local i = 1
  local j = table.maxn( pads )
  local pos
  while( i <= j )
  do
    if( pads[ i ] )
    then
      pos = pads[ i ].padAltPos
      pads[ i ].pad:SHR_sPosition( pos.x, pos.y, pos.z )
    
    end
  
    i = i + 1
  end
  sphere1:SHR_sPosition( sphere1Pos.x, sphere1Pos.y, sphere1Pos.z )
  coroutine.yield()

  AsterPos = Aster:SHR_gPosition()
  
  
  ------------------------------------------------------------------------------
  -- Pads update
  ------------------------------------------------------------------------------
  
  if( p_OK_GO < 0 ) and
    ( vec3.magnitude( vec3.subtract( AsterPos, macaroni2Pos ) ) < p_PLAYER_DIST ) 
  then
    Global:StatTrack( 1, false )
    p_OK_GO = 0
    
  end
  
  if( p_OK_GO >= 0 and p_OK_GO <= p_HIGH_DIF4 )
  then
    if( p_OK_GO <= p_HIGH_DIF and p_OFF_COUNTER == 0 ) 
    then
      offer = vec3.normalize( vec3.subtract( pad1_1FinPos, pads[ 1 ].padAltPos ) )
      offerMult = ( p_ABOVE_EACH1 / p_HIGH_DIF )
      
      p_OFF_COUNTER = 1
      
    elseif( p_OK_GO >= p_HIGH_DIF and p_OFF_COUNTER == 1 )
    then
      file = "entities/e_TriggerLight.xml" 
      local vis = Global:CreateEntityAt( ASSETS_XML .. file, pad1_1FinPos.x, pad1_1FinPos.y + 15, pad1_1FinPos.z, 0, 0, 0 )
      -- Hackiest hack in the history of hacks
      Editor:AttachToIsland( 0, vis:GetMyPointer(), ASSETS_XML .. file )
      
      pads[ 1 ] = nil
      
      offer = vec3.normalize( vec3.subtract( pad1_2FinPos, pads[ 2 ].padAltPos ) )
      offerMult = ( p_ABOVE_EACH2 / p_HIGH_DIF )
      
      p_OFF_COUNTER = 2
    
    elseif( p_OK_GO >= p_HIGH_DIF2 and p_OFF_COUNTER == 2 )
    then
      local vis = Global:CreateEntityAt( ASSETS_XML .. file, pad1_2FinPos.x, pad1_2FinPos.y + 15, pad1_2FinPos.z, 0, 0, 0 )
      -- Hackiest hack in the history of hacks
      Editor:AttachToIsland( 0, vis:GetMyPointer(), ASSETS_XML .. file )
      
      pads[ 2 ] = nil
      
      offer = vec3.normalize( vec3.subtract( pad1_3FinPos, pads[ 3 ].padAltPos ) )
      offerMult = ( p_ABOVE_EACH2 / p_HIGH_DIF )
      
      OFF_COUNTER = 3
    
    elseif( p_OK_GO >= p_HIGH_DIF3 and p_OFF_COUNTER == 3 )
    then
      local vis = Global:CreateEntityAt( ASSETS_XML .. file, pad1_3FinPos.x, pad1_3FinPos.y + 15, pad1_3FinPos.z, 0, 0, 0 )
      -- Hackiest hack in the history of hacks
      Editor:AttachToIsland( 0, vis:GetMyPointer(), ASSETS_XML .. file )
      
      pads[ 3 ] = nil
      
      OFF_COUNTER = 4
    
    end
    
    local time = ( Global:TimeElapsed() / 1000 )
    p_OK_GO = p_OK_GO + time
    
    updatePads( offer, time * offerMult )
  end
  
  
  ------------------------------------------------------------------------------
  -- Sphere update
  ------------------------------------------------------------------------------
  
  if( s_OK_GO < 0 ) and
    ( vec3.magnitude( vec3.subtract( AsterPos, sphere1Pos ) ) < s_PLAYER_DIST ) 
  then
    Global:StatTrack( 1, false )
    s_OK_GO = 0
    
  end
  
  if( s_OK_GO >= 0 and s_OK_GO <= s_HIGH_DIF )
  then
    local time = Global:TimeElapsed() / 1000
    s_OK_GO = s_OK_GO + time
    
    sphere1Pos.y = sphere1Pos.y + ( time * s_HIGH_MOVE )
  
  end
  
  if( sphereLight == nil and s_OK_GO >= s_HIGH_DIF )
  then
    local file = "entities/e_DefaultLight.xml" 
    sphereLight = Global:CreateEntityAt( ASSETS_XML .. file, sphere1Pos.x, sphere1Pos.y - 40, sphere1Pos.z, 0, 0, 0 )
    -- Hackiest hack in the history of hacks
    Editor:AttachToIsland( 0, sphereLight:GetMyPointer(), ASSETS_XML .. file )
  
  end
  
  if( sphereLight )
  then
    local time = Global:TimeElapsed() / 1000
    
    local radius = sphereLight:VIS_GetLightRadius()
    if( radius < 100 )
    then
      radius = radius + ( time * 100 )
      sphereLight:VIS_SetLightRadius( radius )
      
    end
    
    local i = 1
    while( i <= 2 )
    do
      local color = lightOffs[ i ].color
      color = color + ( lightOffs[ i ].adj * time )
      if( color >= 240 or color <= 10 ) then
        lightOffs[ i ].adj = -lightOffs[ i ].adj
        
      end
      lightOffs[ i ].color = color
    
      i = i + 1
    end
    
    
    sphereLight:VIS_SetLightColor( lightOffs[ 1 ].color / 255, 0, lightOffs[ 2 ].color / 255 )
    
  end
  
end