diff --git a/src/main.rs b/src/main.rs index 2494b16..4e67e17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,8 @@ pub fn main() { event!(tracing::Level::INFO, "Starting game loop ({:.3}ms)", loop_time.as_secs_f32() * 1000.0); let mut main_loop = || { let start = Instant::now(); - + + // TODO: Fix key repeat delay issues by using VecDeque for instant key repeat for event in event_pump.poll_iter() { match event { // Handle quitting keys or window close diff --git a/src/pacman.rs b/src/pacman.rs index 1d38bf0..2c5140d 100644 --- a/src/pacman.rs +++ b/src/pacman.rs @@ -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) -> (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);