fix paths
segment/path update logic was somewhat backwards. the result is definitely good enough for the kind of movements I want!
This commit is contained in:
		| @@ -653,17 +653,17 @@ end | |||||||
|  |  | ||||||
| function destination:anchor(fx, fy) | function destination:anchor(fx, fy) | ||||||
|  local af = self.anchor_frac |  local af = self.anchor_frac | ||||||
|  if (af == -1) return 55,63 |  if (af == -1) return 51,59 | ||||||
|  return lerp(fx, primary_ship.x + 4, af), lerp(fy, primary_ship.y + 4, af) |  return lerp(fx, primary_ship.x, af), lerp(fy, primary_ship.y, af) | ||||||
| end | end | ||||||
|  |  | ||||||
| function destination:target_from(fx, fy) | function destination:target_from(fx, fy) | ||||||
|  local rx, ry = self:anchor(fx, fy) |  local rx, ry = self:anchor(fx, fy) | ||||||
|  local xf, yf = self.x_off_frac, self.y_off_frac |  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, 0, -xf) | ||||||
|  if (xf > 0) rx = lerp(rx, 112, xf) |  if (xf > 0) rx = lerp(rx, 104, xf) | ||||||
|  if (yf < 0) ry = lerp(ry, 0, -yf) |  if (yf < 0) ry = lerp(ry, 0, -yf) | ||||||
|  if (yf > 0) ry = lerp(ry, 128, yf) |  if (yf > 0) ry = lerp(ry, 120, yf) | ||||||
|  return rx, ry, self.accel_frac |  return rx, ry, self.accel_frac | ||||||
| end | end | ||||||
|  |  | ||||||
| @@ -725,7 +725,7 @@ 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 | ||||||
|   self.prev_x, self.prev_y = false, false |   self.prev_x, self.prev_y, self.was_approaching = false, false, false | ||||||
|   if self.current_idx == #self.dests then |   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 | ||||||
| @@ -738,25 +738,21 @@ function segment:update(x, y) | |||||||
| end | end | ||||||
|  |  | ||||||
| function segment:should_step(x, y) | function segment:should_step(x, y) | ||||||
|  local dest_x, dest_y, ret = self.dest_x, self.dest_y, false |  local dest_x, dest_y = self.dest_x, self.dest_y | ||||||
|  local dx1, dy1 = x - dest_x, y-dest_y |  local dx1, dy1 = x - dest_x, y-dest_y | ||||||
|  if abs(dx1) <= 4 and abs(dy1) <= 4 then |  if (abs(dx1) <= 1.5 and abs(dy1) <= 1.5) return true | ||||||
|   self.was_approaching = false |  | ||||||
|   ret = true |  | ||||||
|  end |  | ||||||
|  if self.prev_x then |  if self.prev_x then | ||||||
|   local dx0, dy0 = self.prev_x - dest_x, self.prev_y - dest_y |   local dx0, dy0 = self.prev_x - dest_x, self.prev_y - dest_y | ||||||
|   if dx1 * dx1 + dy1 * dy1 > dx0 * dx0 + dy0 * dy0 then |   local d12, d02 = dx1 * dx1 + dy1 * dy1, dx0 * dx0 + dy0 * dy0 | ||||||
|    if self.was_approaching then |   if d12 > d02 then | ||||||
|     self.was_approaching = false |    if (self.was_approaching) return true | ||||||
|     ret = true |  | ||||||
|    end |  | ||||||
|   else |   else | ||||||
|  |    if (d12 == d02 and self.was_approaching) return true | ||||||
|    self.was_approaching = true |    self.was_approaching = true | ||||||
|   end |   end | ||||||
|  end |  end | ||||||
|  self.prev_x, self.prev_y = x, y |  self.prev_x, self.prev_y = x, y | ||||||
|  return ret |  return false | ||||||
| end | end | ||||||
|  |  | ||||||
| path = mknew { | path = mknew { | ||||||
| @@ -784,6 +780,9 @@ path = mknew { | |||||||
|  |  | ||||||
| function path:reset() | function path:reset() | ||||||
|  self.current_idx = 1 |  self.current_idx = 1 | ||||||
|  |  for s in all(self.segs) do | ||||||
|  |   s:reset() | ||||||
|  |  end | ||||||
| end | end | ||||||
|  |  | ||||||
| function path:target_from(fx, fy) | function path:target_from(fx, fy) | ||||||
| @@ -791,15 +790,17 @@ function path:target_from(fx, fy) | |||||||
| end | end | ||||||
|  |  | ||||||
| function path:update(x, y) | function path:update(x, y) | ||||||
|  local idx = self.current_idx |  local idx, segs = self.current_idx, self.segs | ||||||
|  if self.segs[idx]:update(x, y) then |  if not segs[idx]:update(x, y) then | ||||||
|   if idx == #self.segs then |   segs[idx]:reset() | ||||||
|  |   if idx == #segs then | ||||||
|    self.current_idx = self.loop_idx |    self.current_idx = self.loop_idx | ||||||
|    return true |    return | ||||||
|   else |   else | ||||||
|    self.current_idx = idx+1 |    self.current_idx = idx+1 | ||||||
|   end |   end | ||||||
|  end |  end | ||||||
|  |  return true | ||||||
| end | end | ||||||
|  |  | ||||||
| -->8 | -->8 | ||||||
| @@ -1511,9 +1512,19 @@ diamond_loop = segment.new{ | |||||||
|  }, |  }, | ||||||
| } | } | ||||||
|  |  | ||||||
|  | park = segment.new{ | ||||||
|  |  dests = { | ||||||
|  |   destination.new{ | ||||||
|  |    accel_frac = 0 | ||||||
|  |   } | ||||||
|  |  } | ||||||
|  | } | ||||||
|  |  | ||||||
| diamond_bounce = path.new{ | diamond_bounce = path.new{ | ||||||
|  segs = { |  segs = { | ||||||
|  |   park, | ||||||
|   diamond_loop, |   diamond_loop, | ||||||
|  |   park, | ||||||
|   diamond_loop.mirror, |   diamond_loop.mirror, | ||||||
|  }, |  }, | ||||||
| } | } | ||||||
| @@ -1543,13 +1554,11 @@ ship_skirmisher = mknew(ship_f.new{ | |||||||
| }) | }) | ||||||
|  |  | ||||||
| function ship_skirmisher:reset_bounds() | function ship_skirmisher:reset_bounds() | ||||||
|  self.xmin, self.xmax, self.ymin, self.ymax = -8, 112, 0, 120 |  self.xmin, self.xmax, self.ymin, self.ymax = -8, 112, 0, 128 | ||||||
| end | end | ||||||
|  |  | ||||||
| function ship_skirmisher:act() | function ship_skirmisher:act() | ||||||
|  local tx, ty, af = self.path:target_from(self.want_x+4, self.want_y+4) |  local tx, ty, af = self.path:target_from(self.want_x, self.want_y) | ||||||
|  tx -= 4 |  | ||||||
|  ty -= 4 |  | ||||||
|  if af <= 0 then |  if af <= 0 then | ||||||
|   self.xmin, self.xmax, self.ymin, self.ymax = tx,tx,ty,ty |   self.xmin, self.xmax, self.ymin, self.ymax = tx,tx,ty,ty | ||||||
|   return 0,0 |   return 0,0 | ||||||
| @@ -1562,7 +1571,7 @@ end | |||||||
|  |  | ||||||
| function ship_skirmisher:move() | function ship_skirmisher:move() | ||||||
|  if (not ship_f.move(self)) return |  if (not ship_f.move(self)) return | ||||||
|  if (not self.path:update(self.x+4, self.y+4)) self.path:reset() |  if (not self.path:update(self.x, self.y)) self.path:reset() | ||||||
|  return true |  return true | ||||||
| end | end | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user