feat: pathfinding for ghosts, add debug rendering of paths

This commit is contained in:
2025-08-11 15:25:39 -05:00
parent ad2ec35bfb
commit d9c8f97903
4 changed files with 216 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
//! a traverser and display directional animated textures.
use glam::Vec2;
use pathfinding::prelude::dijkstra;
use rand::prelude::*;
use smallvec::SmallVec;
@@ -175,6 +176,40 @@ impl Ghost {
Vec2::new(pos.x + BOARD_PIXEL_OFFSET.x as f32, pos.y + BOARD_PIXEL_OFFSET.y as f32)
}
/// Calculates the shortest path from the ghost's current position to a target node using Dijkstra's algorithm.
///
/// Returns a vector of NodeIds representing the path, or None if no path exists.
/// The path includes the current node and the target node.
pub fn calculate_path_to_target(&self, graph: &Graph, target: NodeId) -> Option<Vec<NodeId>> {
let start_node = self.traverser.position.from_node_id();
// Use Dijkstra's algorithm to find the shortest path
let result = dijkstra(
&start_node,
|&node_id| {
// Get all edges from the current node
graph.adjacency_list[node_id]
.edges()
.filter(|edge| can_ghost_traverse(*edge))
.map(|edge| (edge.target, (edge.distance * 100.0) as u32))
.collect::<Vec<_>>()
},
|&node_id| node_id == target,
);
result.map(|(path, _cost)| path)
}
/// Returns the ghost's color for debug rendering.
pub fn debug_color(&self) -> sdl2::pixels::Color {
match self.ghost_type {
GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
GhostType::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
GhostType::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
}
}
/// Renders the ghost at its current position.
///
/// Draws the appropriate directional sprite based on the ghost's

View File

@@ -111,6 +111,17 @@ impl Pacman {
}
}
/// Returns the current node ID that Pac-Man is at or moving towards.
///
/// If Pac-Man is at a node, returns that node ID.
/// If Pac-Man is between nodes, returns the node it's moving towards.
pub fn current_node_id(&self) -> NodeId {
match self.traverser.position {
Position::AtNode(node_id) => node_id,
Position::BetweenNodes { to, .. } => to,
}
}
/// Renders Pac-Man to the canvas.
///
/// Calculates screen position, determines if Pac-Man is stopped,

View File

@@ -173,12 +173,65 @@ impl Game {
if self.debug_mode {
self.map
.debug_render_with_cursor(canvas, &mut self.text_texture, &mut self.atlas, cursor_pos);
self.render_pathfinding_debug(canvas)?;
}
self.draw_hud(canvas)?;
canvas.present();
Ok(())
}
/// Renders pathfinding debug lines from each ghost to Pac-Man.
///
/// Each ghost's path is drawn in its respective color with a small offset
/// to prevent overlapping lines.
fn render_pathfinding_debug<T: RenderTarget>(&self, canvas: &mut Canvas<T>) -> Result<()> {
let pacman_node = self.pacman.current_node_id();
for (i, ghost) in self.ghosts.iter().enumerate() {
if let Some(path) = ghost.calculate_path_to_target(&self.map.graph, pacman_node) {
if path.len() < 2 {
continue; // Skip if path is too short
}
// Set the ghost's color
canvas.set_draw_color(ghost.debug_color());
// Calculate offset based on ghost index to prevent overlapping lines
let offset = (i as f32) * 2.0 - 3.0; // Offset range: -3.0 to 3.0
// Calculate a consistent offset direction for the entire path
let first_node = self.map.graph.get_node(path[0]).unwrap();
let last_node = self.map.graph.get_node(path[path.len() - 1]).unwrap();
let first_pos = first_node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
let last_pos = last_node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
// Use the overall direction from start to end to determine the perpendicular offset
let overall_dir = (last_pos - first_pos).normalize();
let perp_dir = glam::Vec2::new(-overall_dir.y, overall_dir.x);
// Calculate offset positions for all nodes using the same perpendicular direction
let mut offset_positions = Vec::new();
for &node_id in &path {
let node = self.map.graph.get_node(node_id).unwrap();
let pos = node.position + crate::constants::BOARD_PIXEL_OFFSET.as_vec2();
offset_positions.push(pos + perp_dir * offset);
}
// Draw lines between the offset positions
for window in offset_positions.windows(2) {
canvas
.draw_line(
(window[0].x as i32, window[0].y as i32),
(window[1].x as i32, window[1].y as i32),
)
.map_err(anyhow::Error::msg)?;
}
}
}
Ok(())
}
fn draw_hud<T: RenderTarget>(&mut self, canvas: &mut Canvas<T>) -> Result<()> {
let lives = 3;
let score_text = format!("{:02}", self.score);