From b1b03b0e9c125744a4a92923e05ae152d252ff7b Mon Sep 17 00:00:00 2001 From: Ryan Walters Date: Mon, 1 Sep 2025 15:47:41 -0500 Subject: [PATCH] refactor: move magic numbers & constants --- src/constants.rs | 34 ++++++++++++++++++++++++++++++++++ src/game.rs | 19 +++++++++++-------- 2 files changed, 45 insertions(+), 8 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 42b507b..034cd1f 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -49,6 +49,26 @@ pub const CANVAS_SIZE: UVec2 = UVec2::new( (BOARD_CELL_SIZE.y + BOARD_CELL_OFFSET.y) * CELL_SIZE, ); +/// Collider size constants for different entity types +pub mod collider { + use super::CELL_SIZE; + + /// Collider size for player and ghosts (1.375x cell size) + pub const PLAYER_GHOST_SIZE: f32 = CELL_SIZE as f32 * 1.375; + /// Collider size for pellets (0.4x cell size) + pub const PELLET_SIZE: f32 = CELL_SIZE as f32 * 0.4; + /// Collider size for power pellets/energizers (0.95x cell size) + pub const POWER_PELLET_SIZE: f32 = CELL_SIZE as f32 * 0.95; +} + +/// UI and rendering constants +pub mod ui { + /// Debug font size in points + pub const DEBUG_FONT_SIZE: u16 = 12; + /// Power pellet blink rate in seconds + pub const POWER_PELLET_BLINK_RATE: f32 = 0.2; +} + /// Map tile types that define gameplay behavior and collision properties. #[derive(Debug, Clone, Copy, PartialEq)] pub enum MapTile { @@ -100,3 +120,17 @@ pub const RAW_BOARD: [&str; BOARD_CELL_SIZE.y as usize] = [ "#..........................#", "############################", ]; + +/// Game initialization constants +pub mod startup { + /// Number of frames for the startup sequence (3 seconds at 60 FPS) + pub const STARTUP_FRAMES: u32 = 60 * 3; + /// Number of ticks per frame during startup + pub const STARTUP_TICKS_PER_FRAME: u32 = 60; +} + +/// Game mechanics constants +pub mod mechanics { + /// Player movement speed multiplier + pub const PLAYER_SPEED: f32 = 1.15; +} diff --git a/src/game.rs b/src/game.rs index 9b49cd1..3d8aa93 100644 --- a/src/game.rs +++ b/src/game.rs @@ -110,7 +110,7 @@ impl Game { let static_font_data: &'static [u8] = Box::leak(font_data.to_vec().into_boxed_slice()); let font_asset = RWops::from_bytes(static_font_data).map_err(|_| GameError::Sdl("Failed to load font".to_string()))?; let debug_font = ttf_context - .load_font_from_rwops(font_asset, 12) + .load_font_from_rwops(font_asset, constants::ui::DEBUG_FONT_SIZE) .map_err(|e| GameError::Sdl(e.to_string()))?; // Initialize audio system @@ -213,7 +213,7 @@ impl Game { node: map.start_positions.pacman, }, velocity: Velocity { - speed: 1.15, + speed: constants::mechanics::PLAYER_SPEED, direction: Direction::Left, }, movement_modifiers: MovementModifiers::default(), @@ -226,7 +226,7 @@ impl Game { directional_animation: DirectionalAnimation::new(moving_tiles, stopped_tiles, 5), entity_type: EntityType::Player, collider: Collider { - size: constants::CELL_SIZE as f32 * 1.375, + size: constants::collider::PLAYER_GHOST_SIZE, }, pacman_collider: PacmanCollider, }; @@ -249,7 +249,10 @@ impl Game { world.insert_resource(DebugState::default()); world.insert_resource(AudioState::default()); world.insert_resource(CursorPosition::default()); - world.insert_resource(StartupSequence::new(60 * 3, 60)); + world.insert_resource(StartupSequence::new( + constants::startup::STARTUP_FRAMES, + constants::startup::STARTUP_TICKS_PER_FRAME, + )); world.insert_non_send_resource(atlas); world.insert_non_send_resource(event_pump); @@ -337,12 +340,12 @@ impl Game { .resource::() .iter_nodes() .filter_map(|(id, tile)| match tile { - MapTile::Pellet => Some((*id, EntityType::Pellet, pellet_sprite, constants::CELL_SIZE as f32 * 0.4)), + MapTile::Pellet => Some((*id, EntityType::Pellet, pellet_sprite, constants::collider::PELLET_SIZE)), MapTile::PowerPellet => Some(( *id, EntityType::PowerPellet, energizer_sprite, - constants::CELL_SIZE as f32 * 0.95, + constants::collider::POWER_PELLET_SIZE, )), _ => None, }) @@ -360,7 +363,7 @@ impl Game { // Make power pellets blink if item_type == EntityType::PowerPellet { - item.insert((Frozen, Blinking::new(0.2))); + item.insert((Frozen, Blinking::new(constants::ui::POWER_PELLET_BLINK_RATE))); } } @@ -412,7 +415,7 @@ impl Game { directional_animation: animations, entity_type: EntityType::Ghost, collider: Collider { - size: constants::CELL_SIZE as f32 * 1.375, + size: constants::collider::PLAYER_GHOST_SIZE, }, ghost_collider: GhostCollider, ghost_state: GhostState::Normal,