mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-17 06:12:36 -06:00
refactor: continue working on ghost house implementation, other stuff
This commit is contained in:
@@ -32,7 +32,14 @@ pub enum GhostMode {
|
||||
/// Eyes mode - ghost returns to the ghost house after being eaten
|
||||
Eyes,
|
||||
/// House mode - ghost is in the ghost house, waiting to exit
|
||||
House,
|
||||
House(HouseMode),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum HouseMode {
|
||||
Entering,
|
||||
Exiting,
|
||||
Waiting,
|
||||
}
|
||||
|
||||
/// The different ghost personalities
|
||||
@@ -69,6 +76,8 @@ pub struct Ghost {
|
||||
pub texture: DirectionalAnimatedTexture,
|
||||
pub frightened_texture: BlinkingTexture,
|
||||
pub eyes_texture: DirectionalAnimatedTexture,
|
||||
pub house_offset: i32,
|
||||
pub current_house_offset: i32,
|
||||
}
|
||||
|
||||
impl Ghost {
|
||||
@@ -79,6 +88,7 @@ impl Ghost {
|
||||
atlas: Rc<RefCell<SpriteAtlas>>,
|
||||
map: Rc<RefCell<Map>>,
|
||||
pacman: Rc<RefCell<Pacman>>,
|
||||
house_offset: i32,
|
||||
) -> Ghost {
|
||||
let pixel_position = Map::cell_to_pixel(starting_position);
|
||||
let name = match ghost_type {
|
||||
@@ -127,23 +137,25 @@ impl Ghost {
|
||||
SimpleTickModulator::new(0.9375),
|
||||
map,
|
||||
),
|
||||
mode: GhostMode::Chase,
|
||||
mode: GhostMode::House(HouseMode::Waiting),
|
||||
ghost_type,
|
||||
pacman,
|
||||
texture,
|
||||
frightened_texture,
|
||||
eyes_texture,
|
||||
house_offset,
|
||||
current_house_offset: house_offset,
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the target tile for this ghost based on its current mode
|
||||
pub fn get_target_tile(&self) -> IVec2 {
|
||||
pub fn get_target_tile(&self) -> Option<IVec2> {
|
||||
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(),
|
||||
GhostMode::Scatter => Some(self.get_scatter_target()),
|
||||
GhostMode::Chase => Some(self.get_chase_target()),
|
||||
GhostMode::Frightened => Some(self.get_random_target()),
|
||||
GhostMode::Eyes => Some(self.get_house_target()),
|
||||
GhostMode::House(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,16 +201,51 @@ impl Ghost {
|
||||
IVec2::new(13, 14) // Center of ghost house
|
||||
}
|
||||
|
||||
/// Gets the exit point target when leaving house
|
||||
fn get_house_exit_target(&self) -> IVec2 {
|
||||
IVec2::new(13, 11) // Just above ghost house
|
||||
}
|
||||
|
||||
/// Gets this ghost's chase mode target (to be implemented by each ghost type)
|
||||
/// Gets this ghost's chase mode target based on its personality
|
||||
fn get_chase_target(&self) -> IVec2 {
|
||||
let pacman = self.pacman.borrow();
|
||||
let cell = pacman.base().cell_position;
|
||||
IVec2::new(cell.x as i32, cell.y as i32)
|
||||
let pacman_cell = pacman.base().cell_position;
|
||||
let pacman_direction = pacman.base.direction;
|
||||
|
||||
match self.ghost_type {
|
||||
GhostType::Blinky => {
|
||||
// Blinky (Red) - Directly targets Pac-Man's current position
|
||||
IVec2::new(pacman_cell.x as i32, pacman_cell.y as i32)
|
||||
}
|
||||
GhostType::Pinky => {
|
||||
// Pinky (Pink) - Targets 4 cells ahead of Pac-Man in his direction
|
||||
let offset = pacman_direction.offset();
|
||||
let target_x = (pacman_cell.x as i32) + (offset.x * 4);
|
||||
let target_y = (pacman_cell.y as i32) + (offset.y * 4);
|
||||
IVec2::new(target_x, target_y)
|
||||
}
|
||||
GhostType::Inky => {
|
||||
// Inky (Cyan) - Uses Blinky's position and Pac-Man's position to calculate target
|
||||
// For now, just target Pac-Man with some randomness
|
||||
let mut rng = SmallRng::from_os_rng();
|
||||
let random_offset_x = rng.random_range(-2..=2);
|
||||
let random_offset_y = rng.random_range(-2..=2);
|
||||
IVec2::new(
|
||||
(pacman_cell.x as i32) + random_offset_x,
|
||||
(pacman_cell.y as i32) + random_offset_y,
|
||||
)
|
||||
}
|
||||
GhostType::Clyde => {
|
||||
// Clyde (Orange) - Targets Pac-Man when far, runs to scatter corner when close
|
||||
let distance = ((self.base.base.cell_position.x as i32 - pacman_cell.x as i32).pow(2)
|
||||
+ (self.base.base.cell_position.y as i32 - pacman_cell.y as i32).pow(2))
|
||||
as f32;
|
||||
let distance = distance.sqrt();
|
||||
|
||||
if distance > 8.0 {
|
||||
// Far from Pac-Man - chase
|
||||
IVec2::new(pacman_cell.x as i32, pacman_cell.y as i32)
|
||||
} else {
|
||||
// Close to Pac-Man - scatter to bottom left
|
||||
IVec2::new(0, 35)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates the path to the target tile using the A* algorithm.
|
||||
@@ -239,8 +286,10 @@ impl Ghost {
|
||||
/// 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;
|
||||
let should_reverse = !matches!(self.mode, GhostMode::House(_))
|
||||
&& !matches!(new_mode, GhostMode::House(_))
|
||||
&& !matches!(self.mode, GhostMode::Frightened)
|
||||
&& !matches!(new_mode, GhostMode::Frightened);
|
||||
|
||||
self.mode = new_mode;
|
||||
|
||||
@@ -249,7 +298,7 @@ impl Ghost {
|
||||
GhostMode::Scatter => 0.85,
|
||||
GhostMode::Frightened => 0.7,
|
||||
GhostMode::Eyes => 1.5,
|
||||
GhostMode::House => 0f32,
|
||||
GhostMode::House(_) => 0.7,
|
||||
});
|
||||
|
||||
if should_reverse {
|
||||
@@ -258,36 +307,144 @@ impl Ghost {
|
||||
}
|
||||
|
||||
pub fn tick(&mut self) {
|
||||
if self.mode == GhostMode::House {
|
||||
// For now, do nothing in the house
|
||||
if let GhostMode::House(house_mode) = self.mode {
|
||||
match house_mode {
|
||||
HouseMode::Waiting => {
|
||||
// Ghosts in waiting mode move up and down
|
||||
if self.base.is_grid_aligned() {
|
||||
self.base.update_cell_position();
|
||||
|
||||
// Simple up and down movement
|
||||
let current_pos = self.base.base.cell_position;
|
||||
let start_pos = UVec2::new(13, 14); // Center of ghost house
|
||||
|
||||
if current_pos.y > start_pos.y + 1 {
|
||||
// Too far down, move up
|
||||
self.base.set_direction_if_valid(Direction::Up);
|
||||
} else if current_pos.y < start_pos.y - 1 {
|
||||
// Too far up, move down
|
||||
self.base.set_direction_if_valid(Direction::Down);
|
||||
} else if self.base.direction == Direction::Up {
|
||||
// At top, switch to down
|
||||
self.base.set_direction_if_valid(Direction::Down);
|
||||
} else if self.base.direction == Direction::Down {
|
||||
// At bottom, switch to up
|
||||
self.base.set_direction_if_valid(Direction::Up);
|
||||
}
|
||||
}
|
||||
}
|
||||
HouseMode::Exiting => {
|
||||
// Ghosts exiting move towards the exit
|
||||
if self.base.is_grid_aligned() {
|
||||
self.base.update_cell_position();
|
||||
|
||||
let exit_pos = UVec2::new(13, 11);
|
||||
let current_pos = self.base.base.cell_position;
|
||||
|
||||
// Determine direction to exit
|
||||
if current_pos.y > exit_pos.y {
|
||||
// Need to move up
|
||||
self.base.set_direction_if_valid(Direction::Up);
|
||||
} else if current_pos.y == exit_pos.y && current_pos.x != exit_pos.x {
|
||||
// At exit level, move horizontally to center
|
||||
if current_pos.x < exit_pos.x {
|
||||
self.base.set_direction_if_valid(Direction::Right);
|
||||
} else {
|
||||
self.base.set_direction_if_valid(Direction::Left);
|
||||
}
|
||||
} else if current_pos == exit_pos {
|
||||
// Reached exit, transition to chase mode
|
||||
self.mode = GhostMode::Chase;
|
||||
self.current_house_offset = 0; // Reset offset
|
||||
}
|
||||
}
|
||||
}
|
||||
HouseMode::Entering => {
|
||||
// Ghosts entering move towards their starting position
|
||||
if self.base.is_grid_aligned() {
|
||||
self.base.update_cell_position();
|
||||
|
||||
let start_pos = UVec2::new(13, 14); // Center of ghost house
|
||||
let current_pos = self.base.base.cell_position;
|
||||
|
||||
// Determine direction to starting position
|
||||
if current_pos.y < start_pos.y {
|
||||
// Need to move down
|
||||
self.base.set_direction_if_valid(Direction::Down);
|
||||
} else if current_pos.y == start_pos.y && current_pos.x != start_pos.x {
|
||||
// At house level, move horizontally to center
|
||||
if current_pos.x < start_pos.x {
|
||||
self.base.set_direction_if_valid(Direction::Right);
|
||||
} else {
|
||||
self.base.set_direction_if_valid(Direction::Left);
|
||||
}
|
||||
} else if current_pos == start_pos {
|
||||
// Reached starting position, switch to waiting
|
||||
self.mode = GhostMode::House(HouseMode::Waiting);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update house offset for smooth transitions
|
||||
if self.current_house_offset != 0 {
|
||||
// Gradually reduce offset when turning
|
||||
if self.base.direction == Direction::Left || self.base.direction == Direction::Right {
|
||||
if self.current_house_offset > 0 {
|
||||
self.current_house_offset -= 1;
|
||||
} else if self.current_house_offset < 0 {
|
||||
self.current_house_offset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.base.tick();
|
||||
self.texture.tick();
|
||||
self.frightened_texture.tick();
|
||||
self.eyes_texture.tick();
|
||||
return;
|
||||
}
|
||||
|
||||
// Normal ghost behavior
|
||||
if self.base.is_grid_aligned() {
|
||||
self.base.update_cell_position();
|
||||
if !self.base.handle_tunnel() {
|
||||
// Pathfinding logic (only if not in tunnel)
|
||||
let target_tile = self.get_target_tile();
|
||||
if let Some((path, _)) = self.get_path_to_target(target_tile.as_uvec2()) {
|
||||
if path.len() > 1 {
|
||||
let next_move = path[1];
|
||||
let x = self.base.base.cell_position.x;
|
||||
let y = self.base.base.cell_position.y;
|
||||
let dx = next_move.x as i32 - x as i32;
|
||||
let dy = next_move.y as i32 - y as i32;
|
||||
let new_direction = if dx > 0 {
|
||||
Direction::Right
|
||||
} else if dx < 0 {
|
||||
Direction::Left
|
||||
} else if dy > 0 {
|
||||
Direction::Down
|
||||
} else {
|
||||
Direction::Up
|
||||
};
|
||||
self.base.set_direction_if_valid(new_direction);
|
||||
if let Some(target_tile) = self.get_target_tile() {
|
||||
if let Some((path, _)) = self.get_path_to_target(target_tile.as_uvec2()) {
|
||||
if path.len() > 1 {
|
||||
let next_move = path[1];
|
||||
let x = self.base.base.cell_position.x;
|
||||
let y = self.base.base.cell_position.y;
|
||||
let dx = next_move.x as i32 - x as i32;
|
||||
let dy = next_move.y as i32 - y as i32;
|
||||
let new_direction = if dx > 0 {
|
||||
Direction::Right
|
||||
} else if dx < 0 {
|
||||
Direction::Left
|
||||
} else if dy > 0 {
|
||||
Direction::Down
|
||||
} else {
|
||||
Direction::Up
|
||||
};
|
||||
self.base.set_direction_if_valid(new_direction);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Handle house offset transition when turning
|
||||
if self.current_house_offset != 0 {
|
||||
if self.base.direction == Direction::Left || self.base.direction == Direction::Right {
|
||||
if self.current_house_offset > 0 {
|
||||
self.current_house_offset -= 1;
|
||||
} else if self.current_house_offset < 0 {
|
||||
self.current_house_offset += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.base.tick(); // Handles wall collision and movement
|
||||
self.texture.tick();
|
||||
self.frightened_texture.tick();
|
||||
@@ -324,9 +481,14 @@ impl Moving for Ghost {
|
||||
|
||||
impl Renderable for Ghost {
|
||||
fn render(&mut self, canvas: &mut WindowCanvas) -> Result<()> {
|
||||
let pos = self.base.base.pixel_position;
|
||||
let mut pos = self.base.base.pixel_position;
|
||||
let dir = self.base.direction;
|
||||
|
||||
// Apply house offset if in house mode or transitioning
|
||||
if matches!(self.mode, GhostMode::House(_)) || self.current_house_offset != 0 {
|
||||
pos.x += self.current_house_offset;
|
||||
}
|
||||
|
||||
match self.mode {
|
||||
GhostMode::Frightened => {
|
||||
let tile = self.frightened_texture.animation.current_tile();
|
||||
|
||||
Reference in New Issue
Block a user