mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-08 22:07:57 -06:00
feat: allow instant direction reversal, improve cell position state tracking
This commit is contained in:
154
src/pacman.rs
154
src/pacman.rs
@@ -1,3 +1,4 @@
|
||||
//! This module defines the Pac-Man entity, including its behavior and rendering.
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -17,11 +18,18 @@ use crate::{
|
||||
modulation::{SimpleTickModulator, TickModulator},
|
||||
};
|
||||
|
||||
/// The Pac-Man entity.
|
||||
pub struct Pacman<'a> {
|
||||
// Absolute position on the board (precise)
|
||||
pub position: (i32, i32),
|
||||
/// The absolute position of Pac-Man on the board, in pixels.
|
||||
pub pixel_position: (i32, i32),
|
||||
/// The position of Pac-Man on the board, in grid coordinates.
|
||||
/// This is only updated at the moment Pac-Man is aligned with the grid.
|
||||
pub cell_position: (u32, u32),
|
||||
/// The current direction of Pac-Man.
|
||||
pub direction: Direction,
|
||||
/// The next direction of Pac-Man, which will be applied when Pac-Man is next aligned with the grid.
|
||||
pub next_direction: Option<Direction>,
|
||||
/// Whether Pac-Man is currently stopped.
|
||||
pub stopped: bool,
|
||||
map: Rc<RefCell<Map>>,
|
||||
speed: u32,
|
||||
@@ -30,13 +38,21 @@ pub struct Pacman<'a> {
|
||||
}
|
||||
|
||||
impl Pacman<'_> {
|
||||
/// Creates a new `Pacman` instance.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `starting_position` - The starting position of Pac-Man, in grid coordinates.
|
||||
/// * `atlas` - The texture atlas containing the Pac-Man sprites.
|
||||
/// * `map` - A reference to the game map.
|
||||
pub fn new<'a>(
|
||||
starting_position: (u32, u32),
|
||||
atlas: Texture<'a>,
|
||||
map: Rc<RefCell<Map>>,
|
||||
) -> Pacman<'a> {
|
||||
Pacman {
|
||||
position: Map::cell_to_pixel(starting_position),
|
||||
pixel_position: Map::cell_to_pixel(starting_position),
|
||||
cell_position: starting_position,
|
||||
direction: Direction::Right,
|
||||
next_direction: None,
|
||||
speed: 3,
|
||||
@@ -47,53 +63,85 @@ impl Pacman<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders Pac-Man to the canvas.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `canvas` - The SDL canvas to render to.
|
||||
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
|
||||
// When stopped, render the last frame of the animation
|
||||
if self.stopped {
|
||||
self.sprite
|
||||
.render_until(canvas, self.position, self.direction, 2);
|
||||
.render_until(canvas, self.pixel_position, self.direction, 2);
|
||||
} else {
|
||||
self.sprite.render(canvas, self.position, self.direction);
|
||||
self.sprite
|
||||
.render(canvas, self.pixel_position, self.direction);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates the next cell in the given direction.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `direction` - The direction to check. If `None`, the current direction is used.
|
||||
pub fn next_cell(&self, direction: Option<Direction>) -> (i32, i32) {
|
||||
let (x, y) = direction.unwrap_or(self.direction).offset();
|
||||
let cell = self.cell_position();
|
||||
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;
|
||||
/// Handles a requested direction change.
|
||||
///
|
||||
/// The direction change is only applied if the next tile in the requested
|
||||
/// direction is not a wall.
|
||||
fn handle_direction_change(&mut self) -> bool {
|
||||
match self.next_direction {
|
||||
// If there is no next direction, do nothing.
|
||||
None => return false,
|
||||
// If the next direction is the same as the current direction, do nothing.
|
||||
Some(next_direction) => {
|
||||
if next_direction == self.direction {
|
||||
self.next_direction = None;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get the next cell in the proposed direction.
|
||||
let proposed_next_cell = self.next_cell(self.next_direction);
|
||||
let proposed_next_tile = self
|
||||
.map
|
||||
.borrow()
|
||||
.get_tile(proposed_next_cell)
|
||||
.unwrap_or(MapTile::Empty);
|
||||
if proposed_next_tile != MapTile::Wall {
|
||||
event!(
|
||||
tracing::Level::DEBUG,
|
||||
"Direction change: {:?} -> {:?} at position ({}, {}) internal ({}, {})",
|
||||
self.direction,
|
||||
self.next_direction.unwrap(),
|
||||
self.position.0,
|
||||
self.position.1,
|
||||
self.internal_position().0,
|
||||
self.internal_position().1
|
||||
);
|
||||
self.direction = self.next_direction.unwrap();
|
||||
self.next_direction = None;
|
||||
|
||||
// If the next tile is a wall, do nothing.
|
||||
if proposed_next_tile == MapTile::Wall {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the next tile is not a wall, change direction.
|
||||
event!(
|
||||
tracing::Level::DEBUG,
|
||||
"Direction change: {:?} -> {:?} at position ({}, {}) internal ({}, {})",
|
||||
self.direction,
|
||||
self.next_direction.unwrap(),
|
||||
self.pixel_position.0,
|
||||
self.pixel_position.1,
|
||||
self.internal_position().0,
|
||||
self.internal_position().1
|
||||
);
|
||||
self.direction = self.next_direction.unwrap();
|
||||
self.next_direction = None;
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
/// Returns the internal position of Pac-Man, rounded down to the nearest
|
||||
/// even number.
|
||||
///
|
||||
/// This is used to ensure that Pac-Man is aligned with the grid before
|
||||
/// changing direction.
|
||||
fn internal_position_even(&self) -> (u32, u32) {
|
||||
let (x, y) = self.internal_position();
|
||||
((x / 2u32) * 2u32, (y / 2u32) * 2u32)
|
||||
@@ -108,15 +156,11 @@ impl Entity for Pacman<'_> {
|
||||
}
|
||||
|
||||
fn position(&self) -> (i32, i32) {
|
||||
self.position
|
||||
self.pixel_position
|
||||
}
|
||||
|
||||
fn cell_position(&self) -> (u32, u32) {
|
||||
let (x, y) = self.position;
|
||||
(
|
||||
(x as u32 / CELL_SIZE) - BOARD_OFFSET.0,
|
||||
(y as u32 / CELL_SIZE) - BOARD_OFFSET.1,
|
||||
)
|
||||
self.cell_position
|
||||
}
|
||||
|
||||
fn internal_position(&self) -> (u32, u32) {
|
||||
@@ -125,13 +169,35 @@ impl Entity for Pacman<'_> {
|
||||
}
|
||||
|
||||
fn tick(&mut self) {
|
||||
// Pac-Man can only change direction when he is perfectly aligned with the grid.
|
||||
let can_change = self.internal_position_even() == (0, 0);
|
||||
|
||||
if can_change {
|
||||
self.handle_requested_direction();
|
||||
if let Some(next_direction) = self.next_direction {
|
||||
if next_direction == self.direction.opposite() {
|
||||
let next_tile_position = self.next_cell(Some(next_direction));
|
||||
let next_tile = self
|
||||
.map
|
||||
.borrow()
|
||||
.get_tile(next_tile_position)
|
||||
.unwrap_or(MapTile::Empty);
|
||||
|
||||
let next = self.next_cell(None);
|
||||
let next_tile = self.map.borrow().get_tile(next).unwrap_or(MapTile::Empty);
|
||||
if next_tile != MapTile::Wall {
|
||||
self.direction = next_direction;
|
||||
self.next_direction = None;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if can_change {
|
||||
self.handle_direction_change();
|
||||
|
||||
// Check if the next tile in the current direction is a wall.
|
||||
let next_tile_position = self.next_cell(None);
|
||||
let next_tile = self
|
||||
.map
|
||||
.borrow()
|
||||
.get_tile(next_tile_position)
|
||||
.unwrap_or(MapTile::Empty);
|
||||
|
||||
if !self.stopped && next_tile == MapTile::Wall {
|
||||
event!(tracing::Level::DEBUG, "Wall collision. Stopping.");
|
||||
@@ -146,18 +212,26 @@ impl Entity for Pacman<'_> {
|
||||
let speed = self.speed as i32;
|
||||
match self.direction {
|
||||
Direction::Right => {
|
||||
self.position.0 += speed;
|
||||
self.pixel_position.0 += speed;
|
||||
}
|
||||
Direction::Left => {
|
||||
self.position.0 -= speed;
|
||||
self.pixel_position.0 -= speed;
|
||||
}
|
||||
Direction::Up => {
|
||||
self.position.1 -= speed;
|
||||
self.pixel_position.1 -= speed;
|
||||
}
|
||||
Direction::Down => {
|
||||
self.position.1 += speed;
|
||||
self.pixel_position.1 += speed;
|
||||
}
|
||||
}
|
||||
|
||||
// Update the cell position if Pac-Man is aligned with the grid.
|
||||
if self.internal_position_even() == (0, 0) {
|
||||
self.cell_position = (
|
||||
(self.pixel_position.0 as u32 / CELL_SIZE) - BOARD_OFFSET.0,
|
||||
(self.pixel_position.1 as u32 / CELL_SIZE) - BOARD_OFFSET.1,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user