Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rapid and continuous movement issue #14

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ lower_bound_method = "MinimumMove"
```

For `solver` related configuration options, please refer to [Solver](./solver.md).

## Handling Rapid Inputs

To handle rapid and continuous inputs, the game uses an input buffer. You can configure the size of this buffer by editing the `config.toml` file.

```toml
# Input buffer size for handling rapid inputs.
input_buffer_size = 10
```

Increasing the buffer size can help in managing rapid inputs more effectively, but setting it too high may introduce input lag.
38 changes: 22 additions & 16 deletions src/board.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use soukoban::{direction::Direction, Action, Actions, Level, Tiles};
use std::collections::VecDeque;

#[derive(Clone)]
pub struct Board {
pub level: Level,
actions: Actions,
undone_actions: Actions,
input_queue: VecDeque<Direction>,
}

impl Board {
Expand All @@ -14,6 +16,7 @@ impl Board {
level,
actions: Actions::new(),
undone_actions: Actions::new(),
input_queue: VecDeque::new(),
}
}

Expand All @@ -35,25 +38,28 @@ impl Board {

/// Moves the player or pushes a box in the specified direction.
pub fn move_or_push(&mut self, direction: Direction) {
let map = self.level.map_mut();
let direction_vector = &direction.into();
let player_next_position = map.player_position() + direction_vector;
if map[player_next_position].intersects(Tiles::Wall) {
return;
}
if map[player_next_position].intersects(Tiles::Box) {
let box_next_position = player_next_position + direction_vector;
if map[box_next_position].intersects(Tiles::Wall | Tiles::Box) {
return;
self.input_queue.push_back(direction);
while let Some(direction) = self.input_queue.pop_front() {
let map = self.level.map_mut();
let direction_vector = &direction.into();
let player_next_position = map.player_position() + direction_vector;
if map[player_next_position].intersects(Tiles::Wall) {
continue;
}
map.set_box_position(player_next_position, box_next_position);
if map[player_next_position].intersects(Tiles::Box) {
let box_next_position = player_next_position + direction_vector;
if map[box_next_position].intersects(Tiles::Wall | Tiles::Box) {
continue;
}
map.set_box_position(player_next_position, box_next_position);

self.actions.push(Action::Push(direction));
} else {
self.actions.push(Action::Move(direction));
self.actions.push(Action::Push(direction));
} else {
self.actions.push(Action::Move(direction));
}
map.set_player_position(player_next_position);
self.undone_actions.clear();
}
map.set_player_position(player_next_position);
self.undone_actions.clear();
}

/// Undoes the last push.
Expand Down
5 changes: 1 addition & 4 deletions src/systems/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,7 @@ fn player_move(
player_movement: &mut PlayerMovement,
board: &crate::board::Board,
) {
if !board.moveable(direction) {
return;
}
player_movement.directions.push_front(direction);
player_movement.directions.push_back(direction);
}

fn instant_player_move_to(
Expand Down
Loading