Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions crates/core/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,7 @@ impl Engine {
self.position.x += self.velocity.x * dt;

if let Some(wall) = wall_reached(self.position.x, self.velocity.x, snapshot)
.or_else(|| dock_side_reached(self.position, snapshot))
.or_else(|| dock_side_reached(self.position, self.velocity.x, snapshot))
{
// Arriving at a screen edge sideways is a catch, not a stop.
// It also keeps the sprite inside the displays. The Dock's
Expand Down Expand Up @@ -1160,7 +1160,7 @@ impl Engine {
// longer than the Dock is wide would cross it unseen; the
// snapshot assembler caps `elapsed_ms` at one poll interval,
// which is what keeps a step small.
if let Some(side) = dock_side_reached(self.position, snapshot) {
if let Some(side) = dock_side_reached(self.position, self.velocity.x, snapshot) {
self.position.x = side;
self.velocity = Point::default();
return Some(Contact::Wall);
Expand Down Expand Up @@ -1837,6 +1837,56 @@ mod tests {
panic!("the walk never reached the Dock's top: {previous:?}");
}

/// The Dock's side is a wall for a sprite walking into it (#176), and not
/// for one that has just walked off the top of it. Catching that one put it
/// back on the top to walk off again, so everything beyond the Dock — the
/// screen edge it was heading for included — stayed unreachable for as long
/// as the app ran. #361.
#[test]
fn a_walk_off_the_dock_top_carries_on_past_it_rather_than_climbing_back() {
let dock = dock();
let mut engine = a_character_at(Point { x: 700.0, y: 0.0 });
settle(&mut engine, &dock_snapshot(100));

// Thrown leftwards into the Dock's right side, which it climbs, leaving
// it on the top facing left with the far edge ahead of it.
engine.tick(&WorldSnapshot {
cursor: Point { x: 700.0, y: 760.0 },
verbs: vec![Verb::Grab],
..dock_snapshot(16)
});
engine.tick(&WorldSnapshot {
verbs: vec![Verb::Throw {
velocity: Point { x: -400.0, y: 0.0 },
}],
..dock_snapshot(16)
});
let on_top = settle(&mut engine, &dock_snapshot(16));
assert_eq!(
(on_top.state, on_top.position.y),
(State::Perched, dock.y),
"the throw is meant to leave it on the Dock: {on_top:?}"
);

let mut stepped_off = false;
for _ in 0..400 {
let frame = engine.tick(&WorldSnapshot {
proposal: walk(),
..dock_snapshot(16)
});
let on_dock = frame.state == State::Perched && frame.position.y == dock.y;
stepped_off |= !on_dock;
assert!(
!(stepped_off && on_dock),
"put back on the Dock it had just walked off: {frame:?}"
);
if frame.state == State::Climbing && frame.position.x == one_display().x {
return; // the far wall, which is what walking that way is for
}
}
panic!("the walk never got past the Dock to the display edge");
}

/// Standing on the Dock's top is standing above its side, not behind it:
/// a walk along the top meets no wall.
#[test]
Expand Down
36 changes: 31 additions & 5 deletions crates/core/src/engine/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,22 @@ pub(super) fn dock_in(snapshot: &WorldSnapshot) -> Option<Rect> {
/// Strictly inside that, so the sprite this puts on the line is beside the
/// Dock and stays put: the walk stops going forward instead of being set
/// back, which is the #141 stutter it must not repeat.
pub(super) fn dock_side_reached(position: Point, snapshot: &WorldSnapshot) -> Option<f64> {
///
/// Out in that margin the side is a wall only for a sprite moving into the
/// Dock, the rule `wall_reached` already keeps for a display edge: one moving
/// away has left, and catching it anyway is a wall in the middle of the floor.
/// It is what made the Dock a trap for a walk heading past it — step off the
/// top, get caught by the side just below, climb, and get put back on the top
/// to do it again, for as long as the app ran. #361.
///
/// Strictly behind the Dock there is no such reprieve, whichever way the
/// sprite is going: a real Dock is wide and the way out from under one is a
/// long way sideways, all of it unseen. It climbs.
pub(super) fn dock_side_reached(
position: Point,
velocity_x: f64,
snapshot: &WorldSnapshot,
) -> Option<f64> {
let dock = dock_in(snapshot)?;
let (left, right) = (
dock.x - EDGE_CLEARANCE,
Expand All @@ -285,11 +300,22 @@ pub(super) fn dock_side_reached(position: Point, snapshot: &WorldSnapshot) -> Op
&& position.y <= display.bottom()
})?;

Some(if position.x - left <= right - position.x {
left
let nearer_left = position.x - left <= right - position.x;

// Strictly inside, so a sprite level with the Dock's own edge counts as
// out in the margin and may leave. It is the position stepping off the top
// lands on, and treating it as behind would keep the trap shut.
let behind = position.x > dock.x && position.x < dock.x + dock.width;
let into_dock = if nearer_left {
velocity_x > 0.0
} else {
right
})
velocity_x < 0.0
};
if !behind && !into_dock {
return None;
}

Some(if nearer_left { left } else { right })
}

/// Where a climb beside the Dock steps onto its top, once the feet reach it.
Expand Down