paths sort of work but not really

figure out why they do not show the expected patterns
This commit is contained in:
2025-07-06 20:02:21 -07:00
parent 6efd05febe
commit 10aef3b1ed

View File

@ -686,21 +686,21 @@ segment = mknew{
-- dest_x, dest_y: stored -- dest_x, dest_y: stored
-- current destination to -- current destination to
-- figure out approach/depart -- figure out approach/depart
-- [1..n]: destinations -- dests[1..n]: destinations
init = function(x) init = function(x)
x.init = segment.reset x.init = segment.reset
mknew(x) mknew(x)
if not x.mirror then if not x.mirror then
local mirrored = {mirror=x, init=segment.reset} local mdests = {}
for i, v in ipairs(x) do for i, v in ipairs(x.dests) do
mirrored[i] = destination.new{ mdests[i] = destination.new{
anchor_frac=v.anchor_frac, anchor_frac=v.anchor_frac,
accel_frac=v.accel_frac, accel_frac=v.accel_frac,
x_off_frac=-v.x_off_frac, x_off_frac=-v.x_off_frac,
y_off_frac=v.y_off_frac, y_off_frac=v.y_off_frac,
} }
end end
x.mirror = mknew(x.new(mirrored)) x.mirror = mknew(x.new{mirror=x, init=segment.reset, dests=mdests})
end end
end end
} }
@ -714,7 +714,7 @@ function segment:reset()
end end
function segment:target_from(fx, fy) function segment:target_from(fx, fy)
local rx, ry, af = self[self.current_idx]:target_from(fx, fy) local rx, ry, af = self.dests[self.current_idx]:target_from(fx, fy)
self.dest_x, self.dest_y = rx, ry self.dest_x, self.dest_y = rx, ry
return rx, ry, af return rx, ry, af
end end
@ -725,7 +725,8 @@ function segment:update(x, y)
if (rem > 0) rem -= 1 if (rem > 0) rem -= 1
if self:should_step(x, y) then if self:should_step(x, y) then
if self.current_idx == #self then self.prev_x, self.prev_y = false, false
if self.current_idx == #self.dests then
self.current_idx = self.loop_idx self.current_idx = self.loop_idx
if (rem < 0) rem += 1 if (rem < 0) rem += 1
else else
@ -760,22 +761,21 @@ end
path = mknew { path = mknew {
loop_idx = 1, loop_idx = 1,
-- [1..n]: destinations -- segs[1..n]: segments or subpaths
init=function(x) init=function(x)
if not x.mirror then if not x.mirror then
local mirrored = { local msegs = {}
mirror = x, for i, v in ipairs(x.segs) do
loop_idx = x.loop_idx, msegs[i]=v.mirror
}
for i, v in ipairs(x) do
mirrored[i]=v.mirror
end end
x.mirror = path.new(mirrored) x.mirror = path.new{mirror=x, loop_idx=x.loop_idx, segs=msegs}
end end
x.init = function(y) x.init = function(y)
for i, v in ipairs(x) do local ysegs = {}
y[i]=v.new{} for i, v in ipairs(x.segs) do
ysegs[i]=v.new{}
end end
y.segs = ysegs
y:reset() y:reset()
end end
mknew(x) mknew(x)
@ -787,13 +787,13 @@ function path:reset()
end end
function path:target_from(fx, fy) function path:target_from(fx, fy)
return self[self.current_idx]:target_from(fx, fy) return self.segs[self.current_idx]:target_from(fx, fy)
end end
function path:update(x, y) function path:update(x, y)
local idx = self.current_idx local idx = self.current_idx
if self[idx]:update(x, y) then if self.segs[idx]:update(x, y) then
if idx == #self then if idx == #self.segs then
self.current_idx = self.loop_idx self.current_idx = self.loop_idx
return true return true
else else
@ -1498,6 +1498,7 @@ ship_f = mknew(ship_m.new{
}) })
diamond_loop = segment.new{ diamond_loop = segment.new{
dests = {
destination.new{ destination.new{
x_off_frac=0.25 x_off_frac=0.25
}, destination.new{ }, destination.new{
@ -1507,6 +1508,14 @@ diamond_loop = segment.new{
}, destination.new { }, destination.new {
y_off_frac = -0.25 y_off_frac = -0.25
}, },
},
}
diamond_bounce = path.new{
segs = {
diamond_loop,
diamond_loop.mirror,
},
} }
ship_mook = mknew(ship_f.new{ ship_mook = mknew(ship_f.new{
@ -1530,7 +1539,7 @@ ship_skirmisher = mknew(ship_f.new{
fire_off_y = 7, fire_off_y = 7,
xmin = -8, xmin = -8,
xmax = 112, xmax = 112,
path = diamond_loop, path = diamond_bounce,
}) })
function ship_skirmisher:reset_bounds() function ship_skirmisher:reset_bounds()