refactor: continue improving MovableEntity shared implementation

This commit is contained in:
2025-07-23 15:06:09 -05:00
parent 1fa7a0807f
commit df8f858651
6 changed files with 341 additions and 415 deletions

View File

@@ -1,19 +1,11 @@
use pathfinding::prelude::dijkstra;
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, BOARD_WIDTH, CELL_SIZE},
constants::{MapTile, BOARD_WIDTH},
direction::Direction,
entity::{Entity, MovableEntity},
entity::{Entity, MovableEntity, Renderable},
map::Map,
modulation::{SimpleTickModulator, TickModulator},
pacman::Pacman,
@@ -45,12 +37,12 @@ pub enum GhostType {
impl GhostType {
/// Returns the color of the ghost.
pub fn color(&self) -> Color {
pub fn color(&self) -> sdl2::pixels::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),
GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0),
GhostType::Pinky => sdl2::pixels::Color::RGB(255, 184, 255),
GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255),
GhostType::Clyde => sdl2::pixels::Color::RGB(255, 184, 82),
}
}
}
@@ -63,10 +55,8 @@ pub struct Ghost<'a> {
pub mode: GhostMode,
/// The type/personality of this ghost
pub ghost_type: GhostType,
/// Reference to the game map
pub map: Rc<RefCell<Map>>,
/// Reference to Pac-Man for targeting
pub pacman: Rc<RefCell<Pacman<'a>>>,
pub pacman: std::rc::Rc<std::cell::RefCell<Pacman<'a>>>,
/// Ghost body sprite
body_sprite: AnimatedTexture<'a>,
/// Ghost eyes sprite
@@ -78,10 +68,10 @@ impl Ghost<'_> {
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>>>,
body_texture: sdl2::render::Texture<'a>,
eyes_texture: sdl2::render::Texture<'a>,
map: std::rc::Rc<std::cell::RefCell<Map>>,
pacman: std::rc::Rc<std::cell::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)));
@@ -94,56 +84,94 @@ impl Ghost<'_> {
Direction::Left,
3,
SimpleTickModulator::new(1.0),
map,
),
mode: GhostMode::Chase,
ghost_type,
map,
pacman,
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()
};
/// 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(),
}
}
self.body_sprite
.set_color_modulation(color.r, color.g, color.b);
self.body_sprite
.render(canvas, self.base.pixel_position, Direction::Right);
/// 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::rng();
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.base.direction.opposite() {
continue;
}
let next_cell = self.base.next_cell(Some(*dir));
if !matches!(
self.base.map.borrow().get_tile(next_cell),
Some(MapTile::Wall)
) {
possible_moves.push(next_cell);
}
}
// Always render eyes on top
let eye_frame = if self.mode == GhostMode::Frightened {
4 // Frightened frame
if possible_moves.is_empty() {
// No valid moves, must reverse
self.base.next_cell(Some(self.base.direction.opposite()))
} else {
match self.base.direction {
Direction::Right => 0,
Direction::Up => 1,
Direction::Left => 2,
Direction::Down => 3,
}
};
// Choose a random valid move
possible_moves[rng.random_range(0..possible_moves.len())]
}
}
self.eyes_sprite.render_static(
canvas,
self.base.pixel_position,
Direction::Right,
Some(eye_frame),
);
/// 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();
let cell = pacman.base().cell_position;
(cell.0 as i32, cell.1 as i32)
}
/// Calculates the path to the target tile using the A* algorithm.
pub fn get_path_to_target(&self, target: (u32, u32)) -> Option<(Vec<(u32, u32)>, u32)> {
let start = self.base.cell_position;
let map = self.map.borrow();
let map = self.base.map.borrow();
dijkstra(
&start,
@@ -179,83 +207,6 @@ impl Ghost<'_> {
)
}
/// 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::rng();
let (x, y) = self.base.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.base.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.base.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();
let cell = pacman.base.cell_position;
(cell.0 as i32, cell.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
@@ -274,7 +225,8 @@ impl Ghost<'_> {
};
if should_reverse {
self.base.direction = self.base.direction.opposite();
self.base
.set_direction_if_valid(self.base.direction.opposite());
}
}
}
@@ -284,56 +236,16 @@ impl Entity for Ghost<'_> {
&self.base
}
/// Returns true if the ghost entity is colliding with the other entity.
fn is_colliding(&self, other: &dyn Entity) -> bool {
let (x, y) = self.base.pixel_position;
let (other_x, other_y) = other.base().pixel_position;
x == other_x && y == other_y
}
/// Ticks the ghost entity.
fn tick(&mut self) {
if self.mode == GhostMode::House {
// For now, do nothing in the house
return;
}
if self.base.internal_position() == (0, 0) {
self.base.cell_position = (
(self.base.pixel_position.0 as u32 / CELL_SIZE) - BOARD_OFFSET.0,
(self.base.pixel_position.1 as u32 / CELL_SIZE) - BOARD_OFFSET.1,
);
if self.base.is_grid_aligned() {
self.base.update_cell_position();
let current_tile = self
.map
.borrow()
.get_tile((
self.base.cell_position.0 as i32,
self.base.cell_position.1 as i32,
))
.unwrap_or(MapTile::Empty);
if current_tile == MapTile::Tunnel {
self.base.in_tunnel = true;
}
// Tunnel logic: if in tunnel, force movement and prevent direction change
if self.base.in_tunnel {
// If out of bounds, teleport to the opposite side and exit tunnel
if self.base.cell_position.0 == 0 {
self.base.cell_position.0 = BOARD_WIDTH - 2;
self.base.pixel_position =
Map::cell_to_pixel((self.base.cell_position.0, self.base.cell_position.1));
self.base.in_tunnel = false;
} else if self.base.cell_position.0 == BOARD_WIDTH - 1 {
self.base.cell_position.0 = 1;
self.base.pixel_position =
Map::cell_to_pixel((self.base.cell_position.0, self.base.cell_position.1));
self.base.in_tunnel = false;
} else {
// While in tunnel, do not allow direction change
// and always move in the current direction
}
} else {
if !self.base.handle_tunnel() {
// Pathfinding logic (only if not in tunnel)
let target_tile = self.get_target_tile();
if let Some((path, _)) =
@@ -344,7 +256,7 @@ impl Entity for Ghost<'_> {
let (x, y) = self.base.cell_position;
let dx = next_move.0 as i32 - x as i32;
let dy = next_move.1 as i32 - y as i32;
self.base.direction = if dx > 0 {
let new_direction = if dx > 0 {
Direction::Right
} else if dx < 0 {
Direction::Left
@@ -353,40 +265,60 @@ impl Entity for Ghost<'_> {
} else {
Direction::Up
};
self.base.set_direction_if_valid(new_direction);
}
}
}
// Check if the next tile in the current direction is a wall
let (dx, dy) = self.base.direction.offset();
let next_cell = (
self.base.cell_position.0 as i32 + dx,
self.base.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
// Don't move if the next tile is a wall
if self.base.is_wall_ahead(None) {
return;
}
}
if !self.base.modulation.next() {
return;
}
if self.base.modulation.next() {
self.base.move_forward();
// Update position based on current direction and speed
self.base.move_forward();
// Update cell position when aligned with grid
if self.base.internal_position() == (0, 0) {
self.base.cell_position = (
(self.base.pixel_position.0 as u32 / CELL_SIZE) - BOARD_OFFSET.0,
(self.base.pixel_position.1 as u32 / CELL_SIZE) - BOARD_OFFSET.1,
);
if self.base.is_grid_aligned() {
self.base.update_cell_position();
}
}
}
}
impl Renderable for Ghost<'_> {
fn render(&mut self, canvas: &mut sdl2::render::Canvas<sdl2::video::Window>) {
// Render body
if self.mode != GhostMode::Eyes {
let color = if self.mode == GhostMode::Frightened {
sdl2::pixels::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.base.pixel_position, Direction::Right);
}
// Always render eyes on top
let eye_frame = if self.mode == GhostMode::Frightened {
4 // Frightened frame
} else {
match self.base.direction {
Direction::Right => 0,
Direction::Up => 1,
Direction::Left => 2,
Direction::Down => 3,
}
};
self.eyes_sprite.render_static(
canvas,
self.base.pixel_position,
Direction::Right,
Some(eye_frame),
);
}
}