segment prototype

This commit is contained in:
2025-07-05 20:18:31 -07:00
parent 9d02a1f570
commit 0b98ee540d

View File

@ -665,6 +665,85 @@ function destination:target_from(fx, fy)
return rx, ry return rx, ry
end end
segment = mknew{
-- On loop, where to set index?
loop_idx = 1,
-- current index
current_idx = 1,
-- how long does this segment
-- last? 0 is erroneous,
-- positive values are a frame
-- count, negative values are
-- a number of loops.
limit = -1,
-- remain: remaining steps until end
-- prev_x, prev_y: stored previous
-- location to figure out
-- approach/depart
-- dest_x, dest_y: stored
-- current destination to
-- figure out approach/depart
-- [1..n]: destinations
init = function(x)
x.remain = x.limit
end
}
function segment:reset()
self.current_idx = 1
self.remain = self.limit
self.prev_x = nil
self.prev_y = nil
self.was_approaching = false
end
function segment:target_from(fx, fy)
local rx, ry = self[self.current_idx]:target_from(fx, fy)
self.dest_x, self.dest_y = rx, ry
return rx, ry
end
function segment:update(x, y)
local rem = self.remain
-- frame check
if (rem > 0) rem -= 1
if self:should_step(x, y) then
if self.current_idx == #self then
self.current_idx = self.loop_idx
if (rem < 0) rem += 1
else
self.current_idx += 1
end
end
self.remain = rem
return rem != 0
end
function segment:should_step(x, y)
local dest_x, dest_y, ret = self.dest_x, self.dest_y, false
local dx1, dy1 = x - dest_x, y-dest_y
if abs(dx1) <= 4 and abs(dy1) <= 4 then
self.was_approaching = false
ret = true
end
if self.prev_x then
local dx0, dy0 = dest_x = self.prev_x - dest_x, self.prev_y - dest_y
if dx1 * dx1 + dy1 * dy1 > dx0 * dx0 + dy0 * dy0 then
if self.was_approaching then
self.was_approaching = false
ret = true
end
else
self.was_approaching = true
end
end
self.prev_x, self.prev_y = x, y
return ret
end
-->8 -->8
-- bullet and gun behaviors -- bullet and gun behaviors