paths and segments are now factories instead

the factory/instance deliniation is not clean but it _is_ efficient.
This commit is contained in:
2025-07-06 18:09:56 -07:00
parent 797818214a
commit 8533720114

View File

@ -686,15 +686,28 @@ segment = mknew{
-- figure out approach/depart -- figure out approach/depart
-- [1..n]: destinations -- [1..n]: destinations
init = function(x) init = function(x)
x.remain = x.limit x.init = segment.reset
mknew(x)
if not x.mirror then
local mirrored = {mirror=x}
for i, v in ipairs(x) do
mirrored[i] = destination.new{
anchor_frac=v.anchor_frac
accel_frac=v.accel_frac
x_off_frac=-v.x_off_frac
y_off_frac=v.y_off_frac
}
end
x.mirror = x.new(mirrored)
end
end end
} }
function segment:reset() function segment:reset()
self.current_idx = 1 self.current_idx = 1
self.remain = self.limit self.remain = self.limit
self.prev_x = nil self.prev_x = false
self.prev_y = nil self.prev_y = false
self.was_approaching = false self.was_approaching = false
end end
@ -745,12 +758,29 @@ end
path = mknew { path = mknew {
loop_idx = 1, loop_idx = 1,
current_idx = 1,
-- [1..n]: destinations -- [1..n]: destinations
init=function(x)
if not x.mirror then
local mirrored = {
mirror = x,
loop_idx = x.loop_idx
}
for i, v in ipairs(x) do
mirrored[i]=v.mirror
end
x.mirror = path.new(mirrored)
end
x.init = function(y)
for i, v in ipairs(x) do
y[i]=v.new{}
end
y:reset()
end
mknew(x)
end,
} }
function path:reset() function path:reset()
self.loop_idx = 1
self.current_idx = 1 self.current_idx = 1
end end