mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 07:15:41 -06:00
chore: reorganize component definitions into relevant system files
This commit is contained in:
30
src/game.rs
30
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);
|
||||
|
||||
@@ -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)]
|
||||
|
||||
@@ -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<GameEvent>,
|
||||
mut score: ResMut<ScoreResource>,
|
||||
pacman_query: Query<&CombatState, With<PlayerControlled>>,
|
||||
ghost_query: Query<(Entity, &Ghost), With<GhostCollider>>,
|
||||
mut events: EventWriter<AudioEvent>,
|
||||
) {
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<GameEvent>,
|
||||
mut score: ResMut<ScoreResource>,
|
||||
pacman_query: Query<&CombatState, With<PlayerControlled>>,
|
||||
ghost_query: Query<(Entity, &Ghost), With<GhostCollider>>,
|
||||
mut events: EventWriter<AudioEvent>,
|
||||
) {
|
||||
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!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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::*;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -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,
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user