fix: prevent changing direction into walls

This commit is contained in:
2023-09-11 03:11:08 -05:00
parent 50d0bc7d5f
commit a62ddab9af
2 changed files with 21 additions and 7 deletions

View File

@@ -48,8 +48,8 @@ impl Pacman<'_> {
self.sprite
.render_until(canvas, self.position, self.direction, 2);
} else {
self.sprite.render(canvas, self.position, self.direction);
}
self.sprite.render(canvas, self.position, self.direction);
}
}
pub fn next_cell(&self, direction: Option<Direction>) -> (i32, i32) {
@@ -57,6 +57,21 @@ impl Pacman<'_> {
let cell = self.cell_position();
(cell.0 as i32 + x, cell.1 as i32 + y)
}
fn handle_requested_direction(&mut self) {
if self.next_direction.is_none() { return; }
if self.next_direction.unwrap() == self.direction {
self.next_direction = None;
return;
}
let proposed_next_cell = self.next_cell(self.next_direction);
let proposed_next_tile = self.map.get_tile(proposed_next_cell).unwrap_or(MapTile::Empty);
if proposed_next_tile != MapTile::Wall {
self.direction = self.next_direction.unwrap();
self.next_direction = None;
}
}
}
impl Entity for Pacman<'_> {
@@ -82,11 +97,9 @@ impl Entity for Pacman<'_> {
fn tick(&mut self) {
let can_change = self.internal_position() == (0, 0);
if can_change {
if let Some(direction) = self.next_direction {
self.direction = direction;
self.next_direction = None;
}
self.handle_requested_direction();
let next = self.next_cell(None);
let next_tile = self.map.get_tile(next).unwrap_or(MapTile::Empty);