Open sprunk opened 8 months ago
Closure test cases. for direction, "w", "n", "e" and "s" stand for west, north, east and south.
local spiralWithExtraParameters = Spring.Utilities.GetSpiralGenerator(12, 34, { step = 3, startingDirection = "n", clockwise = false })
local x1, z1 = spiralWithExtraParameters()
local x2, z2 = spiralWithExtraParameters()
local x3, z3 = spiralWithExtraParameters()
if x1 == 12 and z1 == 34
and x2 == 12 and z2 == 31
and x3 == 9 and z3 == 31 then
Spring.Echo("OK")
else
error("fail")
end
local defaultSpiral = Spring.Utilities.GetSpiralGenerator()
local spiralWithDefaultsExplicitlySpecified = Spring.Utilities.GetSpiralGenerator(0, 0, { step = 1, startingDirection = 0, clockwise = false })
for i = 1, 20 do
local xD, zD = defaultSpiral()
local xE, zE = spiralWithDefaultsExplicitlySpecified()
if xD ~= xE or zD ~= zE then
error("fail")
end
end
Spring.Echo("OK 2")
local angledSpiral = Spring.Utilities.GetSpiralGenerator(0, 0, { step = 2, startingDirection = math.tau / 6, clockwise = true })
local x1, z1 = angledSpiral()
local x2, z2 = angledSpiral()
if x1 ~= 1 or z1 ~= math.sqrt(3)
or x2 ~= math.sqrt(3) + 1 or z2 ~= math.sqrt(3) - 1 then
error("fail")
end
Spring.Echo("OK 3")
5089 added a spiral generator into the start unit spawner gadget. It sounds like generally useful math and doesn't really belong there. Extract it to a
Spring.Utilities
function, perhaps one that returns a closure that successively yields the spiral when called repeatedly.