-- propegate.levelgen
--http://bitfighter.org/luadocs/
tau = 2 * math.pi
scriptDescription = "Moves stuff outwards in a spiral!"

zone1 = levelgen:findObjectById(2)
zone2 = levelgen:findObjectById(4)
zone3 = levelgen:findObjectById(6)
zone4 = levelgen:findObjectById(8)
zone5 = levelgen:findObjectById(10)
zone6 = levelgen:findObjectById(12)
zone7 = levelgen:findObjectById(14)
zone8 = levelgen:findObjectById(16)
zone9 = levelgen:findObjectById(18)
zone10 = levelgen:findObjectById(20)
zone11 = levelgen:findObjectById(22)
zone12 = levelgen:findObjectById(24)
nex1 = levelgen:findObjectById(1)
nex2 = levelgen:findObjectById(3)
nex3 = levelgen:findObjectById(5)
nex4 = levelgen:findObjectById(7)
nex5 = levelgen:findObjectById(9)
nex6 = levelgen:findObjectById(11)
nex7 = levelgen:findObjectById(13)
nex8 = levelgen:findObjectById(15)
nex9 = levelgen:findObjectById(17)
nex10 = levelgen:findObjectById(19)
nex11 = levelgen:findObjectById(21)
nex12 = levelgen:findObjectById(23)
zonesToPropegate = {zone1, zone2, zone3, zone4, zone5, zone6, zone7, zone8, zone9, zone10, zone11, zone12}
NexusesToPropegate = {nex1, nex2, nex3, nex4, nex5, nex6, nex7, nex8, nex9, nex10, nex11, nex12}
distance = 150


function translateObjectGeom(inputGeom, pos) --This one too but thanks raptor anyway
    local outputGeom = {}
    -- Note that this assumes the first point in the geometry is the one to rotate about
    -- Our rotatePoint will always be the first in the input geometry
    vector = point.normalize(point.new(inputGeom[1].x, inputGeom[1].y-72.2))
    for index, value in ipairs(inputGeom) do
        local newPoint = point.new()
        table.insert(outputGeom, rotateAboutPoint(point.new(value.x+(vector.x * distance), value.y+(vector.y * distance)), 1))
    end
    
    return outputGeom
end
function rotateAboutPoint(inputPoint, angle) --there is an easier way than this.  see Geom on the luadox
    -- Trig!
    local rotationPoint = point.new(0,0)
    local cosAngle = math.cos(angle)
    local sinAngle = math.sin(angle)

    local x = rotationPoint.x + (((inputPoint.x-rotationPoint.x)*cosAngle) - ((inputPoint.y-rotationPoint.y)*sinAngle))
    local y = rotationPoint.y + (((inputPoint.x-rotationPoint.x)*sinAngle) + ((inputPoint.y-rotationPoint.y)*cosAngle))
    
    return point.new(x, y);
end

function propegateZone(zone)
	local newzone = GoalZone.new()
	newzone:setTeam(zone:getTeamIndex())
	newzone:setGeom(translateObjectGeom(zone:getGeom()))
	levelgen:addItem(newzone)
	Timer:scheduleOnce(function () propegateZone(newzone) end, 1000)
end
function propegateNex(zone)
	local newzone =  NexusZone.new()
	newzone:setGeom(translateObjectGeom(zone:getGeom()))
	levelgen:addItem(newzone)
	Timer:scheduleOnce(function () propegateNex(newzone) end, 1000)
end

  -----------------------------------------------------------//Main
function main()
	for index, zone in ipairs(zonesToPropegate) do
		Timer:scheduleOnce(function () propegateZone(zone) end, 1000)
	end
	for index, zone in ipairs(NexusesToPropegate) do
		Timer:scheduleOnce(function () propegateNex(zone) end, 1000)
	end
	for a = 1, 30 do  --I like a clean console
		logprint(" ")
	end
end