-- Beater.levelgen
tau = math.pi * 2

beater1 = {}
beater1["type"] = ResourceItem
beater1["center"] = point.new(765, 637.5) --this one changes when I add the items.  Oops.
beater1["trueCenter"] = point.new(765, 637.5)
beater1["length"] = 5 --how many to make + 1.
beater1["objects"] = {}
beater1["orbitTime"] = 2500
beater1["x"] = 0
beater1["y"] = 50 --a resourceItem is about this big.
beater1["orbitDirection"] = -1 -- positive for counter clockwise, negative for clockwise

beater2 = {}
beater2["type"] = ResourceItem
beater2["center"] = point.new(637.5, 1428) --this one changes when I add the items.  Oops.
beater2["trueCenter"] = point.new(637.5, 1428)
beater2["length"] = 5 --how many to make + 1.
beater2["objects"] = {}
beater2["orbitTime"] = 3000
beater2["x"] = 0
beater2["y"] = 50 --a resourceItem is about this big.
beater2["orbitDirection"] = 1

beater3 = {}
beater3["type"] = ResourceItem
beater3["center"] = point.new(1785, 408) --this one changes when I add the items.  Oops.
beater3["trueCenter"] = point.new(1785, 408)
beater3["length"] = 4 --how many to make + 1.
beater3["objects"] = {}
beater3["orbitTime"] = 5000
beater3["x"] = 0
beater3["y"] = 43 --a resourceItem is about this big.
beater3["orbitDirection"] = -1

beater4 = {}
beater4["type"] = ResourceItem
beater4["center"] = point.new(1351.5, 382) --this one changes when I add the items.  Oops.
beater4["trueCenter"] = point.new(1351.5, 382)
beater4["length"] = 3 --how many to make + 1.
beater4["objects"] = {}
beater4["orbitTime"] = 4000
beater4["x"] = 0
beater4["y"] = 43 --a resourceItem is about this big.
beater4["orbitDirection"] = 1

beater5 = {}
beater5["type"] = ResourceItem
beater5["center"] = point.new(765, -510) --this one changes when I add the items.  Oops.
beater5["trueCenter"] = point.new(765, -510)
beater5["length"] = 5 --how many to make + 1.
beater5["objects"] = {}
beater5["orbitTime"] = 3000
beater5["x"] = 0
beater5["y"] = 50 --a resourceItem is about this big.
beater5["orbitDirection"] = 1

beater6 = {}
beater6["type"] = ResourceItem
beater6["center"] = point.new(-765, -637.5) --this one changes when I add the items.  Oops.
beater6["trueCenter"] = point.new(-765, -637.5)
beater6["length"] = 5 --how many to make + 1.
beater6["objects"] = {}
beater6["orbitTime"] = 2500
beater6["x"] = 0
beater6["y"] = 50 --a resourceItem is about this big.
beater6["orbitDirection"] = -1 -- positive for counter clockwise, negative for clockwise

beater7 = {}
beater7["type"] = ResourceItem
beater7["center"] = point.new(-637.5, -1428) --this one changes when I add the items.  Oops.
beater7["trueCenter"] = point.new(-637.5, -1428)
beater7["length"] = 5 --how many to make + 1.
beater7["objects"] = {}
beater7["orbitTime"] = 3000
beater7["x"] = 0
beater7["y"] = 50 --a resourceItem is about this big.
beater7["orbitDirection"] = 1

beater8 = {}
beater8["type"] = ResourceItem
beater8["center"] = point.new(-1785, -408) --this one changes when I add the items.  Oops.
beater8["trueCenter"] = point.new(-1785, -408)
beater8["length"] = 4 --how many to make + 1.
beater8["objects"] = {}
beater8["orbitTime"] = 5000
beater8["x"] = 0
beater8["y"] = 43 --a resourceItem is about this big.
beater8["orbitDirection"] = -1

beater9 = {}
beater9["type"] = ResourceItem
beater9["center"] = point.new(-1351.5, -382) --this one changes when I add the items.  Oops.
beater9["trueCenter"] = point.new(-1351.5, -382)
beater9["length"] = 3 --how many to make + 1.
beater9["objects"] = {}
beater9["orbitTime"] = 4000
beater9["x"] = 0
beater9["y"] = 43 --a resourceItem is about this big.
beater9["orbitDirection"] = 1

beater10 = {}
beater10["type"] = ResourceItem
beater10["center"] = point.new(-765, 510) --this one changes when I add the items.  Oops.
beater10["trueCenter"] = point.new(-765, 510)
beater10["length"] = 5 --how many to make + 1.
beater10["objects"] = {}
beater10["orbitTime"] = 3000
beater10["x"] = 0
beater10["y"] = 50 --a resourceItem is about this big.
beater10["orbitDirection"] = 1

beater11 = {}
beater11["type"] = ResourceItem
beater11["center"] = point.new(0, 0) --this one changes when I add the items.  Oops.
beater11["trueCenter"] = point.new(0, 0)
beater11["length"] = 8 --how many to make + 1.
beater11["objects"] = {}
beater11["orbitTime"] = 6000
beater11["x"] = 0
beater11["y"] = 50 --a resourceItem is about this big.
if(math.random(2) == 1) then
	beater11["orbitDirection"] = 1
else
	beater11["orbitDirection"] = -1
end

beaterArray = {beater1, beater2, beater3, beater4, beater5, beater6, beater7, beater8, beater9, beater10, beater11}

origin = point.zero -- The origin

function doBeaters(deltaTime)
	for index, value in ipairs(beaterArray)
	do
		for dex, val in ipairs(value["objects"]) --orbit 
		do
			--[[ kaen:
				well, it's kind of hairy
				but if you understand vector math it's very simple
				the position is just a point on a circle at time t
				well then, we just cauclate a new destination point that will guide the ship around in a circle
				the velocity is perpendicular to (satPos - center)
				it has magnitude equal to the orbit's circumference / period --period being like, orbit time
				(because velocity * period equals circumference)
			--]]
			--[[
				centered on a coordinate axis
				and draw a right triangle like this:
				https://en.wikipedia.org/wiki/File:Unit_circle.svg
				imagine the ship is at the dot
				then increase t a little bit
				that formula I pasted above will tell you where the next x,y is
				the legnth of the diagonal line is labeled in that diagram as 1
				but in orbitbot it is orbitradius

			--]]
			local t = getMachineTime() % (value["orbitTime"]) / value["orbitTime"]
 
			local center = value["trueCenter"]
			local radius = point.distanceTo(val["loc"], center)
			
			local pos = center + point.new(math.sin((t + 1/5) * tau * value["orbitDirection"]), math.cos((t + 1/5) * tau * value["orbitDirection"])) * radius
			local unitVec = point.normalize(center - pos)
			val["item"]:setVel(point.new(value["orbitDirection"] * -unitVec.y, value["orbitDirection"] * unitVec.x) * tau * radius / (value["orbitTime"] / 1000))
			val["item"]:setPos(pos)
		end
	end
end

function onTick(deltaTime)
	doBeaters(deltaTime)
end

function main()
	subscribe(Event.Tick)
   
	for a = 1, 50 do  --I like a clean console
		logprint(" ")
	end
   
	for index, value in ipairs(beaterArray) --Make the items
	do
		for i = 1, value["length"]
		do
			item = {}
			item["loc"] = value["center"]
			item["item"] = value["type"].new()
			item["item"]:setPos(value["center"])
			levelgen:addItem(item["item"])
			table.insert(value["objects"], item)
			value["center"] = point.new(value["center"].x + value["x"], value["center"].y + value["y"])
		end
	end
	Timer:scheduleRepeating(function() 
		beater11["orbitDirection"] = beater11["orbitDirection"] * -1
	end, 10000)
end