mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 09:15:46 -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::events::GameEvent;
|
||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::map::direction::Direction;
|
use crate::map::direction::Direction;
|
||||||
use crate::systems;
|
|
||||||
use crate::systems::blinking::Blinking;
|
use crate::systems::blinking::Blinking;
|
||||||
|
use crate::systems::{self, ghost_collision_system};
|
||||||
|
|
||||||
use crate::systems::movement::{BufferedDirection, Position, Velocity};
|
use crate::systems::movement::{BufferedDirection, Position, Velocity};
|
||||||
use crate::systems::profiling::SystemId;
|
use crate::systems::profiling::SystemId;
|
||||||
use crate::systems::render::RenderDirty;
|
use crate::systems::render::RenderDirty;
|
||||||
use crate::systems::{
|
use crate::systems::{
|
||||||
audio::{audio_system, AudioEvent, AudioResource},
|
audio_system, blinking_system, collision_system, debug_render_system, directional_render_system, dirty_render_system,
|
||||||
blinking::blinking_system,
|
ghost_movement_system, hud_render_system, item_system, profile, ready_visibility_system, render_system, AudioEvent,
|
||||||
collision::collision_system,
|
AudioResource, AudioState, BackbufferResource, Collider, DebugFontResource, DebugState, DebugTextureResource, DeltaTime,
|
||||||
components::{
|
DirectionalAnimated, EntityType, Frozen, Ghost, GhostBundle, GhostCollider, GlobalState, ItemBundle, ItemCollider,
|
||||||
AudioState, Collider, DeltaTime, DirectionalAnimated, EntityType, Frozen, Ghost, GhostBundle, GhostCollider, GlobalState,
|
LevelTiming, MapTextureResource, PacmanCollider, PlayerBundle, PlayerControlled, PlayerStateBundle, Renderable,
|
||||||
ItemBundle, ItemCollider, LevelTiming, PacmanCollider, PlayerBundle, PlayerControlled, PlayerStateBundle, Renderable,
|
ScoreResource, StartupSequence, SystemTimings,
|
||||||
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,
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
use crate::texture::animated::AnimatedTexture;
|
use crate::texture::animated::AnimatedTexture;
|
||||||
use bevy_ecs::event::EventRegistry;
|
use bevy_ecs::event::EventRegistry;
|
||||||
@@ -249,9 +239,9 @@ impl Game {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let input_system = profile(SystemId::Input, systems::input::input_system);
|
let input_system = profile(SystemId::Input, systems::input::input_system);
|
||||||
let player_control_system = profile(SystemId::PlayerControls, systems::player::player_control_system);
|
let player_control_system = profile(SystemId::PlayerControls, systems::player_control_system);
|
||||||
let player_movement_system = profile(SystemId::PlayerMovement, systems::player::player_movement_system);
|
let player_movement_system = profile(SystemId::PlayerMovement, systems::player_movement_system);
|
||||||
let startup_stage_system = profile(SystemId::Stage, systems::stage::startup_stage_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 player_tunnel_slowdown_system = profile(SystemId::PlayerMovement, systems::player::player_tunnel_slowdown_system);
|
||||||
let ghost_movement_system = profile(SystemId::Ghost, ghost_movement_system);
|
let ghost_movement_system = profile(SystemId::Ghost, ghost_movement_system);
|
||||||
let collision_system = profile(SystemId::Collision, collision_system);
|
let collision_system = profile(SystemId::Collision, collision_system);
|
||||||
|
|||||||
@@ -6,10 +6,20 @@
|
|||||||
|
|
||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
event::{Event, EventReader, EventWriter},
|
event::{Event, EventReader, EventWriter},
|
||||||
|
resource::Resource,
|
||||||
system::{NonSendMut, ResMut},
|
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
|
/// Events for triggering audio playback
|
||||||
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Event, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
|||||||
@@ -1,13 +1,37 @@
|
|||||||
|
use bevy_ecs::component::Component;
|
||||||
use bevy_ecs::entity::Entity;
|
use bevy_ecs::entity::Entity;
|
||||||
use bevy_ecs::event::EventWriter;
|
use bevy_ecs::event::{EventReader, EventWriter};
|
||||||
use bevy_ecs::query::With;
|
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::error::GameError;
|
||||||
use crate::events::GameEvent;
|
use crate::events::GameEvent;
|
||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::systems::components::{Collider, GhostCollider, ItemCollider, PacmanCollider};
|
|
||||||
use crate::systems::movement::Position;
|
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.
|
/// Helper function to check collision between two entities with colliders.
|
||||||
pub fn check_collision(
|
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::{
|
use crate::{
|
||||||
map::graph::TraversalFlags,
|
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},
|
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)]
|
#[derive(Resource)]
|
||||||
pub struct GlobalState {
|
pub struct GlobalState {
|
||||||
pub exit: bool,
|
pub exit: bool,
|
||||||
@@ -177,110 +123,6 @@ pub struct ScoreResource(pub u32);
|
|||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
pub struct DeltaTime(pub f32);
|
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.
|
/// Movement modifiers that can affect Pac-Man's speed or handling.
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
pub struct MovementModifiers {
|
pub struct MovementModifiers {
|
||||||
@@ -332,6 +174,19 @@ impl LevelTiming {
|
|||||||
#[derive(Component, Debug, Clone, Copy)]
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
pub struct Frozen;
|
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
|
/// Convenience bundle for attaching the hybrid FSM to the player entity
|
||||||
#[derive(Bundle, Default)]
|
#[derive(Bundle, Default)]
|
||||||
pub struct PlayerStateBundle {
|
pub struct PlayerStateBundle {
|
||||||
@@ -341,72 +196,23 @@ pub struct PlayerStateBundle {
|
|||||||
pub movement_modifiers: MovementModifiers,
|
pub movement_modifiers: MovementModifiers,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Resource, Debug, Clone, Copy)]
|
#[derive(Bundle)]
|
||||||
pub enum StartupSequence {
|
pub struct ItemBundle {
|
||||||
/// Stage 1: Text-only stage
|
pub position: Position,
|
||||||
/// - Player & ghosts are hidden
|
pub sprite: Renderable,
|
||||||
/// - READY! and PLAYER ONE text are shown
|
pub entity_type: EntityType,
|
||||||
/// - Energizers do not blink
|
pub collider: Collider,
|
||||||
TextOnly {
|
pub item_collider: ItemCollider,
|
||||||
/// 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 {
|
#[derive(Bundle)]
|
||||||
/// Creates a new StartupSequence with the specified duration in ticks
|
pub struct GhostBundle {
|
||||||
pub fn new(text_only_ticks: u32, _characters_visible_ticks: u32) -> Self {
|
pub ghost: Ghost,
|
||||||
Self::TextOnly {
|
pub position: Position,
|
||||||
remaining_ticks: text_only_ticks,
|
pub velocity: Velocity,
|
||||||
}
|
pub sprite: Renderable,
|
||||||
}
|
pub directional_animated: DirectionalAnimated,
|
||||||
|
pub entity_type: EntityType,
|
||||||
/// Returns true if the timer is still active (not in GameActive state)
|
pub collider: Collider,
|
||||||
pub fn is_active(&self) -> bool {
|
pub ghost_collider: GhostCollider,
|
||||||
!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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,7 @@ use std::cmp::Ordering;
|
|||||||
|
|
||||||
use crate::constants::BOARD_PIXEL_OFFSET;
|
use crate::constants::BOARD_PIXEL_OFFSET;
|
||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::systems::components::Collider;
|
use crate::systems::{BackbufferResource, Collider, CursorPosition, Position, SystemTimings};
|
||||||
use crate::systems::input::CursorPosition;
|
|
||||||
use crate::systems::movement::Position;
|
|
||||||
use crate::systems::profiling::SystemTimings;
|
|
||||||
use crate::systems::render::BackbufferResource;
|
|
||||||
use bevy_ecs::prelude::*;
|
use bevy_ecs::prelude::*;
|
||||||
use glam::{IVec2, UVec2, Vec2};
|
use glam::{IVec2, UVec2, Vec2};
|
||||||
use sdl2::pixels::Color;
|
use sdl2::pixels::Color;
|
||||||
|
|||||||
@@ -1,15 +1,4 @@
|
|||||||
use bevy_ecs::entity::Entity;
|
use crate::systems::components::Frozen;
|
||||||
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::{
|
use crate::{
|
||||||
map::{
|
map::{
|
||||||
builder::Map,
|
builder::Map,
|
||||||
@@ -17,10 +6,16 @@ use crate::{
|
|||||||
graph::{Edge, TraversalFlags},
|
graph::{Edge, TraversalFlags},
|
||||||
},
|
},
|
||||||
systems::{
|
systems::{
|
||||||
components::{CombatState, DeltaTime, Ghost, PlayerControlled},
|
components::{DeltaTime, Ghost},
|
||||||
movement::{Position, Velocity},
|
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.
|
/// Autonomous ghost AI system implementing randomized movement with backtracking avoidance.
|
||||||
pub fn ghost_movement_system(
|
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::{
|
use crate::{
|
||||||
events::GameEvent,
|
events::GameEvent,
|
||||||
systems::{
|
systems::{AudioEvent, CombatState, EntityType, ItemCollider, LevelTiming, PacmanCollider, ScoreResource},
|
||||||
audio::AudioEvent,
|
|
||||||
components::{CombatState, EntityType, ItemCollider, LevelTiming, PacmanCollider, ScoreResource},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Determines if a collision between two entity types should be handled by the item system.
|
/// 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 profiling;
|
||||||
pub mod render;
|
pub mod render;
|
||||||
pub mod stage;
|
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::{
|
use bevy_ecs::{
|
||||||
entity::Entity,
|
component::Component,
|
||||||
event::{EventReader, EventWriter},
|
event::{EventReader, EventWriter},
|
||||||
prelude::{Commands, ResMut},
|
prelude::ResMut,
|
||||||
query::With,
|
query::With,
|
||||||
system::{Query, Res},
|
system::{Query, Res},
|
||||||
};
|
};
|
||||||
@@ -9,18 +9,110 @@ use bevy_ecs::{
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::GameError,
|
error::GameError,
|
||||||
events::{GameCommand, GameEvent},
|
events::{GameCommand, GameEvent},
|
||||||
map::builder::Map,
|
map::{builder::Map, graph::Edge},
|
||||||
map::graph::Edge,
|
|
||||||
systems::{
|
systems::{
|
||||||
components::{
|
components::{DeltaTime, EntityType, GlobalState, MovementModifiers, PlayerControlled},
|
||||||
AudioState, ControlState, DeltaTime, EntityType, Frozen, GhostCollider, GlobalState, MovementModifiers,
|
|
||||||
PlayerControlled, PlayerLifecycle, StartupSequence,
|
|
||||||
},
|
|
||||||
debug::DebugState,
|
debug::DebugState,
|
||||||
movement::{BufferedDirection, Position, Velocity},
|
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.
|
/// Processes player input commands and updates game state accordingly.
|
||||||
///
|
///
|
||||||
/// Handles keyboard-driven commands like movement direction changes, debug mode
|
/// Handles keyboard-driven commands like movement direction changes, debug mode
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
use crate::constants::CANVAS_SIZE;
|
use crate::constants::CANVAS_SIZE;
|
||||||
use crate::error::{GameError, TextureError};
|
use crate::error::{GameError, TextureError};
|
||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::systems::blinking::Blinking;
|
use crate::systems::{
|
||||||
use crate::systems::components::{
|
Blinking, DeltaTime, DirectionalAnimated, EntityType, GhostCollider, PlayerControlled, Position, Renderable, ScoreResource,
|
||||||
DeltaTime, DirectionalAnimated, EntityType, GhostCollider, PlayerControlled, Renderable, ScoreResource, StartupSequence,
|
StartupSequence, Velocity,
|
||||||
};
|
};
|
||||||
use crate::systems::movement::{Position, Velocity};
|
|
||||||
use crate::texture::sprite::SpriteAtlas;
|
use crate::texture::sprite::SpriteAtlas;
|
||||||
use crate::texture::text::TextTexture;
|
use crate::texture::text::TextTexture;
|
||||||
use bevy_ecs::entity::Entity;
|
use bevy_ecs::entity::Entity;
|
||||||
|
|||||||
@@ -1,9 +1,80 @@
|
|||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
prelude::{Commands, Entity, Query, With},
|
prelude::{Commands, Entity, Query, With},
|
||||||
|
resource::Resource,
|
||||||
system::ResMut,
|
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
|
/// Handles startup sequence transitions and component management
|
||||||
pub fn startup_stage_system(
|
pub fn startup_stage_system(
|
||||||
|
|||||||
@@ -5,9 +5,7 @@ use pacman::{
|
|||||||
events::GameEvent,
|
events::GameEvent,
|
||||||
map::builder::Map,
|
map::builder::Map,
|
||||||
systems::{
|
systems::{
|
||||||
collision::{check_collision, collision_system},
|
check_collision, collision_system, Collider, EntityType, Ghost, GhostCollider, ItemCollider, PacmanCollider, Position,
|
||||||
components::{Collider, EntityType, Ghost, GhostCollider, ItemCollider, PacmanCollider},
|
|
||||||
movement::Position,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,8 @@ use pacman::{
|
|||||||
events::GameEvent,
|
events::GameEvent,
|
||||||
map::builder::Map,
|
map::builder::Map,
|
||||||
systems::{
|
systems::{
|
||||||
audio::AudioEvent,
|
is_valid_item_collision, item_system, AudioEvent, AudioState, EntityType, ItemCollider, PacmanCollider, Position,
|
||||||
components::{AudioState, EntityType, ItemCollider, PacmanCollider, ScoreResource},
|
ScoreResource,
|
||||||
item::{is_valid_item_collision, item_system},
|
|
||||||
movement::Position,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,10 +8,8 @@ use pacman::{
|
|||||||
graph::{Edge, TraversalFlags},
|
graph::{Edge, TraversalFlags},
|
||||||
},
|
},
|
||||||
systems::{
|
systems::{
|
||||||
components::{AudioState, DeltaTime, EntityType, GlobalState, PlayerControlled},
|
can_traverse, player_control_system, player_movement_system, AudioState, BufferedDirection, DebugState, DeltaTime,
|
||||||
debug::DebugState,
|
EntityType, GlobalState, PlayerControlled, Position, Velocity,
|
||||||
movement::{BufferedDirection, Position, Velocity},
|
|
||||||
player::{can_traverse, player_control_system, player_movement_system},
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user