mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-10 08:07:56 -06:00
chore: reorganize component definitions into relevant system files
This commit is contained in:
@@ -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,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user