Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
ee24adf8b2
|
|||
0b98ee540d
|
|||
9d02a1f570
|
168
vacuum_gambit.p8
168
vacuum_gambit.p8
@ -466,9 +466,7 @@ ship_m = mknew{
|
||||
-- xmin, xmax, ymin, ymax:
|
||||
-- movement constraints
|
||||
-- enforced by `constrain`.
|
||||
xmin = 0, xmax = 104,
|
||||
-- ymin, ymax default to nil
|
||||
-- pship needs more constraint
|
||||
xmin = 0, xmax = 104, ymin = 0, ymax = 120
|
||||
}
|
||||
|
||||
function ship_m:die()
|
||||
@ -510,6 +508,10 @@ function ship_m:brake_dist(v0)
|
||||
return (chunk_zone + overage * (tri_frames + 1)) * sgn(v0), (overage > 0) and tri_frames + 1 or tri_frames
|
||||
end
|
||||
|
||||
function ship_m:reset_bounds()
|
||||
self.xmin, self.xmax, self.ymin, self.ymax = 0, 104, 0, 120
|
||||
end
|
||||
|
||||
function ship_m:constrain(p, dp, pmin, pmax, want)
|
||||
if (not pmin) return want
|
||||
local v1, bd, bf, bp
|
||||
@ -615,6 +617,159 @@ function ship_m:refresh_shield()
|
||||
self.shield_refresh_ready = gframe + self.shieldcooldown
|
||||
end
|
||||
|
||||
-->8
|
||||
-- paths
|
||||
|
||||
-- destination: a point the
|
||||
-- center of a ship approaches
|
||||
destination = mknew{
|
||||
-- 0..1: a point on a line
|
||||
-- segment from the ship's
|
||||
-- flotilla spot to the player;
|
||||
-- -1 means center of play area
|
||||
anchor_frac=0,
|
||||
|
||||
-- (0, 1]: relative to max
|
||||
-- acceleration, how quickly
|
||||
-- should the ship accelerate
|
||||
-- to its destination?
|
||||
-- 0: park here instead, using
|
||||
-- constraint following
|
||||
accel_frac=1,
|
||||
|
||||
-- how far to lerp between the
|
||||
-- anchor and the screen bounds.
|
||||
-- this is the "destination"-y
|
||||
-- part of a destination.
|
||||
x_off_frac = 0,
|
||||
y_off_frac = 0
|
||||
}
|
||||
|
||||
function lerp(a, b, f)
|
||||
return (1-f)*a+b*f
|
||||
end
|
||||
|
||||
function destination:anchor(fx, fy)
|
||||
local af = self.anchor_frac
|
||||
if (af == -1) return 55,63
|
||||
return lerp(fx, primary_ship.x + 4, af), lerp(fy, primary_ship.y + 4, af)
|
||||
end
|
||||
|
||||
function destination:target_from(fx, fy)
|
||||
local rx, ry = self:anchor(fx, fy)
|
||||
local xf, yf = self.x_off_frac, self.y_off_frac
|
||||
if (xf < 0) rx = lerp(rx, 0, -xf)
|
||||
if (xf > 0) rx = lerp(rx, 112, xf)
|
||||
if (yf < 0) ry = lerp(ry, 0, -yf)
|
||||
if (yf > 0) ry = lerp(ry, 128, yf)
|
||||
return rx, ry
|
||||
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
|
||||
|
||||
path = mknew {
|
||||
loop_idx = 1,
|
||||
current_idx = 1,
|
||||
-- [1..n]: destinations
|
||||
}
|
||||
|
||||
function path:reset()
|
||||
self.loop_idx = 1
|
||||
self.current_idx = 1
|
||||
end
|
||||
|
||||
function path:target_from(fx, fy)
|
||||
return self[self.current_idx]:target_from(fx, fy)
|
||||
end
|
||||
|
||||
function path:update(x, y)
|
||||
local idx = self.current_idx
|
||||
if self[idx]:update(x, y) then
|
||||
if idx == #self then
|
||||
self.current_idx = self.loop_idx
|
||||
return true
|
||||
else
|
||||
self.current_idx = idx+1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-->8
|
||||
-- bullet and gun behaviors
|
||||
|
||||
@ -1089,7 +1244,6 @@ player = mknew(ship_m.new{
|
||||
ymomentum = 0,
|
||||
maxspd = 1.5, -- momentum cap
|
||||
thrust = 0.1875, -- momentum added from button
|
||||
ymin = 0, ymax = 120, -- stay on screen
|
||||
drag = 0.0625, -- momentum lost per frame
|
||||
act = function(self) -- fetch buttons
|
||||
local b,th = btn(),self.thrust
|
||||
@ -1329,8 +1483,14 @@ ship_skirmisher = mknew(ship_f.new{
|
||||
sparks = smokespark,
|
||||
sparkodds = 3,
|
||||
fire_off_y = 7,
|
||||
xmin = -8,
|
||||
xmax = 112,
|
||||
})
|
||||
|
||||
function ship_skirmisher:reset_bounds()
|
||||
self.xmin, self.xmax, self.ymin, self.ymax = -8, 112, 0, 120
|
||||
end
|
||||
|
||||
function rnd_spawn_loc()
|
||||
local x,y = flr(rnd(304)), flr(rnd(32))
|
||||
if (x<184) return x-40,-y-8
|
||||
|
Reference in New Issue
Block a user