From ad3f896f8291fd3df6c6cd96d4dc5de873b13da7 Mon Sep 17 00:00:00 2001 From: Ryan Walters Date: Thu, 28 Aug 2025 12:54:52 -0500 Subject: [PATCH] chore: reorganize component definitions into relevant system files --- src/game.rs | 30 ++--- src/systems/audio.rs | 12 +- src/systems/collision.rs | 73 ++++++++++- src/systems/components.rs | 262 +++++--------------------------------- src/systems/debug.rs | 6 +- src/systems/ghost.rs | 64 ++-------- src/systems/item.rs | 5 +- src/systems/mod.rs | 14 ++ src/systems/player.rs | 108 ++++++++++++++-- src/systems/render.rs | 7 +- src/systems/stage.rs | 73 ++++++++++- tests/collision.rs | 4 +- tests/item.rs | 6 +- tests/player.rs | 6 +- 14 files changed, 329 insertions(+), 341 deletions(-) diff --git a/src/game.rs b/src/game.rs index b6f0769..7d32fdb 100644 --- a/src/game.rs +++ b/src/game.rs @@ -7,29 +7,19 @@ use crate::error::{GameError, GameResult, TextureError}; use crate::events::GameEvent; use crate::map::builder::Map; use crate::map::direction::Direction; -use crate::systems; use crate::systems::blinking::Blinking; +use crate::systems::{self, ghost_collision_system}; use crate::systems::movement::{BufferedDirection, Position, Velocity}; use crate::systems::profiling::SystemId; use crate::systems::render::RenderDirty; use crate::systems::{ - audio::{audio_system, AudioEvent, AudioResource}, - blinking::blinking_system, - collision::collision_system, - components::{ - AudioState, Collider, DeltaTime, DirectionalAnimated, EntityType, Frozen, Ghost, GhostBundle, GhostCollider, GlobalState, - ItemBundle, ItemCollider, LevelTiming, PacmanCollider, PlayerBundle, PlayerControlled, PlayerStateBundle, Renderable, - ScoreResource, StartupSequence, - }, - debug::{debug_render_system, DebugFontResource, DebugState, DebugTextureResource}, - ghost::{ghost_collision_system, ghost_movement_system}, - item::item_system, - profiling::{profile, SystemTimings}, - render::{ - directional_render_system, dirty_render_system, hud_render_system, ready_visibility_system, render_system, - BackbufferResource, MapTextureResource, - }, + audio_system, blinking_system, collision_system, debug_render_system, directional_render_system, dirty_render_system, + ghost_movement_system, hud_render_system, item_system, profile, ready_visibility_system, render_system, AudioEvent, + AudioResource, AudioState, BackbufferResource, Collider, DebugFontResource, DebugState, DebugTextureResource, DeltaTime, + DirectionalAnimated, EntityType, Frozen, Ghost, GhostBundle, GhostCollider, GlobalState, ItemBundle, ItemCollider, + LevelTiming, MapTextureResource, PacmanCollider, PlayerBundle, PlayerControlled, PlayerStateBundle, Renderable, + ScoreResource, StartupSequence, SystemTimings, }; use crate::texture::animated::AnimatedTexture; use bevy_ecs::event::EventRegistry; @@ -249,9 +239,9 @@ impl Game { ); let input_system = profile(SystemId::Input, systems::input::input_system); - let player_control_system = profile(SystemId::PlayerControls, systems::player::player_control_system); - let player_movement_system = profile(SystemId::PlayerMovement, systems::player::player_movement_system); - let startup_stage_system = profile(SystemId::Stage, systems::stage::startup_stage_system); + let player_control_system = profile(SystemId::PlayerControls, systems::player_control_system); + let player_movement_system = profile(SystemId::PlayerMovement, systems::player_movement_system); + let startup_stage_system = profile(SystemId::Stage, systems::startup_stage_system); let player_tunnel_slowdown_system = profile(SystemId::PlayerMovement, systems::player::player_tunnel_slowdown_system); let ghost_movement_system = profile(SystemId::Ghost, ghost_movement_system); let collision_system = profile(SystemId::Collision, collision_system); diff --git a/src/systems/audio.rs b/src/systems/audio.rs index 915cc83..a20fd58 100644 --- a/src/systems/audio.rs +++ b/src/systems/audio.rs @@ -6,10 +6,20 @@ use bevy_ecs::{ event::{Event, EventReader, EventWriter}, + resource::Resource, system::{NonSendMut, ResMut}, }; -use crate::{audio::Audio, error::GameError, systems::components::AudioState}; +use crate::{audio::Audio, error::GameError}; + +/// Resource for tracking audio state +#[derive(Resource, Debug, Clone, Default)] +pub struct AudioState { + /// Whether audio is currently muted + pub muted: bool, + /// Current sound index for cycling through eat sounds + pub sound_index: usize, +} /// Events for triggering audio playback #[derive(Event, Debug, Clone, Copy, PartialEq, Eq)] diff --git a/src/systems/collision.rs b/src/systems/collision.rs index c120ef8..2ba2763 100644 --- a/src/systems/collision.rs +++ b/src/systems/collision.rs @@ -1,13 +1,37 @@ +use bevy_ecs::component::Component; use bevy_ecs::entity::Entity; -use bevy_ecs::event::EventWriter; +use bevy_ecs::event::{EventReader, EventWriter}; use bevy_ecs::query::With; -use bevy_ecs::system::{Query, Res}; +use bevy_ecs::system::{Commands, Query, Res, ResMut}; use crate::error::GameError; use crate::events::GameEvent; use crate::map::builder::Map; -use crate::systems::components::{Collider, GhostCollider, ItemCollider, PacmanCollider}; use crate::systems::movement::Position; +use crate::systems::{AudioEvent, CombatState, Ghost, PlayerControlled, ScoreResource}; + +#[derive(Component)] +pub struct Collider { + pub size: f32, +} + +impl Collider { + /// Checks if this collider collides with another collider at the given distance. + pub fn collides_with(&self, other_size: f32, distance: f32) -> bool { + let collision_distance = (self.size + other_size) / 2.0; + distance < collision_distance + } +} + +/// Marker components for collision filtering optimization +#[derive(Component)] +pub struct PacmanCollider; + +#[derive(Component)] +pub struct GhostCollider; + +#[derive(Component)] +pub struct ItemCollider; /// Helper function to check collision between two entities with colliders. pub fn check_collision( @@ -82,3 +106,46 @@ pub fn collision_system( } } } + +pub fn ghost_collision_system( + mut commands: Commands, + mut collision_events: EventReader, + mut score: ResMut, + pacman_query: Query<&CombatState, With>, + ghost_query: Query<(Entity, &Ghost), With>, + mut events: EventWriter, +) { + for event in collision_events.read() { + if let GameEvent::Collision(entity1, entity2) = event { + // Check if one is Pacman and the other is a ghost + let (pacman_entity, ghost_entity) = if pacman_query.get(*entity1).is_ok() && ghost_query.get(*entity2).is_ok() { + (*entity1, *entity2) + } else if pacman_query.get(*entity2).is_ok() && ghost_query.get(*entity1).is_ok() { + (*entity2, *entity1) + } else { + continue; + }; + + // Check if Pac-Man is energized + if let Ok(combat_state) = pacman_query.get(pacman_entity) { + if combat_state.is_energized() { + // Pac-Man eats the ghost + if let Ok((ghost_ent, _ghost_type)) = ghost_query.get(ghost_entity) { + // Add score (200 points per ghost eaten) + score.0 += 200; + + // Remove the ghost + commands.entity(ghost_ent).despawn(); + + // Play eat sound + events.write(AudioEvent::PlayEat); + } + } else { + // Pac-Man dies (this would need a death system) + // For now, just log it + tracing::warn!("Pac-Man collided with ghost while not energized!"); + } + } + } + } +} diff --git a/src/systems/components.rs b/src/systems/components.rs index e185046..b4d3a37 100644 --- a/src/systems/components.rs +++ b/src/systems/components.rs @@ -3,7 +3,10 @@ use bitflags::bitflags; use crate::{ map::graph::TraversalFlags, - systems::movement::{BufferedDirection, Position, Velocity}, + systems::{ + movement::{BufferedDirection, Position, Velocity}, + Collider, CombatState, ControlState, GhostCollider, ItemCollider, PacmanCollider, PlayerLifecycle, + }, texture::{animated::AnimatedTexture, sprite::AtlasTile}, }; @@ -109,63 +112,6 @@ bitflags! { } } -#[derive(Component)] -pub struct Collider { - pub size: f32, -} - -impl Collider { - /// Checks if this collider collides with another collider at the given distance. - pub fn collides_with(&self, other_size: f32, distance: f32) -> bool { - let collision_distance = (self.size + other_size) / 2.0; - distance < collision_distance - } -} - -/// Marker components for collision filtering optimization -#[derive(Component)] -pub struct PacmanCollider; - -#[derive(Component)] -pub struct GhostCollider; - -#[derive(Component)] -pub struct ItemCollider; - -#[derive(Bundle)] -pub struct PlayerBundle { - pub player: PlayerControlled, - pub position: Position, - pub velocity: Velocity, - pub buffered_direction: BufferedDirection, - pub sprite: Renderable, - pub directional_animated: DirectionalAnimated, - pub entity_type: EntityType, - pub collider: Collider, - pub pacman_collider: PacmanCollider, -} - -#[derive(Bundle)] -pub struct ItemBundle { - pub position: Position, - pub sprite: Renderable, - pub entity_type: EntityType, - pub collider: Collider, - pub item_collider: ItemCollider, -} - -#[derive(Bundle)] -pub struct GhostBundle { - pub ghost: Ghost, - pub position: Position, - pub velocity: Velocity, - pub sprite: Renderable, - pub directional_animated: DirectionalAnimated, - pub entity_type: EntityType, - pub collider: Collider, - pub ghost_collider: GhostCollider, -} - #[derive(Resource)] pub struct GlobalState { pub exit: bool, @@ -177,110 +123,6 @@ pub struct ScoreResource(pub u32); #[derive(Resource)] pub struct DeltaTime(pub f32); -/// Resource for tracking audio state -#[derive(Resource, Debug, Clone, Default)] -pub struct AudioState { - /// Whether audio is currently muted - pub muted: bool, - /// Current sound index for cycling through eat sounds - pub sound_index: usize, -} - -/// Lifecycle state for the player entity. -#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)] -pub enum PlayerLifecycle { - Spawning, - Alive, - Dying, - Respawning, -} - -impl PlayerLifecycle { - /// Returns true when gameplay input and movement should be active - pub fn is_interactive(self) -> bool { - matches!(self, PlayerLifecycle::Alive) - } -} - -impl Default for PlayerLifecycle { - fn default() -> Self { - PlayerLifecycle::Spawning - } -} - -/// Whether player input should be processed. -#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)] -pub enum ControlState { - InputEnabled, - InputLocked, -} - -impl Default for ControlState { - fn default() -> Self { - Self::InputLocked - } -} - -/// Combat-related state for Pac-Man. Tick-based energizer logic. -#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)] -pub enum CombatState { - Normal, - Energized { - /// Remaining energizer duration in ticks (frames) - remaining_ticks: u32, - /// Ticks until flashing begins (counts down to 0, then flashing is active) - flash_countdown_ticks: u32, - }, -} - -impl Default for CombatState { - fn default() -> Self { - CombatState::Normal - } -} - -impl CombatState { - pub fn is_energized(&self) -> bool { - matches!(self, CombatState::Energized { .. }) - } - - pub fn is_flashing(&self) -> bool { - matches!(self, CombatState::Energized { flash_countdown_ticks, .. } if *flash_countdown_ticks == 0) - } - - pub fn deactivate_energizer(&mut self) { - *self = CombatState::Normal; - } - - /// Activate energizer using tick-based durations. - pub fn activate_energizer_ticks(&mut self, total_ticks: u32, flash_lead_ticks: u32) { - let flash_countdown_ticks = total_ticks.saturating_sub(flash_lead_ticks); - *self = CombatState::Energized { - remaining_ticks: total_ticks, - flash_countdown_ticks, - }; - } - - /// Advance one frame. When ticks reach zero, returns to Normal. - pub fn tick_frame(&mut self) { - if let CombatState::Energized { - remaining_ticks, - flash_countdown_ticks, - } = self - { - if *remaining_ticks > 0 { - *remaining_ticks -= 1; - if *flash_countdown_ticks > 0 { - *flash_countdown_ticks -= 1; - } - } - if *remaining_ticks == 0 { - *self = CombatState::Normal; - } - } - } -} - /// Movement modifiers that can affect Pac-Man's speed or handling. #[derive(Component, Debug, Clone, Copy)] pub struct MovementModifiers { @@ -332,6 +174,19 @@ impl LevelTiming { #[derive(Component, Debug, Clone, Copy)] pub struct Frozen; +#[derive(Bundle)] +pub struct PlayerBundle { + pub player: PlayerControlled, + pub position: Position, + pub velocity: Velocity, + pub buffered_direction: BufferedDirection, + pub sprite: Renderable, + pub directional_animated: DirectionalAnimated, + pub entity_type: EntityType, + pub collider: Collider, + pub pacman_collider: PacmanCollider, +} + /// Convenience bundle for attaching the hybrid FSM to the player entity #[derive(Bundle, Default)] pub struct PlayerStateBundle { @@ -341,72 +196,23 @@ pub struct PlayerStateBundle { pub movement_modifiers: MovementModifiers, } -#[derive(Resource, Debug, Clone, Copy)] -pub enum StartupSequence { - /// Stage 1: Text-only stage - /// - Player & ghosts are hidden - /// - READY! and PLAYER ONE text are shown - /// - Energizers do not blink - TextOnly { - /// Remaining ticks in this stage - remaining_ticks: u32, - }, - /// Stage 2: Characters visible stage - /// - PLAYER ONE text is hidden, READY! text remains - /// - Ghosts and Pac-Man are now shown - CharactersVisible { - /// Remaining ticks in this stage - remaining_ticks: u32, - }, - /// Stage 3: Game begins - /// - Final state, game is fully active - GameActive, +#[derive(Bundle)] +pub struct ItemBundle { + pub position: Position, + pub sprite: Renderable, + pub entity_type: EntityType, + pub collider: Collider, + pub item_collider: ItemCollider, } -impl StartupSequence { - /// Creates a new StartupSequence with the specified duration in ticks - pub fn new(text_only_ticks: u32, _characters_visible_ticks: u32) -> Self { - Self::TextOnly { - remaining_ticks: text_only_ticks, - } - } - - /// Returns true if the timer is still active (not in GameActive state) - pub fn is_active(&self) -> bool { - !matches!(self, StartupSequence::GameActive) - } - - /// Returns true if we're in the game active stage - pub fn is_game_active(&self) -> bool { - matches!(self, StartupSequence::GameActive) - } - - /// Ticks the timer by one frame, returning transition information if state changes - pub fn tick(&mut self) -> Option<(StartupSequence, StartupSequence)> { - match self { - StartupSequence::TextOnly { remaining_ticks } => { - if *remaining_ticks > 0 { - *remaining_ticks -= 1; - None - } else { - let from = *self; - *self = StartupSequence::CharactersVisible { - remaining_ticks: 60, // 1 second at 60 FPS - }; - Some((from, *self)) - } - } - StartupSequence::CharactersVisible { remaining_ticks } => { - if *remaining_ticks > 0 { - *remaining_ticks -= 1; - None - } else { - let from = *self; - *self = StartupSequence::GameActive; - Some((from, *self)) - } - } - StartupSequence::GameActive => None, - } - } +#[derive(Bundle)] +pub struct GhostBundle { + pub ghost: Ghost, + pub position: Position, + pub velocity: Velocity, + pub sprite: Renderable, + pub directional_animated: DirectionalAnimated, + pub entity_type: EntityType, + pub collider: Collider, + pub ghost_collider: GhostCollider, } diff --git a/src/systems/debug.rs b/src/systems/debug.rs index cc85429..74b273b 100644 --- a/src/systems/debug.rs +++ b/src/systems/debug.rs @@ -3,11 +3,7 @@ use std::cmp::Ordering; use crate::constants::BOARD_PIXEL_OFFSET; use crate::map::builder::Map; -use crate::systems::components::Collider; -use crate::systems::input::CursorPosition; -use crate::systems::movement::Position; -use crate::systems::profiling::SystemTimings; -use crate::systems::render::BackbufferResource; +use crate::systems::{BackbufferResource, Collider, CursorPosition, Position, SystemTimings}; use bevy_ecs::prelude::*; use glam::{IVec2, UVec2, Vec2}; use sdl2::pixels::Color; diff --git a/src/systems/ghost.rs b/src/systems/ghost.rs index c362f9a..eef4f8e 100644 --- a/src/systems/ghost.rs +++ b/src/systems/ghost.rs @@ -1,15 +1,4 @@ -use bevy_ecs::entity::Entity; -use bevy_ecs::event::{EventReader, EventWriter}; -use bevy_ecs::query::{With, Without}; -use bevy_ecs::system::{Commands, Query, Res, ResMut}; -use rand::rngs::SmallRng; -use rand::seq::IndexedRandom; -use rand::SeedableRng; -use smallvec::SmallVec; - -use crate::events::GameEvent; -use crate::systems::audio::AudioEvent; -use crate::systems::components::{Frozen, GhostCollider, ScoreResource}; +use crate::systems::components::Frozen; use crate::{ map::{ builder::Map, @@ -17,10 +6,16 @@ use crate::{ graph::{Edge, TraversalFlags}, }, systems::{ - components::{CombatState, DeltaTime, Ghost, PlayerControlled}, + components::{DeltaTime, Ghost}, movement::{Position, Velocity}, }, }; +use bevy_ecs::query::Without; +use bevy_ecs::system::{Query, Res}; +use rand::rngs::SmallRng; +use rand::seq::IndexedRandom; +use rand::SeedableRng; +use smallvec::SmallVec; /// Autonomous ghost AI system implementing randomized movement with backtracking avoidance. pub fn ghost_movement_system( @@ -73,46 +68,3 @@ pub fn ghost_movement_system( } } } - -pub fn ghost_collision_system( - mut commands: Commands, - mut collision_events: EventReader, - mut score: ResMut, - pacman_query: Query<&CombatState, With>, - ghost_query: Query<(Entity, &Ghost), With>, - mut events: EventWriter, -) { - for event in collision_events.read() { - if let GameEvent::Collision(entity1, entity2) = event { - // Check if one is Pacman and the other is a ghost - let (pacman_entity, ghost_entity) = if pacman_query.get(*entity1).is_ok() && ghost_query.get(*entity2).is_ok() { - (*entity1, *entity2) - } else if pacman_query.get(*entity2).is_ok() && ghost_query.get(*entity1).is_ok() { - (*entity2, *entity1) - } else { - continue; - }; - - // Check if Pac-Man is energized - if let Ok(combat_state) = pacman_query.get(pacman_entity) { - if combat_state.is_energized() { - // Pac-Man eats the ghost - if let Ok((ghost_ent, _ghost_type)) = ghost_query.get(ghost_entity) { - // Add score (200 points per ghost eaten) - score.0 += 200; - - // Remove the ghost - commands.entity(ghost_ent).despawn(); - - // Play eat sound - events.write(AudioEvent::PlayEat); - } - } else { - // Pac-Man dies (this would need a death system) - // For now, just log it - tracing::warn!("Pac-Man collided with ghost while not energized!"); - } - } - } - } -} diff --git a/src/systems/item.rs b/src/systems/item.rs index e202c58..126142f 100644 --- a/src/systems/item.rs +++ b/src/systems/item.rs @@ -2,10 +2,7 @@ use bevy_ecs::{event::EventReader, prelude::*, query::With, system::Query}; use crate::{ events::GameEvent, - systems::{ - audio::AudioEvent, - components::{CombatState, EntityType, ItemCollider, LevelTiming, PacmanCollider, ScoreResource}, - }, + systems::{AudioEvent, CombatState, EntityType, ItemCollider, LevelTiming, PacmanCollider, ScoreResource}, }; /// Determines if a collision between two entity types should be handled by the item system. diff --git a/src/systems/mod.rs b/src/systems/mod.rs index 861014e..98abe3b 100644 --- a/src/systems/mod.rs +++ b/src/systems/mod.rs @@ -17,3 +17,17 @@ pub mod player; pub mod profiling; pub mod render; pub mod stage; + +pub use self::audio::*; +pub use self::blinking::*; +pub use self::collision::*; +pub use self::components::*; +pub use self::debug::*; +pub use self::ghost::*; +pub use self::input::*; +pub use self::item::*; +pub use self::movement::*; +pub use self::player::*; +pub use self::profiling::*; +pub use self::render::*; +pub use self::stage::*; diff --git a/src/systems/player.rs b/src/systems/player.rs index f68acdd..774239f 100644 --- a/src/systems/player.rs +++ b/src/systems/player.rs @@ -1,7 +1,7 @@ use bevy_ecs::{ - entity::Entity, + component::Component, event::{EventReader, EventWriter}, - prelude::{Commands, ResMut}, + prelude::ResMut, query::With, system::{Query, Res}, }; @@ -9,18 +9,110 @@ use bevy_ecs::{ use crate::{ error::GameError, events::{GameCommand, GameEvent}, - map::builder::Map, - map::graph::Edge, + map::{builder::Map, graph::Edge}, systems::{ - components::{ - AudioState, ControlState, DeltaTime, EntityType, Frozen, GhostCollider, GlobalState, MovementModifiers, - PlayerControlled, PlayerLifecycle, StartupSequence, - }, + components::{DeltaTime, EntityType, GlobalState, MovementModifiers, PlayerControlled}, debug::DebugState, movement::{BufferedDirection, Position, Velocity}, + AudioState, }, }; +/// Lifecycle state for the player entity. +#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)] +pub enum PlayerLifecycle { + Spawning, + Alive, + Dying, + Respawning, +} + +impl PlayerLifecycle { + /// Returns true when gameplay input and movement should be active + pub fn is_interactive(self) -> bool { + matches!(self, PlayerLifecycle::Alive) + } +} + +impl Default for PlayerLifecycle { + fn default() -> Self { + PlayerLifecycle::Spawning + } +} + +/// Whether player input should be processed. +#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)] +pub enum ControlState { + InputEnabled, + InputLocked, +} + +impl Default for ControlState { + fn default() -> Self { + Self::InputLocked + } +} + +/// Combat-related state for Pac-Man. Tick-based energizer logic. +#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)] +pub enum CombatState { + Normal, + Energized { + /// Remaining energizer duration in ticks (frames) + remaining_ticks: u32, + /// Ticks until flashing begins (counts down to 0, then flashing is active) + flash_countdown_ticks: u32, + }, +} + +impl Default for CombatState { + fn default() -> Self { + CombatState::Normal + } +} + +impl CombatState { + pub fn is_energized(&self) -> bool { + matches!(self, CombatState::Energized { .. }) + } + + pub fn is_flashing(&self) -> bool { + matches!(self, CombatState::Energized { flash_countdown_ticks, .. } if *flash_countdown_ticks == 0) + } + + pub fn deactivate_energizer(&mut self) { + *self = CombatState::Normal; + } + + /// Activate energizer using tick-based durations. + pub fn activate_energizer_ticks(&mut self, total_ticks: u32, flash_lead_ticks: u32) { + let flash_countdown_ticks = total_ticks.saturating_sub(flash_lead_ticks); + *self = CombatState::Energized { + remaining_ticks: total_ticks, + flash_countdown_ticks, + }; + } + + /// Advance one frame. When ticks reach zero, returns to Normal. + pub fn tick_frame(&mut self) { + if let CombatState::Energized { + remaining_ticks, + flash_countdown_ticks, + } = self + { + if *remaining_ticks > 0 { + *remaining_ticks -= 1; + if *flash_countdown_ticks > 0 { + *flash_countdown_ticks -= 1; + } + } + if *remaining_ticks == 0 { + *self = CombatState::Normal; + } + } + } +} + /// Processes player input commands and updates game state accordingly. /// /// Handles keyboard-driven commands like movement direction changes, debug mode diff --git a/src/systems/render.rs b/src/systems/render.rs index 9c5e4c7..11f60b4 100644 --- a/src/systems/render.rs +++ b/src/systems/render.rs @@ -1,11 +1,10 @@ use crate::constants::CANVAS_SIZE; use crate::error::{GameError, TextureError}; use crate::map::builder::Map; -use crate::systems::blinking::Blinking; -use crate::systems::components::{ - DeltaTime, DirectionalAnimated, EntityType, GhostCollider, PlayerControlled, Renderable, ScoreResource, StartupSequence, +use crate::systems::{ + Blinking, DeltaTime, DirectionalAnimated, EntityType, GhostCollider, PlayerControlled, Position, Renderable, ScoreResource, + StartupSequence, Velocity, }; -use crate::systems::movement::{Position, Velocity}; use crate::texture::sprite::SpriteAtlas; use crate::texture::text::TextTexture; use bevy_ecs::entity::Entity; diff --git a/src/systems/stage.rs b/src/systems/stage.rs index b835d34..2fcaa4f 100644 --- a/src/systems/stage.rs +++ b/src/systems/stage.rs @@ -1,9 +1,80 @@ use bevy_ecs::{ prelude::{Commands, Entity, Query, With}, + resource::Resource, system::ResMut, }; -use crate::systems::components::{Frozen, GhostCollider, PlayerControlled, StartupSequence}; +use crate::systems::{Frozen, GhostCollider, PlayerControlled}; + +#[derive(Resource, Debug, Clone, Copy)] +pub enum StartupSequence { + /// Stage 1: Text-only stage + /// - Player & ghosts are hidden + /// - READY! and PLAYER ONE text are shown + /// - Energizers do not blink + TextOnly { + /// Remaining ticks in this stage + remaining_ticks: u32, + }, + /// Stage 2: Characters visible stage + /// - PLAYER ONE text is hidden, READY! text remains + /// - Ghosts and Pac-Man are now shown + CharactersVisible { + /// Remaining ticks in this stage + remaining_ticks: u32, + }, + /// Stage 3: Game begins + /// - Final state, game is fully active + GameActive, +} + +impl StartupSequence { + /// Creates a new StartupSequence with the specified duration in ticks + pub fn new(text_only_ticks: u32, _characters_visible_ticks: u32) -> Self { + Self::TextOnly { + remaining_ticks: text_only_ticks, + } + } + + /// Returns true if the timer is still active (not in GameActive state) + pub fn is_active(&self) -> bool { + !matches!(self, StartupSequence::GameActive) + } + + /// Returns true if we're in the game active stage + pub fn is_game_active(&self) -> bool { + matches!(self, StartupSequence::GameActive) + } + + /// Ticks the timer by one frame, returning transition information if state changes + pub fn tick(&mut self) -> Option<(StartupSequence, StartupSequence)> { + match self { + StartupSequence::TextOnly { remaining_ticks } => { + if *remaining_ticks > 0 { + *remaining_ticks -= 1; + None + } else { + let from = *self; + *self = StartupSequence::CharactersVisible { + remaining_ticks: 60, // 1 second at 60 FPS + }; + Some((from, *self)) + } + } + StartupSequence::CharactersVisible { remaining_ticks } => { + if *remaining_ticks > 0 { + *remaining_ticks -= 1; + None + } else { + let from = *self; + *self = StartupSequence::GameActive; + Some((from, *self)) + } + } + StartupSequence::GameActive => None, + } + } +} /// Handles startup sequence transitions and component management pub fn startup_stage_system( diff --git a/tests/collision.rs b/tests/collision.rs index fd0faa0..3e9a617 100644 --- a/tests/collision.rs +++ b/tests/collision.rs @@ -5,9 +5,7 @@ use pacman::{ events::GameEvent, map::builder::Map, systems::{ - collision::{check_collision, collision_system}, - components::{Collider, EntityType, Ghost, GhostCollider, ItemCollider, PacmanCollider}, - movement::Position, + check_collision, collision_system, Collider, EntityType, Ghost, GhostCollider, ItemCollider, PacmanCollider, Position, }, }; diff --git a/tests/item.rs b/tests/item.rs index 6c739df..fa66481 100644 --- a/tests/item.rs +++ b/tests/item.rs @@ -4,10 +4,8 @@ use pacman::{ events::GameEvent, map::builder::Map, systems::{ - audio::AudioEvent, - components::{AudioState, EntityType, ItemCollider, PacmanCollider, ScoreResource}, - item::{is_valid_item_collision, item_system}, - movement::Position, + is_valid_item_collision, item_system, AudioEvent, AudioState, EntityType, ItemCollider, PacmanCollider, Position, + ScoreResource, }, }; diff --git a/tests/player.rs b/tests/player.rs index 2c2a29a..018b465 100644 --- a/tests/player.rs +++ b/tests/player.rs @@ -8,10 +8,8 @@ use pacman::{ graph::{Edge, TraversalFlags}, }, systems::{ - components::{AudioState, DeltaTime, EntityType, GlobalState, PlayerControlled}, - debug::DebugState, - movement::{BufferedDirection, Position, Velocity}, - player::{can_traverse, player_control_system, player_movement_system}, + can_traverse, player_control_system, player_movement_system, AudioState, BufferedDirection, DebugState, DeltaTime, + EntityType, GlobalState, PlayerControlled, Position, Velocity, }, };