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


Global:StatTrack( 2, true )


local ASSETS_SOUNDS = "../../Assets/Sounds/"
local TRIGGER_LIGHT = "triggerlight"
local music = { [ 1 ] = { }, [ 2 ] = { } }


if( 1 )
then
  local i = 1
  local areas = 2
  while( i <= areas )
  do
    local j = 1
    while( j <= 4 ) 
    do
      music[ i ][ j ] = ASSETS_SOUNDS .. TRIGGER_LIGHT .. i .. "_" .. j .. ".ogg"
    
      j = j + 1
    end
  
    i = i + 1
  end
  
end


local Aster = Player:gAster()
local myData = Entity:Data()

Entity:SPT_RefreshNumbers( myData )

local pos = Entity:SHR_gPosition()
local colors = { r = 1.0, g = 1.0, b = 1.0 }
local PLAYER_DIST = 25
local RAD_DEFAULT = 10
local RAD_INCR = 25
local OK_STOP = .5
local OK_GO = -1

if( myData.rMin ~= nil and myData.rMax ~= nil ) then
  colors.r = ( math.random( myData.rMin, myData.rMax ) ) / 100

end

if( myData.gMin ~= nil and myData.gMax ~= nil ) then
  colors.g = ( math.random( myData.gMin, myData.gMax ) ) / 100

end

if( myData.bMin ~= nil and myData.bMax ~= nil ) then
  colors.b = ( math.random( myData.bMin, myData.bMax ) ) / 100

end

if( myData.player_dist ~= nil ) then
  PLAYER_DIST = myData.player_dist
  
end

if( myData.rad_default ~= nil ) then
  RAD_DEFAULT = myData.rad_default
  
end

if( myData.rad_incr ~= nil ) then
  RAD_INCR = myData.rad_incr
  
end

if( myData.ok_stop ~= nil ) then
  OK_STOP = myData.ok_stop
  
end

local LIGHTFILE = "../../Assets/Xml/entities/e_DefaultLight.xml"
local light = Global:CreateEntityAt( LIGHTFILE, pos.x, pos.y, pos.z, 0, 0, 0 )
light:VIS_SetLightColor( colors.r, colors.g, colors.b )
light:VIS_SetLightRadius( RAD_DEFAULT )
-- Hackiest hack in the history of hacks
Editor:AttachToIsland( 0, light:GetMyPointer(), LIGHTFILE )

while( 1 ) 
do
  coroutine.yield()
  
  if( OK_GO < 0 ) and
    ( vec3.magnitude( vec3.subtract( Aster:SHR_gPosition(), light:SHR_gPosition() ) ) < PLAYER_DIST ) 
  then
    Global:StatTrack( 2, false )
    OK_GO = 0
    local rand = math.random( 1, 4 )
    Global:PlaySound2D( music[ Global:CurrArea() ][ rand ] )
    
  
  elseif( OK_GO >= OK_STOP ) then
    --Global:DeleteEntityByPtr( Entity:GetMyPointer() )
  
  end
  
  if( OK_GO >= 0 and OK_GO <= OK_STOP )
  then
    local time = Global:TimeElapsed() / 1000
    
    light:VIS_SetLightRadius( light:VIS_GetLightRadius() + ( RAD_INCR * time ) )
    OK_GO = OK_GO + time
  
  end
  
  pos = Entity:SHR_gPosition()
  light:SHR_sPosition( pos.x, pos.y, pos.z )

end