feat: pathfinding, ghost and blinky state, update dependencies

This commit is contained in:
2025-07-22 13:18:02 -05:00
parent fd7eecf53e
commit 0a46f64866
10 changed files with 942 additions and 170 deletions

View File

@@ -9,41 +9,29 @@ use crate::direction::Direction;
/// An animated texture, which is a texture that is rendered as a series of
/// frames.
///
/// This struct manages the state of an animated texture, including the current
/// frame and the number of frames in the animation.
pub struct AnimatedTexture<'a> {
// Parameters
raw_texture: Texture<'a>,
/// The current tick of the animation.
ticker: u32,
/// Whether the animation is currently playing in reverse.
reversed: bool,
/// The offset of the texture, in pixels.
offset: (i32, i32),
/// The number of ticks per frame.
ticks_per_frame: u32,
/// The number of frames in the animation.
frame_count: u32,
/// The width of each frame, in pixels.
frame_width: u32,
/// The height of each frame, in pixels.
frame_height: u32,
width: u32,
height: u32,
// State
ticker: u32,
reversed: bool,
}
impl<'a> AnimatedTexture<'a> {
/// Creates a new `AnimatedTexture`.
///
/// # Arguments
///
/// * `texture` - The texture to animate.
/// * `ticks_per_frame` - The number of ticks to display each frame for.
/// * `frame_count` - The number of frames in the animation.
/// * `frame_width` - The width of each frame.
/// * `frame_height` - The height of each frame.
/// * `offset` - The offset of the texture, in pixels.
pub fn new(
texture: Texture<'a>,
ticks_per_frame: u32,
frame_count: u32,
frame_width: u32,
frame_height: u32,
width: u32,
height: u32,
offset: Option<(i32, i32)>,
) -> Self {
AnimatedTexture {
@@ -52,20 +40,26 @@ impl<'a> AnimatedTexture<'a> {
reversed: false,
ticks_per_frame,
frame_count,
frame_width,
frame_height,
width,
height,
offset: offset.unwrap_or((0, 0)),
}
}
/// Returns the current frame number.
fn current_frame(&self) -> u32 {
self.ticker / self.ticks_per_frame
}
/// Advances the animation by one tick.
///
/// The animation will play forwards, then backwards, then forwards, and so on.
/// This method updates the internal ticker that tracks the current frame
/// of the animation. The animation automatically reverses direction when
/// it reaches the end, creating a ping-pong effect.
///
/// When `reversed` is `false`, the ticker increments until it reaches
/// the total number of ticks for all frames, then reverses direction.
/// When `reversed` is `true`, the ticker decrements until it reaches 0,
/// then reverses direction again.
pub fn tick(&mut self) {
if self.reversed {
self.ticker -= 1;
@@ -82,27 +76,33 @@ impl<'a> AnimatedTexture<'a> {
}
}
/// Calculates the source rectangle for the given frame.
fn get_frame_rect(&self, frame: u32) -> Rect {
if frame >= self.frame_count {
panic!("Frame {} is out of bounds for this texture", frame);
}
Rect::new(
frame as i32 * self.frame_width as i32,
0,
self.frame_width,
self.frame_height,
)
}
/// Renders the animation to the canvas.
/// Gets the source rectangle for a specific frame of the animated texture.
///
/// This method calculates the position and dimensions of a frame within the
/// texture atlas. Frames are arranged horizontally in a single row, so the
/// rectangle's x-coordinate is calculated by multiplying the frame index
/// by the frame width.
///
/// # Arguments
///
/// * `canvas` - The canvas to render to.
/// * `position` - The position to render the animation at.
/// * `direction` - The direction the animation is facing.
/// * `frame` - The frame index to get the rectangle for (0-based)
///
/// # Returns
///
/// A `Rect` representing the source rectangle for the specified frame
fn get_frame_rect(&self, frame: u32) -> Option<Rect> {
if frame >= self.frame_count {
return None;
}
Some(Rect::new(
frame as i32 * self.width as i32,
0,
self.width,
self.height,
))
}
pub fn render(
&mut self,
canvas: &mut Canvas<Window>,
@@ -113,41 +113,22 @@ impl<'a> AnimatedTexture<'a> {
self.tick();
}
/// Renders the animation to the canvas, but only ticks the animation until
/// the given frame is reached.
/// Renders a specific frame of the animated texture to the canvas.
///
/// This method renders a static frame without advancing the animation ticker.
/// It's useful for displaying a specific frame, such as when an entity is stopped
/// or when you want to manually control which frame is displayed.
///
/// # Arguments
///
/// * `canvas` - The canvas to render to.
/// * `position` - The position to render the animation at.
/// * `direction` - The direction the animation is facing.
/// * `frame` - The frame to render until.
pub fn render_until(
&mut self,
canvas: &mut Canvas<Window>,
position: (i32, i32),
direction: Direction,
frame: u32,
) {
// TODO: If the frame we're targeting is in the opposite direction (due
// to self.reverse), we should pre-emptively reverse. This would require
// a more complex ticking mechanism.
let current = self.current_frame();
self.render_static(canvas, position, direction, Some(current));
if frame != current {
self.tick();
}
}
/// Renders a specific frame of the animation.
/// * `canvas` - The SDL canvas to render to
/// * `position` - The pixel position where the texture should be rendered
/// * `direction` - The direction to rotate the texture based on entity facing
/// * `frame` - Optional specific frame to render. If `None`, uses the current frame
///
/// # Arguments
/// # Panics
///
/// * `canvas` - The canvas to render to.
/// * `position` - The position to render the animation at.
/// * `direction` - The direction the animation is facing.
/// * `frame` - The frame to render. If `None`, the current frame is used.
/// Panics if the specified frame is out of bounds for this texture.
pub fn render_static(
&mut self,
canvas: &mut Canvas<Window>,
@@ -155,19 +136,20 @@ impl<'a> AnimatedTexture<'a> {
direction: Direction,
frame: Option<u32>,
) {
let frame_rect = self.get_frame_rect(frame.unwrap_or(self.current_frame()));
let position_rect = Rect::new(
let texture_source_frame_rect =
self.get_frame_rect(frame.unwrap_or_else(|| self.current_frame()));
let canvas_destination_rect = Rect::new(
position.0 + self.offset.0,
position.1 + self.offset.1,
self.frame_width,
self.frame_height,
self.width,
self.height,
);
canvas
.copy_ex(
&self.raw_texture,
Some(frame_rect),
Some(position_rect),
texture_source_frame_rect,
Some(canvas_destination_rect),
direction.angle(),
None,
false,
@@ -175,4 +157,9 @@ impl<'a> AnimatedTexture<'a> {
)
.expect("Could not render texture on canvas");
}
/// Sets the color modulation for the texture.
pub fn set_color_modulation(&mut self, r: u8, g: u8, b: u8) {
self.raw_texture.set_color_mod(r, g, b);
}
}

View File

@@ -20,7 +20,7 @@ pub const WINDOW_WIDTH: u32 = CELL_SIZE * BOARD_WIDTH;
pub const WINDOW_HEIGHT: u32 = CELL_SIZE * (BOARD_HEIGHT + 6);
/// An enum representing the different types of tiles on the map.
#[derive(Debug, Copy, Clone, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MapTile {
/// An empty tile.
Empty,

View File

@@ -1,4 +1,5 @@
//! This module contains the main game logic and state.
use std::cell::RefCell;
use std::rc::Rc;
use sdl2::image::LoadTexture;
@@ -14,6 +15,7 @@ use crate::audio::Audio;
use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
use crate::direction::Direction;
use crate::entity::Entity;
use crate::ghosts::Blinky;
use crate::map::Map;
use crate::pacman::Pacman;
@@ -24,6 +26,10 @@ static POWER_PELLET_TEXTURE_DATA: &[u8] = include_bytes!("../assets/24/energizer
static MAP_TEXTURE_DATA: &[u8] = include_bytes!("../assets/map.png");
static FONT_DATA: &[u8] = include_bytes!("../assets/font/konami.ttf");
// Add ghost texture data
static GHOST_BODY_TEXTURE_DATA: &[u8] = include_bytes!("../assets/32/ghost_body.png");
static GHOST_EYES_TEXTURE_DATA: &[u8] = include_bytes!("../assets/32/ghost_eyes.png");
/// The main game state.
///
/// This struct contains all the information necessary to run the game, including
@@ -34,11 +40,13 @@ pub struct Game<'a> {
pellet_texture: Texture<'a>,
power_pellet_texture: Texture<'a>,
font: Font<'a, 'static>,
pacman: Pacman<'a>,
pacman: Rc<RefCell<Pacman<'a>>>,
map: Rc<std::cell::RefCell<Map>>,
debug: bool,
score: u32,
audio: Audio,
// Add ghost
blinky: Blinky<'a>,
}
impl Game<'_> {
@@ -62,7 +70,28 @@ impl Game<'_> {
let pacman_atlas = texture_creator
.load_texture_bytes(PACMAN_TEXTURE_DATA)
.expect("Could not load pacman texture from embedded data");
let pacman = Pacman::new((1, 1), pacman_atlas, Rc::clone(&map));
let pacman = Rc::new(std::cell::RefCell::new(Pacman::new(
(1, 1),
pacman_atlas,
Rc::clone(&map),
)));
// Load ghost textures
let ghost_body = texture_creator
.load_texture_bytes(GHOST_BODY_TEXTURE_DATA)
.expect("Could not load ghost body texture from embedded data");
let ghost_eyes = texture_creator
.load_texture_bytes(GHOST_EYES_TEXTURE_DATA)
.expect("Could not load ghost eyes texture from embedded data");
// Create Blinky
let blinky = Blinky::new(
(13, 11), // Starting position just above ghost house
ghost_body,
ghost_eyes,
Rc::clone(&map),
Rc::clone(&pacman),
);
// Load pellet texture from embedded data
let pellet_texture = texture_creator
@@ -90,15 +119,16 @@ impl Game<'_> {
Game {
canvas,
pacman: pacman,
pacman,
debug: false,
map: map,
map,
map_texture,
pellet_texture,
power_pellet_texture,
font,
score: 0,
audio,
blinky,
}
}
@@ -110,7 +140,7 @@ impl Game<'_> {
pub fn keyboard_event(&mut self, keycode: Keycode) {
// Change direction
let direction = Direction::from_keycode(keycode);
self.pacman.next_direction = direction;
self.pacman.borrow_mut().next_direction = direction;
// Toggle debug mode
if keycode == Keycode::Space {
@@ -143,9 +173,16 @@ impl Game<'_> {
// Reset the score
self.score = 0;
// Reset Pacman position (you might want to customize this)
// For now, we'll keep Pacman where he is, but you could add:
// self.pacman.position = Map::cell_to_pixel((1, 1));
// Reset Pac-Man position
let mut pacman = self.pacman.borrow_mut();
pacman.pixel_position = Map::cell_to_pixel((1, 1));
pacman.cell_position = (1, 1);
// Reset ghost positions
self.blinky.set_mode(crate::ghost::GhostMode::House);
self.blinky.pixel_position = Map::cell_to_pixel((13, 11));
self.blinky.cell_position = (13, 11);
self.blinky.direction = Direction::Left;
event!(tracing::Level::INFO, "Game reset - map and score cleared");
}
@@ -153,13 +190,14 @@ impl Game<'_> {
/// Advances the game by one tick.
pub fn tick(&mut self) {
self.check_pellet_eating();
self.pacman.tick();
self.pacman.borrow_mut().tick();
self.blinky.tick();
}
/// Checks if Pac-Man is currently eating a pellet and updates the game state
/// accordingly.
fn check_pellet_eating(&mut self) {
let cell_pos = self.pacman.cell_position();
let cell_pos = self.pacman.borrow().cell_position();
// Check if there's a pellet at the current position
let tile = {
@@ -227,8 +265,11 @@ impl Game<'_> {
}
}
// Render the pacman
self.pacman.render(self.canvas);
// Render Pac-Man
self.pacman.borrow_mut().render(self.canvas);
// Render ghost
self.blinky.render(self.canvas);
// Render score
self.render_ui();
@@ -244,7 +285,7 @@ impl Game<'_> {
.unwrap_or(MapTile::Empty);
let mut color = None;
if (x, y) == self.pacman.cell_position() {
if (x, y) == self.pacman.borrow().cell_position() {
self.draw_cell((x, y), Color::CYAN);
} else {
color = match tile {
@@ -263,7 +304,7 @@ impl Game<'_> {
}
// Draw the next cell
let next_cell = self.pacman.next_cell(None);
let next_cell = self.pacman.borrow().next_cell(None);
self.draw_cell((next_cell.0 as u32, next_cell.1 as u32), Color::YELLOW);
}

367
src/ghost.rs Normal file
View File

@@ -0,0 +1,367 @@
use pathfinding::prelude::astar;
use sdl2::{
pixels::Color,
render::{Canvas, Texture},
video::Window,
};
use std::cell::RefCell;
use std::rc::Rc;
use rand::Rng;
use crate::{
animation::AnimatedTexture,
constants::{MapTile, BOARD_OFFSET, CELL_SIZE},
direction::Direction,
entity::Entity,
map::Map,
modulation::{SimpleTickModulator, TickModulator},
pacman::Pacman,
};
/// The different modes a ghost can be in
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GhostMode {
/// Chase mode - ghost actively pursues Pac-Man using its unique strategy
Chase,
/// Scatter mode - ghost heads to its home corner
Scatter,
/// Frightened mode - ghost moves randomly and can be eaten
Frightened,
/// Eyes mode - ghost returns to the ghost house after being eaten
Eyes,
/// House mode - ghost is in the ghost house, waiting to exit
House,
}
/// The different ghost personalities
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GhostType {
Blinky, // Red - Shadow
Pinky, // Pink - Speedy
Inky, // Cyan - Bashful
Clyde, // Orange - Pokey
}
impl GhostType {
/// Returns the color of the ghost.
pub fn color(&self) -> Color {
match self {
GhostType::Blinky => Color::RGB(255, 0, 0),
GhostType::Pinky => Color::RGB(255, 184, 255),
GhostType::Inky => Color::RGB(0, 255, 255),
GhostType::Clyde => Color::RGB(255, 184, 82),
}
}
}
/// Base ghost struct that contains common functionality
pub struct Ghost<'a> {
/// The absolute position of the ghost on the board, in pixels
pub pixel_position: (i32, i32),
/// The position of the ghost on the board, in grid coordinates
pub cell_position: (u32, u32),
/// The current direction of the ghost
pub direction: Direction,
/// The current mode of the ghost
pub mode: GhostMode,
/// The type/personality of this ghost
pub ghost_type: GhostType,
/// Whether the ghost is currently blue (frightened)
pub is_blue: bool,
/// Reference to the game map
pub map: Rc<RefCell<Map>>,
/// Reference to Pac-Man for targeting
pub pacman: Rc<RefCell<Pacman<'a>>>,
/// Movement speed
speed: u32,
/// Movement modulator
modulation: SimpleTickModulator,
/// Ghost body sprite
body_sprite: AnimatedTexture<'a>,
/// Ghost eyes sprite
eyes_sprite: AnimatedTexture<'a>,
}
impl Ghost<'_> {
/// Creates a new ghost instance
pub fn new<'a>(
ghost_type: GhostType,
starting_position: (u32, u32),
body_texture: Texture<'a>,
eyes_texture: Texture<'a>,
map: Rc<RefCell<Map>>,
pacman: Rc<RefCell<Pacman<'a>>>,
) -> Ghost<'a> {
let color = ghost_type.color();
let mut body_sprite = AnimatedTexture::new(body_texture, 8, 2, 32, 32, Some((-4, -4)));
body_sprite.set_color_modulation(color.r, color.g, color.b);
Ghost {
pixel_position: Map::cell_to_pixel(starting_position),
cell_position: starting_position,
direction: Direction::Left,
mode: GhostMode::Chase,
ghost_type,
is_blue: false,
map,
pacman,
speed: 3,
modulation: SimpleTickModulator::new(1.0),
body_sprite,
eyes_sprite: AnimatedTexture::new(eyes_texture, 1, 4, 32, 32, Some((-4, -4))),
}
}
/// Renders the ghost to the canvas
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
// Render body
if self.mode != GhostMode::Eyes {
let color = if self.mode == GhostMode::Frightened {
Color::RGB(0, 0, 255)
} else {
self.ghost_type.color()
};
self.body_sprite
.set_color_modulation(color.r, color.g, color.b);
self.body_sprite
.render(canvas, self.pixel_position, Direction::Right);
}
// Always render eyes on top
let eye_frame = if self.mode == GhostMode::Frightened {
4 // Frightened frame
} else {
match self.direction {
Direction::Right => 0,
Direction::Up => 1,
Direction::Left => 2,
Direction::Down => 3,
}
};
self.eyes_sprite.render_static(
canvas,
self.pixel_position,
Direction::Right,
Some(eye_frame),
);
}
/// Calculates the path to the target tile using the A* algorithm.
fn get_path_to_target(&self, target: (u32, u32)) -> Option<(Vec<(u32, u32)>, u32)> {
let start = self.cell_position;
let map = self.map.borrow();
astar(
&start,
|&p| {
let mut successors = vec![];
for dir in &[
Direction::Up,
Direction::Down,
Direction::Left,
Direction::Right,
] {
let (dx, dy) = dir.offset();
let next_p = (p.0 as i32 + dx, p.1 as i32 + dy);
if let Some(tile) = map.get_tile(next_p) {
if tile != MapTile::Wall {
successors.push(((next_p.0 as u32, next_p.1 as u32), 1));
}
}
}
successors
},
|&p| {
((p.0 as i32 - target.0 as i32).abs() + (p.1 as i32 - target.1 as i32).abs()) as u32
},
|&p| p == target,
)
}
/// Gets the target tile for this ghost based on its current mode
pub fn get_target_tile(&self) -> (i32, i32) {
match self.mode {
GhostMode::Scatter => self.get_scatter_target(),
GhostMode::Chase => self.get_chase_target(),
GhostMode::Frightened => self.get_random_target(),
GhostMode::Eyes => self.get_house_target(),
GhostMode::House => self.get_house_exit_target(),
}
}
/// Gets this ghost's home corner target for scatter mode
fn get_scatter_target(&self) -> (i32, i32) {
match self.ghost_type {
GhostType::Blinky => (25, 0), // Top right
GhostType::Pinky => (2, 0), // Top left
GhostType::Inky => (27, 35), // Bottom right
GhostType::Clyde => (0, 35), // Bottom left
}
}
/// Gets a random adjacent tile for frightened mode
fn get_random_target(&self) -> (i32, i32) {
let mut rng = rand::thread_rng();
let (x, y) = self.cell_position;
let mut possible_moves = Vec::new();
// Check all four directions
for dir in &[
Direction::Up,
Direction::Down,
Direction::Left,
Direction::Right,
] {
// Don't allow reversing direction
if *dir == self.direction.opposite() {
continue;
}
let (dx, dy) = dir.offset();
let next_cell = (x as i32 + dx, y as i32 + dy);
let tile = self.map.borrow().get_tile(next_cell);
if let Some(MapTile::Wall) = tile {
// It's a wall, not a valid move
} else {
possible_moves.push(next_cell);
}
}
if possible_moves.is_empty() {
// No valid moves, must reverse
let (dx, dy) = self.direction.opposite().offset();
return (x as i32 + dx, y as i32 + dy);
}
// Choose a random valid move
possible_moves[rng.gen_range(0..possible_moves.len())]
}
/// Gets the ghost house target for returning eyes
fn get_house_target(&self) -> (i32, i32) {
(13, 14) // Center of ghost house
}
/// Gets the exit point target when leaving house
fn get_house_exit_target(&self) -> (i32, i32) {
(13, 11) // Just above ghost house
}
/// Gets this ghost's chase mode target (to be implemented by each ghost type)
fn get_chase_target(&self) -> (i32, i32) {
// Default implementation just targets Pac-Man directly
let pacman = self.pacman.borrow();
(pacman.cell_position.0 as i32, pacman.cell_position.1 as i32)
}
/// Changes the ghost's mode and handles direction reversal
pub fn set_mode(&mut self, new_mode: GhostMode) {
// Don't reverse if going to/from frightened or if in house
let should_reverse = self.mode != GhostMode::House
&& new_mode != GhostMode::Frightened
&& self.mode != GhostMode::Frightened;
self.mode = new_mode;
if should_reverse {
self.direction = self.direction.opposite();
}
}
}
impl Entity for Ghost<'_> {
fn position(&self) -> (i32, i32) {
self.pixel_position
}
fn cell_position(&self) -> (u32, u32) {
self.cell_position
}
fn internal_position(&self) -> (u32, u32) {
let (x, y) = self.position();
(x as u32 % CELL_SIZE, y as u32 % CELL_SIZE)
}
fn is_colliding(&self, other: &dyn Entity) -> bool {
let (x, y) = self.position();
let (other_x, other_y) = other.position();
x == other_x && y == other_y
}
fn tick(&mut self) {
if self.mode == GhostMode::House {
// For now, do nothing in the house
return;
}
if self.internal_position() == (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,
);
// Pathfinding logic
let target_tile = self.get_target_tile();
if let Some((path, _)) =
self.get_path_to_target((target_tile.0 as u32, target_tile.1 as u32))
{
if path.len() > 1 {
let next_move = path[1];
let (x, y) = self.cell_position;
let dx = next_move.0 as i32 - x as i32;
let dy = next_move.1 as i32 - y as i32;
self.direction = if dx > 0 {
Direction::Right
} else if dx < 0 {
Direction::Left
} else if dy > 0 {
Direction::Down
} else {
Direction::Up
};
}
}
// Check if the next tile in the current direction is a wall
let (dx, dy) = self.direction.offset();
let next_cell = (
self.cell_position.0 as i32 + dx,
self.cell_position.1 as i32 + dy,
);
let next_tile = self
.map
.borrow()
.get_tile(next_cell)
.unwrap_or(MapTile::Empty);
if next_tile == MapTile::Wall {
// Don't move if the next tile is a wall
return;
}
}
if !self.modulation.next() {
return;
}
// Update position based on current direction and speed
let speed = self.speed as i32;
match self.direction {
Direction::Right => self.pixel_position.0 += speed,
Direction::Left => self.pixel_position.0 -= speed,
Direction::Up => self.pixel_position.1 -= speed,
Direction::Down => self.pixel_position.1 += speed,
}
// Update cell position when aligned with grid
if self.internal_position() == (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,
);
}
}
}

88
src/ghosts/blinky.rs Normal file
View File

@@ -0,0 +1,88 @@
use std::cell::RefCell;
use std::rc::Rc;
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use crate::{
entity::Entity,
ghost::{Ghost, GhostMode, GhostType},
map::Map,
pacman::Pacman,
};
pub struct Blinky<'a> {
ghost: Ghost<'a>,
}
impl<'a> Blinky<'a> {
pub fn new(
starting_position: (u32, u32),
body_texture: Texture<'a>,
eyes_texture: Texture<'a>,
map: Rc<RefCell<Map>>,
pacman: Rc<RefCell<Pacman<'a>>>,
) -> Blinky<'a> {
Blinky {
ghost: Ghost::new(
GhostType::Blinky,
starting_position,
body_texture,
eyes_texture,
map,
pacman,
),
}
}
/// Gets Blinky's chase target - directly targets Pac-Man's current position
fn get_chase_target(&self) -> (i32, i32) {
let pacman = self.ghost.pacman.borrow();
(pacman.cell_position.0 as i32, pacman.cell_position.1 as i32)
}
pub fn set_mode(&mut self, mode: GhostMode) {
self.ghost.set_mode(mode);
}
pub fn render(&mut self, canvas: &mut Canvas<Window>) {
self.ghost.render(canvas);
}
}
impl<'a> Entity for Blinky<'a> {
fn position(&self) -> (i32, i32) {
self.ghost.position()
}
fn cell_position(&self) -> (u32, u32) {
self.ghost.cell_position()
}
fn internal_position(&self) -> (u32, u32) {
self.ghost.internal_position()
}
fn is_colliding(&self, other: &dyn Entity) -> bool {
self.ghost.is_colliding(other)
}
fn tick(&mut self) {
self.ghost.tick()
}
}
// Allow direct access to ghost fields
impl<'a> std::ops::Deref for Blinky<'a> {
type Target = Ghost<'a>;
fn deref(&self) -> &Self::Target {
&self.ghost
}
}
impl<'a> std::ops::DerefMut for Blinky<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.ghost
}
}

3
src/ghosts/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
mod blinky;
pub use blinky::Blinky;

View File

@@ -58,6 +58,8 @@ mod constants;
mod direction;
mod entity;
mod game;
mod ghost;
mod ghosts;
mod helper;
mod map;
mod modulation;

View File

@@ -69,10 +69,9 @@ impl Pacman<'_> {
///
/// * `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.pixel_position, self.direction, 2);
.render_static(canvas, self.pixel_position, self.direction, Some(2));
} else {
self.sprite
.render(canvas, self.pixel_position, self.direction);
@@ -208,29 +207,31 @@ impl Entity for Pacman<'_> {
}
}
if !self.stopped && self.modulation.next() {
let speed = self.speed as i32;
match self.direction {
Direction::Right => {
self.pixel_position.0 += speed;
if !self.stopped {
if self.modulation.next() {
let speed = self.speed as i32;
match self.direction {
Direction::Right => {
self.pixel_position.0 += speed;
}
Direction::Left => {
self.pixel_position.0 -= speed;
}
Direction::Up => {
self.pixel_position.1 -= speed;
}
Direction::Down => {
self.pixel_position.1 += speed;
}
}
Direction::Left => {
self.pixel_position.0 -= speed;
}
Direction::Up => {
self.pixel_position.1 -= speed;
}
Direction::Down => {
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,
);
// 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,
);
}
}
}
}