-- castleblizzard.levelgen
-- blizzard.levelgen
objects = {}

textitem = nil

enabled = true
waittime = 0

function onShipEnteredZone(ship, zone, zoneType, zoneId)
  if(zoneId == 1) then
   if waittime <= 0 then
     if enabled then
       enabled = false
       textitem:setText("OFF")
       for index, object in ipairs(objects) do
         object:setVel(point.new(0, 0))
       end
     else
       textitem:setText("ON")
       enabled = true
     end
     textitem:setGeom(textitem:getGeom())  -- this line is to properly send packUpdate to clients
     waittime = 500
   end
 end
end

function onTick(timePassed)
  if waittime >= 0 then
    waittime = waittime - timePassed
  end

  if enabled then
    for index, object in ipairs(objects) do
      vel = object:getVel()
      pos = object:getPos()
      a = math.atan2(pos.x, pos.y)
      object:setVel(point.new(vel.x - math.cos(a) * 2 * timePassed, vel.y + math.sin(a) * 2 * timePassed))
    end
  end
end

function main()
  for a = 1, 100 do
    objects[a] = ResourceItem.new()
    objects[a]:setPos(point.new(math.cos(a * 2.3) * (750 + a), math.sin(a * 2.3) * (750 + a)))
    levelgen:addItem(objects[a])
    a=a+1
  end

  textitem = TextItem.new()
  textitem:setText("ON")
  textitem:setGeom(point.new(-51, 153), point.new(51, 153))
  levelgen:addItem(textitem)

  enabled = true
  waittime = 0
  subscribe(Event.Tick)
  subscribe(Event.ShipEnteredZone)
end