refactor: replace HashMap with fixed-size arrays for textures in DirectionalAnimatedTexture

This commit is contained in:
2025-08-11 11:13:46 -05:00
parent 6702b3723a
commit e0f8443e75
4 changed files with 26 additions and 34 deletions

View File

@@ -29,9 +29,9 @@ impl Direction {
self.into()
}
/// Returns the direction as a u8 (0-3). Constant time.
/// Returns the direction as a usize (0-3). Constant time.
/// This is useful for indexing into arrays.
pub const fn as_u8(self) -> u8 {
pub const fn as_usize(self) -> usize {
match self {
Direction::Up => 0,
Direction::Down => 1,
@@ -51,5 +51,3 @@ impl From<Direction> for IVec2 {
}
}
}
pub const DIRECTIONS: [Direction; 4] = [Direction::Up, Direction::Down, Direction::Left, Direction::Right];

View File

@@ -15,7 +15,6 @@ use crate::texture::directional::DirectionalAnimatedTexture;
use crate::texture::sprite::SpriteAtlas;
use sdl2::keyboard::Keycode;
use sdl2::render::{Canvas, RenderTarget};
use std::collections::HashMap;
/// Determines if Pac-Man can traverse a given edge.
///
@@ -41,8 +40,8 @@ impl Pacman {
/// Sets up animated textures for all four directions with moving and stopped states.
/// The moving animation cycles through open mouth, closed mouth, and full sprites.
pub fn new(graph: &Graph, start_node: NodeId, atlas: &SpriteAtlas) -> Self {
let mut textures = HashMap::new();
let mut stopped_textures = HashMap::new();
let mut textures = [None, None, None, None];
let mut stopped_textures = [None, None, None, None];
for direction in Direction::DIRECTIONS {
let moving_prefix = match direction {
@@ -59,14 +58,9 @@ impl Pacman {
let stopped_tiles = vec![SpriteAtlas::get_tile(atlas, &format!("{moving_prefix}_b.png")).unwrap()];
textures.insert(
direction,
AnimatedTexture::new(moving_tiles, 0.08).expect("Invalid frame duration"),
);
stopped_textures.insert(
direction,
AnimatedTexture::new(stopped_tiles, 0.1).expect("Invalid frame duration"),
);
textures[direction.as_usize()] = Some(AnimatedTexture::new(moving_tiles, 0.08).expect("Invalid frame duration"));
stopped_textures[direction.as_usize()] =
Some(AnimatedTexture::new(stopped_tiles, 0.1).expect("Invalid frame duration"));
}
Self {