mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-10 08:07:56 -06:00
fix: prevent changing direction into walls
This commit is contained in:
@@ -73,6 +73,7 @@ pub fn main() {
|
|||||||
let mut main_loop = || {
|
let mut main_loop = || {
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
|
// TODO: Fix key repeat delay issues by using VecDeque for instant key repeat
|
||||||
for event in event_pump.poll_iter() {
|
for event in event_pump.poll_iter() {
|
||||||
match event {
|
match event {
|
||||||
// Handle quitting keys or window close
|
// Handle quitting keys or window close
|
||||||
|
|||||||
@@ -48,8 +48,8 @@ impl Pacman<'_> {
|
|||||||
self.sprite
|
self.sprite
|
||||||
.render_until(canvas, self.position, self.direction, 2);
|
.render_until(canvas, self.position, self.direction, 2);
|
||||||
} else {
|
} 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) {
|
pub fn next_cell(&self, direction: Option<Direction>) -> (i32, i32) {
|
||||||
@@ -57,6 +57,21 @@ impl Pacman<'_> {
|
|||||||
let cell = self.cell_position();
|
let cell = self.cell_position();
|
||||||
(cell.0 as i32 + x, cell.1 as i32 + y)
|
(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<'_> {
|
impl Entity for Pacman<'_> {
|
||||||
@@ -82,11 +97,9 @@ impl Entity for Pacman<'_> {
|
|||||||
|
|
||||||
fn tick(&mut self) {
|
fn tick(&mut self) {
|
||||||
let can_change = self.internal_position() == (0, 0);
|
let can_change = self.internal_position() == (0, 0);
|
||||||
|
|
||||||
if can_change {
|
if can_change {
|
||||||
if let Some(direction) = self.next_direction {
|
self.handle_requested_direction();
|
||||||
self.direction = direction;
|
|
||||||
self.next_direction = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let next = self.next_cell(None);
|
let next = self.next_cell(None);
|
||||||
let next_tile = self.map.get_tile(next).unwrap_or(MapTile::Empty);
|
let next_tile = self.map.get_tile(next).unwrap_or(MapTile::Empty);
|
||||||
|
|||||||
Reference in New Issue
Block a user