Compare commits

...

4 Commits

8 changed files with 74 additions and 86 deletions

View File

@@ -33,7 +33,7 @@ pub struct App<'a> {
last_tick: Instant, last_tick: Instant,
} }
impl<'a> App<'a> { impl App<'_> {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?; let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?; let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;

View File

@@ -140,15 +140,14 @@ impl Audio {
/// ///
/// If audio is disabled, this function does nothing. /// If audio is disabled, this function does nothing.
pub fn set_mute(&mut self, mute: bool) { pub fn set_mute(&mut self, mute: bool) {
if self.disabled { if !self.disabled {
return; let channels = 4;
let volume = if mute { 0 } else { 32 };
for i in 0..channels {
mixer::Channel(i).set_volume(volume);
}
} }
let channels = 4;
let volume = if mute { 0 } else { 32 };
for i in 0..channels {
mixer::Channel(i).set_volume(volume);
}
self.muted = mute; self.muted = mute;
} }
@@ -158,6 +157,7 @@ impl Audio {
} }
/// Returns `true` if the audio system is disabled. /// Returns `true` if the audio system is disabled.
#[allow(dead_code)]
pub fn is_disabled(&self) -> bool { pub fn is_disabled(&self) -> bool {
self.disabled self.disabled
} }

View File

@@ -37,8 +37,6 @@ pub enum MapTile {
Pellet, Pellet,
/// A power pellet. /// A power pellet.
PowerPellet, PowerPellet,
/// A starting position for an entity.
StartingPosition(u8),
/// A tunnel tile. /// A tunnel tile.
Tunnel, Tunnel,
} }
@@ -68,7 +66,7 @@ pub const RAW_BOARD: [&str; BOARD_CELL_SIZE.y as usize] = [
"#............##............#", "#............##............#",
"#.####.#####.##.#####.####.#", "#.####.#####.##.#####.####.#",
"#.####.#####.##.#####.####.#", "#.####.#####.##.#####.####.#",
"#o..##.......0 .......##..o#", "#o..##.......X .......##..o#",
"###.##.##.########.##.##.###", "###.##.##.########.##.##.###",
"###.##.##.########.##.##.###", "###.##.##.########.##.##.###",
"#......##....##....##......#", "#......##....##....##......#",
@@ -139,30 +137,12 @@ mod tests {
fn test_map_tile_variants() { fn test_map_tile_variants() {
assert_ne!(MapTile::Empty, MapTile::Wall); assert_ne!(MapTile::Empty, MapTile::Wall);
assert_ne!(MapTile::Pellet, MapTile::PowerPellet); assert_ne!(MapTile::Pellet, MapTile::PowerPellet);
assert_ne!(MapTile::StartingPosition(0), MapTile::StartingPosition(1));
assert_ne!(MapTile::Tunnel, MapTile::Empty); assert_ne!(MapTile::Tunnel, MapTile::Empty);
} }
#[test]
fn test_map_tile_starting_position() {
let pos0 = MapTile::StartingPosition(0);
let pos1 = MapTile::StartingPosition(1);
let pos0_clone = MapTile::StartingPosition(0);
assert_eq!(pos0, pos0_clone);
assert_ne!(pos0, pos1);
}
#[test]
fn test_map_tile_debug() {
let tile = MapTile::Wall;
let debug_str = format!("{:?}", tile);
assert!(!debug_str.is_empty());
}
#[test] #[test]
fn test_map_tile_clone() { fn test_map_tile_clone() {
let original = MapTile::StartingPosition(5); let original = MapTile::Wall;
let cloned = original; let cloned = original;
assert_eq!(original, cloned); assert_eq!(original, cloned);
} }
@@ -217,10 +197,10 @@ mod tests {
#[test] #[test]
fn test_raw_board_starting_position() { fn test_raw_board_starting_position() {
// Should have a starting position '0' for Pac-Man // Should have a starting position 'X' for Pac-Man
let mut found_starting_position = false; let mut found_starting_position = false;
for row in RAW_BOARD.iter() { for row in RAW_BOARD.iter() {
if row.contains('0') { if row.contains('X') {
found_starting_position = true; found_starting_position = true;
break; break;
} }

View File

@@ -11,6 +11,8 @@ use sdl2::render::{Canvas, RenderTarget};
use std::collections::{HashMap, VecDeque}; use std::collections::{HashMap, VecDeque};
use tracing::debug; use tracing::debug;
/// The starting positions of the entities in the game.
#[allow(dead_code)]
pub struct NodePositions { pub struct NodePositions {
pub pacman: NodeId, pub pacman: NodeId,
pub blinky: NodeId, pub blinky: NodeId,
@@ -19,20 +21,20 @@ pub struct NodePositions {
pub clyde: NodeId, pub clyde: NodeId,
} }
/// The game map, responsible for holding the tile-based layout and the navigation graph. /// The main map structure containing the game board and navigation graph.
///
/// The map is represented as a 2D array of `MapTile`s. It also stores a navigation
/// `Graph` that entities like Pac-Man and ghosts use for movement. The graph is
/// generated from the walkable tiles of the map.
pub struct Map { pub struct Map {
/// The current state of the map. /// The current state of the map.
#[allow(dead_code)]
current: [[MapTile; BOARD_CELL_SIZE.y as usize]; BOARD_CELL_SIZE.x as usize], current: [[MapTile; BOARD_CELL_SIZE.y as usize]; BOARD_CELL_SIZE.x as usize],
/// The node map for entity movement. /// The node map for entity movement.
pub graph: Graph, pub graph: Graph,
/// A mapping from grid positions to node IDs. /// A mapping from grid positions to node IDs.
pub grid_to_node: HashMap<IVec2, NodeId>, pub grid_to_node: HashMap<IVec2, NodeId>,
/// A mapping of the starting positions of the entities. /// A mapping of the starting positions of the entities.
#[allow(dead_code)]
pub start_positions: NodePositions, pub start_positions: NodePositions,
/// Pac-Man's starting position.
pacman_start: Option<IVec2>,
} }
impl Map { impl Map {
@@ -51,6 +53,7 @@ impl Map {
let map = parsed_map.tiles; let map = parsed_map.tiles;
let house_door = parsed_map.house_door; let house_door = parsed_map.house_door;
let tunnel_ends = parsed_map.tunnel_ends; let tunnel_ends = parsed_map.tunnel_ends;
let pacman_start = parsed_map.pacman_start;
let mut graph = Graph::new(); let mut graph = Graph::new();
let mut grid_to_node = HashMap::new(); let mut grid_to_node = HashMap::new();
@@ -58,25 +61,7 @@ impl Map {
let cell_offset = Vec2::splat(CELL_SIZE as f32 / 2.0); let cell_offset = Vec2::splat(CELL_SIZE as f32 / 2.0);
// Find a starting point for the graph generation, preferably Pac-Man's position. // Find a starting point for the graph generation, preferably Pac-Man's position.
let start_pos = (0..BOARD_CELL_SIZE.y) let start_pos = pacman_start.expect("Pac-Man's starting position not found");
.flat_map(|y| (0..BOARD_CELL_SIZE.x).map(move |x| IVec2::new(x as i32, y as i32)))
.find(|&p| matches!(map[p.x as usize][p.y as usize], MapTile::StartingPosition(0)))
.unwrap_or_else(|| {
// Fallback to any valid walkable tile if Pac-Man's start is not found
(0..BOARD_CELL_SIZE.y)
.flat_map(|y| (0..BOARD_CELL_SIZE.x).map(move |x| IVec2::new(x as i32, y as i32)))
.find(|&p| {
matches!(
map[p.x as usize][p.y as usize],
MapTile::Pellet
| MapTile::PowerPellet
| MapTile::Empty
| MapTile::Tunnel
| MapTile::StartingPosition(_)
)
})
.expect("No valid starting position found on map for graph generation")
});
// Add the starting position to the graph/queue // Add the starting position to the graph/queue
let mut queue = VecDeque::new(); let mut queue = VecDeque::new();
@@ -110,7 +95,7 @@ impl Map {
// Skip if the new position is not a walkable tile // Skip if the new position is not a walkable tile
if matches!( if matches!(
map[new_position.x as usize][new_position.y as usize], map[new_position.x as usize][new_position.y as usize],
MapTile::Pellet | MapTile::PowerPellet | MapTile::Empty | MapTile::Tunnel | MapTile::StartingPosition(_) MapTile::Pellet | MapTile::PowerPellet | MapTile::Empty | MapTile::Tunnel
) { ) {
// Add the new position to the graph/queue // Add the new position to the graph/queue
let pos = Vec2::new( let pos = Vec2::new(
@@ -167,9 +152,10 @@ impl Map {
Map { Map {
current: map, current: map,
grid_to_node,
graph, graph,
grid_to_node,
start_positions, start_positions,
pacman_start,
} }
} }
@@ -183,14 +169,9 @@ impl Map {
/// ///
/// The starting position as a grid coordinate (`UVec2`), or `None` if not found. /// The starting position as a grid coordinate (`UVec2`), or `None` if not found.
pub fn find_starting_position(&self, entity_id: u8) -> Option<UVec2> { pub fn find_starting_position(&self, entity_id: u8) -> Option<UVec2> {
for (x, col) in self.current.iter().enumerate().take(BOARD_CELL_SIZE.x as usize) { // For now, only Pac-Man (entity_id 0) is supported
for (y, &cell) in col.iter().enumerate().take(BOARD_CELL_SIZE.y as usize) { if entity_id == 0 {
if let MapTile::StartingPosition(id) = cell { return self.pacman_start.map(|pos| UVec2::new(pos.x as u32, pos.y as u32));
if id == entity_id {
return Some(UVec2::new(x as u32, y as u32));
}
}
}
} }
None None
} }
@@ -373,7 +354,7 @@ impl Map {
mod tests { mod tests {
use super::*; use super::*;
use crate::constants::{BOARD_CELL_SIZE, CELL_SIZE}; use crate::constants::{BOARD_CELL_SIZE, CELL_SIZE};
use glam::{IVec2, UVec2, Vec2}; use glam::{IVec2, Vec2};
fn create_minimal_test_board() -> [&'static str; BOARD_CELL_SIZE.y as usize] { fn create_minimal_test_board() -> [&'static str; BOARD_CELL_SIZE.y as usize] {
let mut board = [""; BOARD_CELL_SIZE.y as usize]; let mut board = [""; BOARD_CELL_SIZE.y as usize];
@@ -401,7 +382,7 @@ mod tests {
board[20] = "#............##............#"; board[20] = "#............##............#";
board[21] = "#.####.#####.##.#####.####.#"; board[21] = "#.####.#####.##.#####.####.#";
board[22] = "#.####.#####.##.#####.####.#"; board[22] = "#.####.#####.##.#####.####.#";
board[23] = "#o..##.......0 .......##..o#"; board[23] = "#o..##.......X .......##..o#";
board[24] = "###.##.##.########.##.##.###"; board[24] = "###.##.##.########.##.##.###";
board[25] = "###.##.##.########.##.##.###"; board[25] = "###.##.##.########.##.##.###";
board[26] = "#......##....##....##......#"; board[26] = "#......##....##....##......#";
@@ -517,7 +498,7 @@ mod tests {
// Check that adjacent walkable tiles are connected // Check that adjacent walkable tiles are connected
// Find any node that has connections // Find any node that has connections
let mut found_connected_node = false; let mut found_connected_node = false;
for (grid_pos, &node_id) in &map.grid_to_node { for &node_id in map.grid_to_node.values() {
let intersection = &map.graph.adjacency_list[node_id]; let intersection = &map.graph.adjacency_list[node_id];
if intersection.edges().next().is_some() { if intersection.edges().next().is_some() {
found_connected_node = true; found_connected_node = true;

View File

@@ -22,6 +22,8 @@ pub struct ParsedMap {
pub house_door: [Option<IVec2>; 2], pub house_door: [Option<IVec2>; 2],
/// The positions of the tunnel end tiles. /// The positions of the tunnel end tiles.
pub tunnel_ends: [Option<IVec2>; 2], pub tunnel_ends: [Option<IVec2>; 2],
/// Pac-Man's starting position.
pub pacman_start: Option<IVec2>,
} }
/// Parser for converting raw board layouts into structured map data. /// Parser for converting raw board layouts into structured map data.
@@ -44,8 +46,8 @@ impl MapTileParser {
'o' => Ok(MapTile::PowerPellet), 'o' => Ok(MapTile::PowerPellet),
' ' => Ok(MapTile::Empty), ' ' => Ok(MapTile::Empty),
'T' => Ok(MapTile::Tunnel), 'T' => Ok(MapTile::Tunnel),
c @ '0'..='4' => Ok(MapTile::StartingPosition(c.to_digit(10).unwrap() as u8)), 'X' => Ok(MapTile::Empty), // Pac-Man's starting position, treated as empty
'=' => Ok(MapTile::Wall), // House door is represented as a wall tile '=' => Ok(MapTile::Wall), // House door is represented as a wall tile
_ => Err(ParseError::UnknownCharacter(c)), _ => Err(ParseError::UnknownCharacter(c)),
} }
} }
@@ -68,6 +70,7 @@ impl MapTileParser {
let mut tiles = [[MapTile::Empty; BOARD_CELL_SIZE.y as usize]; BOARD_CELL_SIZE.x as usize]; let mut tiles = [[MapTile::Empty; BOARD_CELL_SIZE.y as usize]; BOARD_CELL_SIZE.x as usize];
let mut house_door = [None; 2]; let mut house_door = [None; 2];
let mut tunnel_ends = [None; 2]; let mut tunnel_ends = [None; 2];
let mut pacman_start: Option<IVec2> = None;
for (y, line) in raw_board.iter().enumerate().take(BOARD_CELL_SIZE.y as usize) { for (y, line) in raw_board.iter().enumerate().take(BOARD_CELL_SIZE.y as usize) {
for (x, character) in line.chars().enumerate().take(BOARD_CELL_SIZE.x as usize) { for (x, character) in line.chars().enumerate().take(BOARD_CELL_SIZE.x as usize) {
@@ -92,6 +95,11 @@ impl MapTileParser {
_ => {} _ => {}
} }
// Track Pac-Man's starting position
if character == 'X' {
pacman_start = Some(IVec2::new(x as i32, y as i32));
}
tiles[x][y] = tile; tiles[x][y] = tile;
} }
} }
@@ -106,6 +114,7 @@ impl MapTileParser {
tiles, tiles,
house_door, house_door,
tunnel_ends, tunnel_ends,
pacman_start,
}) })
} }
} }
@@ -122,18 +131,11 @@ mod tests {
assert!(matches!(MapTileParser::parse_character('o').unwrap(), MapTile::PowerPellet)); assert!(matches!(MapTileParser::parse_character('o').unwrap(), MapTile::PowerPellet));
assert!(matches!(MapTileParser::parse_character(' ').unwrap(), MapTile::Empty)); assert!(matches!(MapTileParser::parse_character(' ').unwrap(), MapTile::Empty));
assert!(matches!(MapTileParser::parse_character('T').unwrap(), MapTile::Tunnel)); assert!(matches!(MapTileParser::parse_character('T').unwrap(), MapTile::Tunnel));
assert!(matches!( assert!(matches!(MapTileParser::parse_character('X').unwrap(), MapTile::Empty));
MapTileParser::parse_character('0').unwrap(),
MapTile::StartingPosition(0)
));
assert!(matches!(
MapTileParser::parse_character('4').unwrap(),
MapTile::StartingPosition(4)
));
assert!(matches!(MapTileParser::parse_character('=').unwrap(), MapTile::Wall)); assert!(matches!(MapTileParser::parse_character('=').unwrap(), MapTile::Wall));
// Test invalid character // Test invalid character
assert!(MapTileParser::parse_character('X').is_err()); assert!(MapTileParser::parse_character('Z').is_err());
} }
#[test] #[test]
@@ -154,15 +156,18 @@ mod tests {
// Verify we found tunnel ends // Verify we found tunnel ends
assert!(parsed.tunnel_ends[0].is_some()); assert!(parsed.tunnel_ends[0].is_some());
assert!(parsed.tunnel_ends[1].is_some()); assert!(parsed.tunnel_ends[1].is_some());
// Verify we found Pac-Man's starting position
assert!(parsed.pacman_start.is_some());
} }
#[test] #[test]
fn test_parse_board_invalid_character() { fn test_parse_board_invalid_character() {
let mut invalid_board = RAW_BOARD.clone(); let mut invalid_board = RAW_BOARD.clone();
invalid_board[0] = "###########################X"; invalid_board[0] = "###########################Z";
let result = MapTileParser::parse_board(invalid_board); let result = MapTileParser::parse_board(invalid_board);
assert!(result.is_err()); assert!(result.is_err());
assert!(matches!(result.unwrap_err(), ParseError::UnknownCharacter('X'))); assert!(matches!(result.unwrap_err(), ParseError::UnknownCharacter('Z')));
} }
} }

View File

@@ -50,19 +50,26 @@ impl AnimatedTexture {
tile.render(canvas, atlas, dest) tile.render(canvas, atlas, dest)
} }
// Helper methods for testing /// Returns the current frame index.
#[allow(dead_code)]
pub fn current_frame(&self) -> usize { pub fn current_frame(&self) -> usize {
self.current_frame self.current_frame
} }
/// Returns the time bank.
#[allow(dead_code)]
pub fn time_bank(&self) -> f32 { pub fn time_bank(&self) -> f32 {
self.time_bank self.time_bank
} }
/// Returns the frame duration.
#[allow(dead_code)]
pub fn frame_duration(&self) -> f32 { pub fn frame_duration(&self) -> f32 {
self.frame_duration self.frame_duration
} }
/// Returns the number of tiles in the animation.
#[allow(dead_code)]
pub fn tiles_len(&self) -> usize { pub fn tiles_len(&self) -> usize {
self.tiles.len() self.tiles.len()
} }

View File

@@ -55,19 +55,26 @@ impl DirectionalAnimatedTexture {
} }
} }
// Helper methods for testing /// Returns true if the texture has a direction.
#[allow(dead_code)]
pub fn has_direction(&self, direction: Direction) -> bool { pub fn has_direction(&self, direction: Direction) -> bool {
self.textures.contains_key(&direction) self.textures.contains_key(&direction)
} }
/// Returns true if the texture has a stopped direction.
#[allow(dead_code)]
pub fn has_stopped_direction(&self, direction: Direction) -> bool { pub fn has_stopped_direction(&self, direction: Direction) -> bool {
self.stopped_textures.contains_key(&direction) self.stopped_textures.contains_key(&direction)
} }
/// Returns the number of textures.
#[allow(dead_code)]
pub fn texture_count(&self) -> usize { pub fn texture_count(&self) -> usize {
self.textures.len() self.textures.len()
} }
/// Returns the number of stopped textures.
#[allow(dead_code)]
pub fn stopped_texture_count(&self) -> usize { pub fn stopped_texture_count(&self) -> usize {
self.stopped_textures.len() self.stopped_textures.len()
} }

View File

@@ -50,11 +50,14 @@ impl AtlasTile {
Ok(()) Ok(())
} }
// Helper methods for testing /// Creates a new atlas tile.
#[allow(dead_code)]
pub fn new(pos: U16Vec2, size: U16Vec2, color: Option<Color>) -> Self { pub fn new(pos: U16Vec2, size: U16Vec2, color: Option<Color>) -> Self {
Self { pos, size, color } Self { pos, size, color }
} }
/// Sets the color of the tile.
#[allow(dead_code)]
pub fn with_color(mut self, color: Color) -> Self { pub fn with_color(mut self, color: Color) -> Self {
self.color = Some(color); self.color = Some(color);
self self
@@ -96,15 +99,20 @@ impl SpriteAtlas {
&self.texture &self.texture
} }
// Helper methods for testing /// Returns the number of tiles in the atlas.
#[allow(dead_code)]
pub fn tiles_count(&self) -> usize { pub fn tiles_count(&self) -> usize {
self.tiles.len() self.tiles.len()
} }
/// Returns true if the atlas has a tile with the given name.
#[allow(dead_code)]
pub fn has_tile(&self, name: &str) -> bool { pub fn has_tile(&self, name: &str) -> bool {
self.tiles.contains_key(name) self.tiles.contains_key(name)
} }
/// Returns the default color of the atlas.
#[allow(dead_code)]
pub fn default_color(&self) -> Option<Color> { pub fn default_color(&self) -> Option<Color> {
self.default_color self.default_color
} }