mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-10 02:07:55 -06:00
refactor: store common components & bundles in 'common' submodule, move others directly into relevant files, create 'animation' submodule
This commit is contained in:
@@ -42,8 +42,7 @@ use crate::{
|
|||||||
asset::{get_asset_bytes, Asset},
|
asset::{get_asset_bytes, Asset},
|
||||||
events::GameCommand,
|
events::GameCommand,
|
||||||
map::render::MapRenderer,
|
map::render::MapRenderer,
|
||||||
systems::debug::{BatchedLinesResource, TtfAtlasResource},
|
systems::{BatchedLinesResource, Bindings, CursorPosition, TtfAtlasResource},
|
||||||
systems::input::{Bindings, CursorPosition},
|
|
||||||
texture::sprite::{AtlasMapper, SpriteAtlas},
|
texture::sprite::{AtlasMapper, SpriteAtlas},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::constants::{MapTile, BOARD_CELL_SIZE, CELL_SIZE};
|
|||||||
use crate::map::direction::Direction;
|
use crate::map::direction::Direction;
|
||||||
use crate::map::graph::{Graph, Node, TraversalFlags};
|
use crate::map::graph::{Graph, Node, TraversalFlags};
|
||||||
use crate::map::parser::MapTileParser;
|
use crate::map::parser::MapTileParser;
|
||||||
use crate::systems::movement::NodeId;
|
use crate::systems::NodeId;
|
||||||
use bevy_ecs::resource::Resource;
|
use bevy_ecs::resource::Resource;
|
||||||
use glam::{I8Vec2, IVec2, Vec2};
|
use glam::{I8Vec2, IVec2, Vec2};
|
||||||
use std::collections::{HashMap, VecDeque};
|
use std::collections::{HashMap, VecDeque};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use glam::Vec2;
|
use glam::Vec2;
|
||||||
|
|
||||||
use crate::systems::movement::NodeId;
|
use crate::systems::NodeId;
|
||||||
|
|
||||||
use super::direction::Direction;
|
use super::direction::Direction;
|
||||||
|
|
||||||
|
|||||||
132
src/systems/animation.rs
Normal file
132
src/systems/animation.rs
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
use bevy_ecs::{
|
||||||
|
component::Component,
|
||||||
|
query::{Has, Or, With, Without},
|
||||||
|
resource::Resource,
|
||||||
|
system::{Query, Res},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
systems::{DeltaTime, Dying, Frozen, Position, Renderable, Velocity},
|
||||||
|
texture::animated::{DirectionalTiles, TileSequence},
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Directional animation component with shared timing across all directions
|
||||||
|
#[derive(Component, Clone)]
|
||||||
|
pub struct DirectionalAnimation {
|
||||||
|
pub moving_tiles: DirectionalTiles,
|
||||||
|
pub stopped_tiles: DirectionalTiles,
|
||||||
|
pub current_frame: usize,
|
||||||
|
pub time_bank: u16,
|
||||||
|
pub frame_duration: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DirectionalAnimation {
|
||||||
|
/// Creates a new directional animation with the given tiles and frame duration
|
||||||
|
pub fn new(moving_tiles: DirectionalTiles, stopped_tiles: DirectionalTiles, frame_duration: u16) -> Self {
|
||||||
|
Self {
|
||||||
|
moving_tiles,
|
||||||
|
stopped_tiles,
|
||||||
|
current_frame: 0,
|
||||||
|
time_bank: 0,
|
||||||
|
frame_duration,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tag component to mark animations that should loop when they reach the end
|
||||||
|
#[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
|
pub struct Looping;
|
||||||
|
|
||||||
|
/// Linear animation component for non-directional animations (frightened ghosts)
|
||||||
|
#[derive(Component, Resource, Clone)]
|
||||||
|
pub struct LinearAnimation {
|
||||||
|
pub tiles: TileSequence,
|
||||||
|
pub current_frame: usize,
|
||||||
|
pub time_bank: u16,
|
||||||
|
pub frame_duration: u16,
|
||||||
|
pub finished: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LinearAnimation {
|
||||||
|
/// Creates a new linear animation with the given tiles and frame duration
|
||||||
|
pub fn new(tiles: TileSequence, frame_duration: u16) -> Self {
|
||||||
|
Self {
|
||||||
|
tiles,
|
||||||
|
current_frame: 0,
|
||||||
|
time_bank: 0,
|
||||||
|
frame_duration,
|
||||||
|
finished: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates directional animated entities with synchronized timing across directions.
|
||||||
|
///
|
||||||
|
/// This runs before the render system to update sprites based on current direction and movement state.
|
||||||
|
/// All directions share the same frame timing to ensure perfect synchronization.
|
||||||
|
pub fn directional_render_system(
|
||||||
|
dt: Res<DeltaTime>,
|
||||||
|
mut query: Query<(&Position, &Velocity, &mut DirectionalAnimation, &mut Renderable), Without<Frozen>>,
|
||||||
|
) {
|
||||||
|
let ticks = (dt.seconds * 60.0).round() as u16; // Convert from seconds to ticks at 60 ticks/sec
|
||||||
|
|
||||||
|
for (position, velocity, mut anim, mut renderable) in query.iter_mut() {
|
||||||
|
let stopped = matches!(position, Position::Stopped { .. });
|
||||||
|
|
||||||
|
// Only tick animation when moving to preserve stopped frame
|
||||||
|
if !stopped {
|
||||||
|
// Tick shared animation state
|
||||||
|
anim.time_bank += ticks;
|
||||||
|
while anim.time_bank >= anim.frame_duration {
|
||||||
|
anim.time_bank -= anim.frame_duration;
|
||||||
|
anim.current_frame += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get tiles for current direction and movement state
|
||||||
|
let tiles = if stopped {
|
||||||
|
anim.stopped_tiles.get(velocity.direction)
|
||||||
|
} else {
|
||||||
|
anim.moving_tiles.get(velocity.direction)
|
||||||
|
};
|
||||||
|
|
||||||
|
if !tiles.is_empty() {
|
||||||
|
let new_tile = tiles.get_tile(anim.current_frame);
|
||||||
|
if renderable.sprite != new_tile {
|
||||||
|
renderable.sprite = new_tile;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// System that updates `Renderable` sprites for entities with `LinearAnimation`.
|
||||||
|
#[allow(clippy::type_complexity)]
|
||||||
|
pub fn linear_render_system(
|
||||||
|
dt: Res<DeltaTime>,
|
||||||
|
mut query: Query<(&mut LinearAnimation, &mut Renderable, Has<Looping>), Or<(Without<Frozen>, With<Dying>)>>,
|
||||||
|
) {
|
||||||
|
for (mut anim, mut renderable, looping) in query.iter_mut() {
|
||||||
|
if anim.finished {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
anim.time_bank += dt.ticks as u16;
|
||||||
|
let frames_to_advance = (anim.time_bank / anim.frame_duration) as usize;
|
||||||
|
|
||||||
|
if frames_to_advance == 0 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let total_frames = anim.tiles.len();
|
||||||
|
|
||||||
|
if !looping && anim.current_frame + frames_to_advance >= total_frames {
|
||||||
|
anim.finished = true;
|
||||||
|
anim.current_frame = total_frames - 1;
|
||||||
|
} else {
|
||||||
|
anim.current_frame += frames_to_advance;
|
||||||
|
}
|
||||||
|
|
||||||
|
anim.time_bank %= anim.frame_duration;
|
||||||
|
renderable.sprite = anim.tiles.get_tile(anim.current_frame);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,10 +5,7 @@ use bevy_ecs::{
|
|||||||
system::{Commands, Query, Res},
|
system::{Commands, Query, Res},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::systems::{
|
use crate::systems::{DeltaTime, Frozen, Hidden, Renderable};
|
||||||
components::{DeltaTime, Renderable},
|
|
||||||
Frozen, Hidden,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct Blinking {
|
pub struct Blinking {
|
||||||
|
|||||||
@@ -7,13 +7,10 @@ use bevy_ecs::{
|
|||||||
};
|
};
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
use crate::error::GameError;
|
|
||||||
use crate::events::{GameEvent, StageTransition};
|
use crate::events::{GameEvent, StageTransition};
|
||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::systems::{
|
use crate::systems::{movement::Position, AudioEvent, DyingSequence, Frozen, GameStage, Ghost, PlayerControlled, ScoreResource};
|
||||||
components::GhostState, movement::Position, AudioEvent, DyingSequence, Frozen, GameStage, Ghost, PlayerControlled,
|
use crate::{error::GameError, systems::GhostState};
|
||||||
ScoreResource,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// A component for defining the collision area of an entity.
|
/// A component for defining the collision area of an entity.
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
|
|||||||
43
src/systems/common/bundles.rs
Normal file
43
src/systems/common/bundles.rs
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
use bevy_ecs::bundle::Bundle;
|
||||||
|
|
||||||
|
use crate::systems::{
|
||||||
|
BufferedDirection, Collider, DirectionalAnimation, EntityType, Ghost, GhostCollider, GhostState, ItemCollider,
|
||||||
|
LastAnimationState, MovementModifiers, PacmanCollider, PlayerControlled, Position, Renderable, Velocity,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Bundle)]
|
||||||
|
pub struct PlayerBundle {
|
||||||
|
pub player: PlayerControlled,
|
||||||
|
pub position: Position,
|
||||||
|
pub velocity: Velocity,
|
||||||
|
pub buffered_direction: BufferedDirection,
|
||||||
|
pub sprite: Renderable,
|
||||||
|
pub directional_animation: DirectionalAnimation,
|
||||||
|
pub entity_type: EntityType,
|
||||||
|
pub collider: Collider,
|
||||||
|
pub movement_modifiers: MovementModifiers,
|
||||||
|
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_animation: DirectionalAnimation,
|
||||||
|
pub entity_type: EntityType,
|
||||||
|
pub collider: Collider,
|
||||||
|
pub ghost_collider: GhostCollider,
|
||||||
|
pub ghost_state: GhostState,
|
||||||
|
pub last_animation_state: LastAnimationState,
|
||||||
|
}
|
||||||
103
src/systems/common/components.rs
Normal file
103
src/systems/common/components.rs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
use bevy_ecs::{component::Component, resource::Resource};
|
||||||
|
|
||||||
|
use crate::map::graph::TraversalFlags;
|
||||||
|
|
||||||
|
/// A tag component denoting the type of entity.
|
||||||
|
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum EntityType {
|
||||||
|
Player,
|
||||||
|
Ghost,
|
||||||
|
Pellet,
|
||||||
|
PowerPellet,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EntityType {
|
||||||
|
/// Returns the traversal flags for this entity type.
|
||||||
|
pub fn traversal_flags(&self) -> TraversalFlags {
|
||||||
|
match self {
|
||||||
|
EntityType::Player => TraversalFlags::PACMAN,
|
||||||
|
EntityType::Ghost => TraversalFlags::GHOST,
|
||||||
|
_ => TraversalFlags::empty(), // Static entities don't traverse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn score_value(&self) -> Option<u32> {
|
||||||
|
match self {
|
||||||
|
EntityType::Pellet => Some(10),
|
||||||
|
EntityType::PowerPellet => Some(50),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_collectible(&self) -> bool {
|
||||||
|
matches!(self, EntityType::Pellet | EntityType::PowerPellet)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct GlobalState {
|
||||||
|
pub exit: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct ScoreResource(pub u32);
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct DeltaTime {
|
||||||
|
/// Floating-point delta time in seconds
|
||||||
|
pub seconds: f32,
|
||||||
|
/// Integer tick delta (usually 1, but can be different for testing)
|
||||||
|
pub ticks: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
impl DeltaTime {
|
||||||
|
/// Creates a new DeltaTime from a floating-point delta time in seconds
|
||||||
|
///
|
||||||
|
/// While this method exists as a helper, it does not mean that seconds and ticks are interchangeable.
|
||||||
|
pub fn from_seconds(seconds: f32) -> Self {
|
||||||
|
Self {
|
||||||
|
seconds,
|
||||||
|
ticks: (seconds * 60.0).round() as u32,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a new DeltaTime from an integer tick delta
|
||||||
|
///
|
||||||
|
/// While this method exists as a helper, it does not mean that seconds and ticks are interchangeable.
|
||||||
|
pub fn from_ticks(ticks: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
seconds: ticks as f32 / 60.0,
|
||||||
|
ticks,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Movement modifiers that can affect Pac-Man's speed or handling.
|
||||||
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
pub struct MovementModifiers {
|
||||||
|
/// Multiplier applied to base speed (e.g., tunnels)
|
||||||
|
pub speed_multiplier: f32,
|
||||||
|
/// True when currently in a tunnel slowdown region
|
||||||
|
pub tunnel_slowdown_active: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for MovementModifiers {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
speed_multiplier: 1.0,
|
||||||
|
tunnel_slowdown_active: false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Tag component for entities that should be frozen during startup
|
||||||
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
pub struct Frozen;
|
||||||
|
|
||||||
|
/// Component for HUD life sprite entities.
|
||||||
|
/// Each life sprite entity has an index indicating its position from left to right (0, 1, 2, etc.).
|
||||||
|
/// This mostly functions as a tag component for sprites.
|
||||||
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
pub struct PlayerLife {
|
||||||
|
pub index: u32,
|
||||||
|
}
|
||||||
5
src/systems/common/mod.rs
Normal file
5
src/systems/common/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
pub mod bundles;
|
||||||
|
pub mod components;
|
||||||
|
|
||||||
|
pub use self::bundles::*;
|
||||||
|
pub use self::components::*;
|
||||||
@@ -1,411 +0,0 @@
|
|||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use bevy_ecs::{bundle::Bundle, component::Component, resource::Resource};
|
|
||||||
use bitflags::bitflags;
|
|
||||||
|
|
||||||
use crate::{
|
|
||||||
map::graph::TraversalFlags,
|
|
||||||
systems::{
|
|
||||||
movement::{BufferedDirection, Position, Velocity},
|
|
||||||
Collider, GhostCollider, ItemCollider, PacmanCollider,
|
|
||||||
},
|
|
||||||
texture::{
|
|
||||||
animated::{DirectionalTiles, TileSequence},
|
|
||||||
sprite::AtlasTile,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
/// A tag component for entities that are controlled by the player.
|
|
||||||
#[derive(Default, Component)]
|
|
||||||
pub struct PlayerControlled;
|
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub enum Ghost {
|
|
||||||
Blinky,
|
|
||||||
Pinky,
|
|
||||||
Inky,
|
|
||||||
Clyde,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ghost {
|
|
||||||
/// Returns the ghost type name for atlas lookups.
|
|
||||||
pub fn as_str(self) -> &'static str {
|
|
||||||
match self {
|
|
||||||
Ghost::Blinky => "blinky",
|
|
||||||
Ghost::Pinky => "pinky",
|
|
||||||
Ghost::Inky => "inky",
|
|
||||||
Ghost::Clyde => "clyde",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the base movement speed for this ghost type.
|
|
||||||
pub fn base_speed(self) -> f32 {
|
|
||||||
match self {
|
|
||||||
Ghost::Blinky => 1.0,
|
|
||||||
Ghost::Pinky => 0.95,
|
|
||||||
Ghost::Inky => 0.9,
|
|
||||||
Ghost::Clyde => 0.85,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the ghost's color for debug rendering.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
pub fn debug_color(&self) -> sdl2::pixels::Color {
|
|
||||||
match self {
|
|
||||||
Ghost::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
|
|
||||||
Ghost::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
|
|
||||||
Ghost::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
|
|
||||||
Ghost::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A tag component denoting the type of entity.
|
|
||||||
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub enum EntityType {
|
|
||||||
Player,
|
|
||||||
Ghost,
|
|
||||||
Pellet,
|
|
||||||
PowerPellet,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EntityType {
|
|
||||||
/// Returns the traversal flags for this entity type.
|
|
||||||
pub fn traversal_flags(&self) -> TraversalFlags {
|
|
||||||
match self {
|
|
||||||
EntityType::Player => TraversalFlags::PACMAN,
|
|
||||||
EntityType::Ghost => TraversalFlags::GHOST,
|
|
||||||
_ => TraversalFlags::empty(), // Static entities don't traverse
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn score_value(&self) -> Option<u32> {
|
|
||||||
match self {
|
|
||||||
EntityType::Pellet => Some(10),
|
|
||||||
EntityType::PowerPellet => Some(50),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn is_collectible(&self) -> bool {
|
|
||||||
matches!(self, EntityType::Pellet | EntityType::PowerPellet)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A component for entities that have a sprite, with a layer for ordering.
|
|
||||||
///
|
|
||||||
/// This is intended to be modified by other entities allowing animation.
|
|
||||||
#[derive(Component)]
|
|
||||||
pub struct Renderable {
|
|
||||||
pub sprite: AtlasTile,
|
|
||||||
pub layer: u8,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Directional animation component with shared timing across all directions
|
|
||||||
#[derive(Component, Clone)]
|
|
||||||
pub struct DirectionalAnimation {
|
|
||||||
pub moving_tiles: DirectionalTiles,
|
|
||||||
pub stopped_tiles: DirectionalTiles,
|
|
||||||
pub current_frame: usize,
|
|
||||||
pub time_bank: u16,
|
|
||||||
pub frame_duration: u16,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DirectionalAnimation {
|
|
||||||
/// Creates a new directional animation with the given tiles and frame duration
|
|
||||||
pub fn new(moving_tiles: DirectionalTiles, stopped_tiles: DirectionalTiles, frame_duration: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
moving_tiles,
|
|
||||||
stopped_tiles,
|
|
||||||
current_frame: 0,
|
|
||||||
time_bank: 0,
|
|
||||||
frame_duration,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tag component to mark animations that should loop when they reach the end
|
|
||||||
#[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
|
|
||||||
pub struct Looping;
|
|
||||||
|
|
||||||
/// Linear animation component for non-directional animations (frightened ghosts)
|
|
||||||
#[derive(Component, Resource, Clone)]
|
|
||||||
pub struct LinearAnimation {
|
|
||||||
pub tiles: TileSequence,
|
|
||||||
pub current_frame: usize,
|
|
||||||
pub time_bank: u16,
|
|
||||||
pub frame_duration: u16,
|
|
||||||
pub finished: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl LinearAnimation {
|
|
||||||
/// Creates a new linear animation with the given tiles and frame duration
|
|
||||||
pub fn new(tiles: TileSequence, frame_duration: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
tiles,
|
|
||||||
current_frame: 0,
|
|
||||||
time_bank: 0,
|
|
||||||
frame_duration,
|
|
||||||
finished: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bitflags! {
|
|
||||||
#[derive(Component, Default, Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
||||||
pub struct CollisionLayer: u8 {
|
|
||||||
const PACMAN = 1 << 0;
|
|
||||||
const GHOST = 1 << 1;
|
|
||||||
const ITEM = 1 << 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct GlobalState {
|
|
||||||
pub exit: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct ScoreResource(pub u32);
|
|
||||||
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct DeltaTime {
|
|
||||||
/// Floating-point delta time in seconds
|
|
||||||
pub seconds: f32,
|
|
||||||
/// Integer tick delta (usually 1, but can be different for testing)
|
|
||||||
pub ticks: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
impl DeltaTime {
|
|
||||||
/// Creates a new DeltaTime from a floating-point delta time in seconds
|
|
||||||
///
|
|
||||||
/// While this method exists as a helper, it does not mean that seconds and ticks are interchangeable.
|
|
||||||
pub fn from_seconds(seconds: f32) -> Self {
|
|
||||||
Self {
|
|
||||||
seconds,
|
|
||||||
ticks: (seconds * 60.0).round() as u32,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Creates a new DeltaTime from an integer tick delta
|
|
||||||
///
|
|
||||||
/// While this method exists as a helper, it does not mean that seconds and ticks are interchangeable.
|
|
||||||
pub fn from_ticks(ticks: u32) -> Self {
|
|
||||||
Self {
|
|
||||||
seconds: ticks as f32 / 60.0,
|
|
||||||
ticks,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Movement modifiers that can affect Pac-Man's speed or handling.
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
|
||||||
pub struct MovementModifiers {
|
|
||||||
/// Multiplier applied to base speed (e.g., tunnels)
|
|
||||||
pub speed_multiplier: f32,
|
|
||||||
/// True when currently in a tunnel slowdown region
|
|
||||||
pub tunnel_slowdown_active: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for MovementModifiers {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
speed_multiplier: 1.0,
|
|
||||||
tunnel_slowdown_active: false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tag component for entities that should be frozen during startup
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
|
||||||
pub struct Frozen;
|
|
||||||
|
|
||||||
/// Tag component for eaten ghosts
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
|
||||||
pub struct Eaten;
|
|
||||||
|
|
||||||
/// Tag component for Pac-Man during his death animation.
|
|
||||||
/// This is mainly because the Frozen tag would stop both movement and animation, while the Dying tag can signal that the animation should continue despite being frozen.
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
|
||||||
pub struct Dying;
|
|
||||||
|
|
||||||
/// Component for HUD life sprite entities.
|
|
||||||
/// Each life sprite entity has an index indicating its position from left to right (0, 1, 2, etc.).
|
|
||||||
/// This mostly functions as a tag component for sprites.
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
|
||||||
pub struct PlayerLife {
|
|
||||||
pub index: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
|
||||||
pub enum GhostState {
|
|
||||||
/// Normal ghost behavior - chasing Pac-Man
|
|
||||||
Normal,
|
|
||||||
/// Frightened state after power pellet - ghost can be eaten
|
|
||||||
Frightened {
|
|
||||||
remaining_ticks: u32,
|
|
||||||
flash: bool,
|
|
||||||
remaining_flash_ticks: u32,
|
|
||||||
},
|
|
||||||
/// Eyes state - ghost has been eaten and is returning to ghost house
|
|
||||||
Eyes,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Component to track the last animation state for efficient change detection
|
|
||||||
#[derive(Component, Debug, Clone, Copy, PartialEq)]
|
|
||||||
pub struct LastAnimationState(pub GhostAnimation);
|
|
||||||
|
|
||||||
impl GhostState {
|
|
||||||
/// Creates a new frightened state with the specified duration
|
|
||||||
pub fn new_frightened(total_ticks: u32, flash_start_ticks: u32) -> Self {
|
|
||||||
Self::Frightened {
|
|
||||||
remaining_ticks: total_ticks,
|
|
||||||
flash: false,
|
|
||||||
remaining_flash_ticks: flash_start_ticks, // Time until flashing starts
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Ticks the ghost state, returning true if the state changed.
|
|
||||||
pub fn tick(&mut self) -> bool {
|
|
||||||
if let GhostState::Frightened {
|
|
||||||
remaining_ticks,
|
|
||||||
flash,
|
|
||||||
remaining_flash_ticks,
|
|
||||||
} = self
|
|
||||||
{
|
|
||||||
// Transition out of frightened state
|
|
||||||
if *remaining_ticks == 0 {
|
|
||||||
*self = GhostState::Normal;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
*remaining_ticks -= 1;
|
|
||||||
|
|
||||||
if *remaining_flash_ticks > 0 {
|
|
||||||
*remaining_flash_ticks = remaining_flash_ticks.saturating_sub(1);
|
|
||||||
if *remaining_flash_ticks == 0 {
|
|
||||||
*flash = true;
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the appropriate animation state for this ghost state
|
|
||||||
pub fn animation_state(&self) -> GhostAnimation {
|
|
||||||
match self {
|
|
||||||
GhostState::Normal => GhostAnimation::Normal,
|
|
||||||
GhostState::Eyes => GhostAnimation::Eyes,
|
|
||||||
GhostState::Frightened { flash: false, .. } => GhostAnimation::Frightened { flash: false },
|
|
||||||
GhostState::Frightened { flash: true, .. } => GhostAnimation::Frightened { flash: true },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Enumeration of different ghost animation states.
|
|
||||||
/// Note that this is used in micromap which has a fixed size based on the number of variants,
|
|
||||||
/// so extending this should be done with caution, and will require updating the micromap's capacity.
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub enum GhostAnimation {
|
|
||||||
/// Normal ghost appearance with directional movement animations
|
|
||||||
Normal,
|
|
||||||
/// Blue ghost appearance when vulnerable (power pellet active)
|
|
||||||
Frightened { flash: bool },
|
|
||||||
/// Eyes-only animation when ghost has been consumed by Pac-Man (Eaten state)
|
|
||||||
Eyes,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Global resource containing pre-loaded animation sets for all ghost types.
|
|
||||||
///
|
|
||||||
/// This resource is initialized once during game startup and provides O(1) access
|
|
||||||
/// to animation sets for each ghost type. The animation system uses this resource
|
|
||||||
/// to efficiently switch between different ghost states without runtime asset loading.
|
|
||||||
///
|
|
||||||
/// The HashMap is keyed by `Ghost` enum variants (Blinky, Pinky, Inky, Clyde) and
|
|
||||||
/// contains the normal directional animation for each ghost type.
|
|
||||||
#[derive(Resource)]
|
|
||||||
pub struct GhostAnimations {
|
|
||||||
pub normal: HashMap<Ghost, DirectionalAnimation>,
|
|
||||||
pub eyes: DirectionalAnimation,
|
|
||||||
pub frightened: LinearAnimation,
|
|
||||||
pub frightened_flashing: LinearAnimation,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GhostAnimations {
|
|
||||||
/// Creates a new GhostAnimations resource with the provided data.
|
|
||||||
pub fn new(
|
|
||||||
normal: HashMap<Ghost, DirectionalAnimation>,
|
|
||||||
eyes: DirectionalAnimation,
|
|
||||||
frightened: LinearAnimation,
|
|
||||||
frightened_flashing: LinearAnimation,
|
|
||||||
) -> Self {
|
|
||||||
Self {
|
|
||||||
normal,
|
|
||||||
eyes,
|
|
||||||
frightened,
|
|
||||||
frightened_flashing,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the normal directional animation for the specified ghost type.
|
|
||||||
pub fn get_normal(&self, ghost_type: &Ghost) -> Option<&DirectionalAnimation> {
|
|
||||||
self.normal.get(ghost_type)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the eyes animation (shared across all ghosts).
|
|
||||||
pub fn eyes(&self) -> &DirectionalAnimation {
|
|
||||||
&self.eyes
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the frightened animations (shared across all ghosts).
|
|
||||||
pub fn frightened(&self, flash: bool) -> &LinearAnimation {
|
|
||||||
if flash {
|
|
||||||
&self.frightened_flashing
|
|
||||||
} else {
|
|
||||||
&self.frightened
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Bundle)]
|
|
||||||
pub struct PlayerBundle {
|
|
||||||
pub player: PlayerControlled,
|
|
||||||
pub position: Position,
|
|
||||||
pub velocity: Velocity,
|
|
||||||
pub buffered_direction: BufferedDirection,
|
|
||||||
pub sprite: Renderable,
|
|
||||||
pub directional_animation: DirectionalAnimation,
|
|
||||||
pub entity_type: EntityType,
|
|
||||||
pub collider: Collider,
|
|
||||||
pub movement_modifiers: MovementModifiers,
|
|
||||||
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_animation: DirectionalAnimation,
|
|
||||||
pub entity_type: EntityType,
|
|
||||||
pub collider: Collider,
|
|
||||||
pub ghost_collider: GhostCollider,
|
|
||||||
pub ghost_state: GhostState,
|
|
||||||
pub last_animation_state: LastAnimationState,
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use crate::platform;
|
use crate::platform;
|
||||||
use crate::systems::components::{
|
use crate::systems::{DirectionalAnimation, Frozen, LinearAnimation, Looping};
|
||||||
DirectionalAnimation, Frozen, GhostAnimation, GhostState, LastAnimationState, LinearAnimation, Looping,
|
|
||||||
};
|
|
||||||
use crate::{
|
use crate::{
|
||||||
map::{
|
map::{
|
||||||
builder::Map,
|
builder::Map,
|
||||||
@@ -9,18 +9,201 @@ use crate::{
|
|||||||
graph::{Edge, TraversalFlags},
|
graph::{Edge, TraversalFlags},
|
||||||
},
|
},
|
||||||
systems::{
|
systems::{
|
||||||
components::{DeltaTime, Ghost},
|
components::DeltaTime,
|
||||||
movement::{Position, Velocity},
|
movement::{Position, Velocity},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
use bevy_ecs::component::Component;
|
||||||
|
use bevy_ecs::resource::Resource;
|
||||||
use tracing::{debug, trace, warn};
|
use tracing::{debug, trace, warn};
|
||||||
|
|
||||||
use crate::systems::GhostAnimations;
|
|
||||||
use bevy_ecs::query::Without;
|
use bevy_ecs::query::Without;
|
||||||
use bevy_ecs::system::{Commands, Query, Res};
|
use bevy_ecs::system::{Commands, Query, Res};
|
||||||
use rand::seq::IndexedRandom;
|
use rand::seq::IndexedRandom;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
|
/// Tag component for eaten ghosts
|
||||||
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
pub struct Eaten;
|
||||||
|
|
||||||
|
/// Tag component for Pac-Man during his death animation.
|
||||||
|
/// This is mainly because the Frozen tag would stop both movement and animation, while the Dying tag can signal that the animation should continue despite being frozen.
|
||||||
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
pub struct Dying;
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum Ghost {
|
||||||
|
Blinky,
|
||||||
|
Pinky,
|
||||||
|
Inky,
|
||||||
|
Clyde,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Ghost {
|
||||||
|
/// Returns the ghost type name for atlas lookups.
|
||||||
|
pub fn as_str(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Ghost::Blinky => "blinky",
|
||||||
|
Ghost::Pinky => "pinky",
|
||||||
|
Ghost::Inky => "inky",
|
||||||
|
Ghost::Clyde => "clyde",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the base movement speed for this ghost type.
|
||||||
|
pub fn base_speed(self) -> f32 {
|
||||||
|
match self {
|
||||||
|
Ghost::Blinky => 1.0,
|
||||||
|
Ghost::Pinky => 0.95,
|
||||||
|
Ghost::Inky => 0.9,
|
||||||
|
Ghost::Clyde => 0.85,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the ghost's color for debug rendering.
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn debug_color(&self) -> sdl2::pixels::Color {
|
||||||
|
match self {
|
||||||
|
Ghost::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
|
||||||
|
Ghost::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
|
||||||
|
Ghost::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
|
||||||
|
Ghost::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
pub enum GhostState {
|
||||||
|
/// Normal ghost behavior - chasing Pac-Man
|
||||||
|
Normal,
|
||||||
|
/// Frightened state after power pellet - ghost can be eaten
|
||||||
|
Frightened {
|
||||||
|
remaining_ticks: u32,
|
||||||
|
flash: bool,
|
||||||
|
remaining_flash_ticks: u32,
|
||||||
|
},
|
||||||
|
/// Eyes state - ghost has been eaten and is returning to ghost house
|
||||||
|
Eyes,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GhostState {
|
||||||
|
/// Creates a new frightened state with the specified duration
|
||||||
|
pub fn new_frightened(total_ticks: u32, flash_start_ticks: u32) -> Self {
|
||||||
|
Self::Frightened {
|
||||||
|
remaining_ticks: total_ticks,
|
||||||
|
flash: false,
|
||||||
|
remaining_flash_ticks: flash_start_ticks, // Time until flashing starts
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Ticks the ghost state, returning true if the state changed.
|
||||||
|
pub fn tick(&mut self) -> bool {
|
||||||
|
if let GhostState::Frightened {
|
||||||
|
remaining_ticks,
|
||||||
|
flash,
|
||||||
|
remaining_flash_ticks,
|
||||||
|
} = self
|
||||||
|
{
|
||||||
|
// Transition out of frightened state
|
||||||
|
if *remaining_ticks == 0 {
|
||||||
|
*self = GhostState::Normal;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
*remaining_ticks -= 1;
|
||||||
|
|
||||||
|
if *remaining_flash_ticks > 0 {
|
||||||
|
*remaining_flash_ticks = remaining_flash_ticks.saturating_sub(1);
|
||||||
|
if *remaining_flash_ticks == 0 {
|
||||||
|
*flash = true;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the appropriate animation state for this ghost state
|
||||||
|
pub fn animation_state(&self) -> GhostAnimation {
|
||||||
|
match self {
|
||||||
|
GhostState::Normal => GhostAnimation::Normal,
|
||||||
|
GhostState::Eyes => GhostAnimation::Eyes,
|
||||||
|
GhostState::Frightened { flash: false, .. } => GhostAnimation::Frightened { flash: false },
|
||||||
|
GhostState::Frightened { flash: true, .. } => GhostAnimation::Frightened { flash: true },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Enumeration of different ghost animation states.
|
||||||
|
/// Note that this is used in micromap which has a fixed size based on the number of variants,
|
||||||
|
/// so extending this should be done with caution, and will require updating the micromap's capacity.
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub enum GhostAnimation {
|
||||||
|
/// Normal ghost appearance with directional movement animations
|
||||||
|
Normal,
|
||||||
|
/// Blue ghost appearance when vulnerable (power pellet active)
|
||||||
|
Frightened { flash: bool },
|
||||||
|
/// Eyes-only animation when ghost has been consumed by Pac-Man (Eaten state)
|
||||||
|
Eyes,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Global resource containing pre-loaded animation sets for all ghost types.
|
||||||
|
///
|
||||||
|
/// This resource is initialized once during game startup and provides O(1) access
|
||||||
|
/// to animation sets for each ghost type. The animation system uses this resource
|
||||||
|
/// to efficiently switch between different ghost states without runtime asset loading.
|
||||||
|
///
|
||||||
|
/// The HashMap is keyed by `Ghost` enum variants (Blinky, Pinky, Inky, Clyde) and
|
||||||
|
/// contains the normal directional animation for each ghost type.
|
||||||
|
#[derive(Resource)]
|
||||||
|
pub struct GhostAnimations {
|
||||||
|
pub normal: HashMap<Ghost, DirectionalAnimation>,
|
||||||
|
pub eyes: DirectionalAnimation,
|
||||||
|
pub frightened: LinearAnimation,
|
||||||
|
pub frightened_flashing: LinearAnimation,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GhostAnimations {
|
||||||
|
/// Creates a new GhostAnimations resource with the provided data.
|
||||||
|
pub fn new(
|
||||||
|
normal: HashMap<Ghost, DirectionalAnimation>,
|
||||||
|
eyes: DirectionalAnimation,
|
||||||
|
frightened: LinearAnimation,
|
||||||
|
frightened_flashing: LinearAnimation,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
normal,
|
||||||
|
eyes,
|
||||||
|
frightened,
|
||||||
|
frightened_flashing,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the normal directional animation for the specified ghost type.
|
||||||
|
pub fn get_normal(&self, ghost_type: &Ghost) -> Option<&DirectionalAnimation> {
|
||||||
|
self.normal.get(ghost_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the eyes animation (shared across all ghosts).
|
||||||
|
pub fn eyes(&self) -> &DirectionalAnimation {
|
||||||
|
&self.eyes
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the frightened animations (shared across all ghosts).
|
||||||
|
pub fn frightened(&self, flash: bool) -> &LinearAnimation {
|
||||||
|
if flash {
|
||||||
|
&self.frightened_flashing
|
||||||
|
} else {
|
||||||
|
&self.frightened
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 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(
|
||||||
map: Res<Map>,
|
map: Res<Map>,
|
||||||
@@ -185,6 +368,10 @@ fn find_direction_to_target(
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Component to track the last animation state for efficient change detection
|
||||||
|
#[derive(Component, Debug, Clone, Copy, PartialEq)]
|
||||||
|
pub struct LastAnimationState(pub GhostAnimation);
|
||||||
|
|
||||||
/// Unified system that manages ghost state transitions and animations with component swapping
|
/// Unified system that manages ghost state transitions and animations with component swapping
|
||||||
pub fn ghost_state_system(
|
pub fn ghost_state_system(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ use sdl2::{
|
|||||||
};
|
};
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
|
|
||||||
use crate::systems::components::DeltaTime;
|
use crate::systems::DeltaTime;
|
||||||
use crate::{
|
use crate::{
|
||||||
events::{GameCommand, GameEvent},
|
events::{GameCommand, GameEvent},
|
||||||
map::direction::Direction,
|
map::direction::Direction,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use bevy_ecs::{
|
|||||||
entity::Entity,
|
entity::Entity,
|
||||||
event::{EventReader, EventWriter},
|
event::{EventReader, EventWriter},
|
||||||
query::With,
|
query::With,
|
||||||
system::{Commands, Query, ResMut},
|
system::{Commands, Query, ResMut, Single},
|
||||||
};
|
};
|
||||||
use tracing::{debug, trace};
|
use tracing::{debug, trace};
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ pub fn item_system(
|
|||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut collision_events: EventReader<GameEvent>,
|
mut collision_events: EventReader<GameEvent>,
|
||||||
mut score: ResMut<ScoreResource>,
|
mut score: ResMut<ScoreResource>,
|
||||||
pacman_query: Query<Entity, With<PacmanCollider>>,
|
pacman: Single<Entity, With<PacmanCollider>>,
|
||||||
item_query: Query<(Entity, &EntityType), With<ItemCollider>>,
|
item_query: Query<(Entity, &EntityType), With<ItemCollider>>,
|
||||||
mut ghost_query: Query<&mut GhostState, With<GhostCollider>>,
|
mut ghost_query: Query<&mut GhostState, With<GhostCollider>>,
|
||||||
mut events: EventWriter<AudioEvent>,
|
mut events: EventWriter<AudioEvent>,
|
||||||
@@ -35,10 +35,10 @@ pub fn item_system(
|
|||||||
for event in collision_events.read() {
|
for event in collision_events.read() {
|
||||||
if let GameEvent::Collision(entity1, entity2) = event {
|
if let GameEvent::Collision(entity1, entity2) = event {
|
||||||
// Check if one is Pacman and the other is an item
|
// Check if one is Pacman and the other is an item
|
||||||
let (_pacman_entity, item_entity) = if pacman_query.get(*entity1).is_ok() && item_query.get(*entity2).is_ok() {
|
let (_, item_entity) = if *pacman == *entity1 && item_query.get(*entity2).is_ok() {
|
||||||
(*entity1, *entity2)
|
(*pacman, *entity2)
|
||||||
} else if pacman_query.get(*entity2).is_ok() && item_query.get(*entity1).is_ok() {
|
} else if *pacman == *entity2 && item_query.get(*entity1).is_ok() {
|
||||||
(*entity2, *entity1)
|
(*pacman, *entity1)
|
||||||
} else {
|
} else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use bevy_ecs::{
|
|||||||
system::{Commands, Query, Res},
|
system::{Commands, Query, Res},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::systems::components::DeltaTime;
|
use crate::systems::DeltaTime;
|
||||||
|
|
||||||
/// Component for entities that should be automatically deleted after a certain number of ticks
|
/// Component for entities that should be automatically deleted after a certain number of ticks
|
||||||
#[derive(Component, Debug, Clone, Copy)]
|
#[derive(Component, Debug, Clone, Copy)]
|
||||||
|
|||||||
@@ -9,9 +9,10 @@ pub mod profiling;
|
|||||||
#[cfg_attr(coverage_nightly, coverage(off))]
|
#[cfg_attr(coverage_nightly, coverage(off))]
|
||||||
pub mod render;
|
pub mod render;
|
||||||
|
|
||||||
|
pub mod animation;
|
||||||
pub mod blinking;
|
pub mod blinking;
|
||||||
pub mod collision;
|
pub mod collision;
|
||||||
pub mod components;
|
pub mod common;
|
||||||
pub mod ghost;
|
pub mod ghost;
|
||||||
pub mod input;
|
pub mod input;
|
||||||
pub mod item;
|
pub mod item;
|
||||||
@@ -22,10 +23,11 @@ pub mod state;
|
|||||||
|
|
||||||
// Re-export all the modules. Do not fine-tune the exports.
|
// Re-export all the modules. Do not fine-tune the exports.
|
||||||
|
|
||||||
|
pub use self::animation::*;
|
||||||
pub use self::audio::*;
|
pub use self::audio::*;
|
||||||
pub use self::blinking::*;
|
pub use self::blinking::*;
|
||||||
pub use self::collision::*;
|
pub use self::collision::*;
|
||||||
pub use self::components::*;
|
pub use self::common::*;
|
||||||
pub use self::debug::*;
|
pub use self::debug::*;
|
||||||
pub use self::ghost::*;
|
pub use self::ghost::*;
|
||||||
pub use self::input::*;
|
pub use self::input::*;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use bevy_ecs::{
|
use bevy_ecs::{
|
||||||
|
component::Component,
|
||||||
event::EventReader,
|
event::EventReader,
|
||||||
query::{With, Without},
|
query::{With, Without},
|
||||||
system::{Query, Res, ResMut, Single},
|
system::{Query, Res, ResMut, Single},
|
||||||
@@ -9,13 +10,17 @@ use crate::{
|
|||||||
events::{GameCommand, GameEvent},
|
events::{GameCommand, GameEvent},
|
||||||
map::{builder::Map, graph::Edge},
|
map::{builder::Map, graph::Edge},
|
||||||
systems::{
|
systems::{
|
||||||
components::{DeltaTime, EntityType, Frozen, GlobalState, MovementModifiers, PlayerControlled},
|
components::{DeltaTime, EntityType, Frozen, GlobalState, MovementModifiers},
|
||||||
debug::DebugState,
|
debug::DebugState,
|
||||||
movement::{BufferedDirection, Position, Velocity},
|
movement::{BufferedDirection, Position, Velocity},
|
||||||
AudioState,
|
AudioState,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A tag component for entities that are controlled by the player.
|
||||||
|
#[derive(Default, Component)]
|
||||||
|
pub struct PlayerControlled;
|
||||||
|
|
||||||
pub fn can_traverse(entity_type: EntityType, edge: Edge) -> bool {
|
pub fn can_traverse(entity_type: EntityType, edge: Edge) -> bool {
|
||||||
let entity_flags = entity_type.traversal_flags();
|
let entity_flags = entity_type.traversal_flags();
|
||||||
edge.traversal_flags.contains(entity_flags)
|
edge.traversal_flags.contains(entity_flags)
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
use crate::map::builder::Map;
|
use crate::map::builder::Map;
|
||||||
use crate::map::direction::Direction;
|
use crate::map::direction::Direction;
|
||||||
use crate::systems::input::TouchState;
|
|
||||||
use crate::systems::{
|
use crate::systems::{
|
||||||
debug_render_system, BatchedLinesResource, Collider, CursorPosition, DebugState, DebugTextureResource, DeltaTime,
|
debug_render_system, BatchedLinesResource, Collider, CursorPosition, DebugState, DebugTextureResource, GameStage, PlayerLife,
|
||||||
DirectionalAnimation, Dying, Frozen, GameStage, LinearAnimation, Looping, PlayerLife, PlayerLives, Position, Renderable,
|
PlayerLives, Position, ScoreResource, StartupSequence, SystemId, SystemTimings, TouchState, TtfAtlasResource,
|
||||||
ScoreResource, StartupSequence, SystemId, SystemTimings, TtfAtlasResource, Velocity,
|
|
||||||
};
|
};
|
||||||
use crate::texture::sprite::SpriteAtlas;
|
use crate::texture::sprite::{AtlasTile, SpriteAtlas};
|
||||||
use crate::texture::sprites::{GameSprite, PacmanSprite};
|
use crate::texture::sprites::{GameSprite, PacmanSprite};
|
||||||
use crate::texture::text::TextTexture;
|
use crate::texture::text::TextTexture;
|
||||||
use crate::{
|
use crate::{
|
||||||
@@ -16,7 +14,7 @@ use crate::{
|
|||||||
use bevy_ecs::component::Component;
|
use bevy_ecs::component::Component;
|
||||||
use bevy_ecs::entity::Entity;
|
use bevy_ecs::entity::Entity;
|
||||||
use bevy_ecs::event::EventWriter;
|
use bevy_ecs::event::EventWriter;
|
||||||
use bevy_ecs::query::{Changed, Has, Or, With, Without};
|
use bevy_ecs::query::{Changed, Or, With, Without};
|
||||||
use bevy_ecs::removal_detection::RemovedComponents;
|
use bevy_ecs::removal_detection::RemovedComponents;
|
||||||
use bevy_ecs::resource::Resource;
|
use bevy_ecs::resource::Resource;
|
||||||
use bevy_ecs::system::{Commands, NonSendMut, Query, Res, ResMut};
|
use bevy_ecs::system::{Commands, NonSendMut, Query, Res, ResMut};
|
||||||
@@ -27,6 +25,15 @@ use sdl2::render::{BlendMode, Canvas, Texture};
|
|||||||
use sdl2::video::Window;
|
use sdl2::video::Window;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
|
/// A component for entities that have a sprite, with a layer for ordering.
|
||||||
|
///
|
||||||
|
/// This is intended to be modified by other entities allowing animation.
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct Renderable {
|
||||||
|
pub sprite: AtlasTile,
|
||||||
|
pub layer: u8,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Resource, Default)]
|
#[derive(Resource, Default)]
|
||||||
pub struct RenderDirty(pub bool);
|
pub struct RenderDirty(pub bool);
|
||||||
|
|
||||||
@@ -56,77 +63,6 @@ pub fn dirty_render_system(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Updates directional animated entities with synchronized timing across directions.
|
|
||||||
///
|
|
||||||
/// This runs before the render system to update sprites based on current direction and movement state.
|
|
||||||
/// All directions share the same frame timing to ensure perfect synchronization.
|
|
||||||
pub fn directional_render_system(
|
|
||||||
dt: Res<DeltaTime>,
|
|
||||||
mut query: Query<(&Position, &Velocity, &mut DirectionalAnimation, &mut Renderable), Without<Frozen>>,
|
|
||||||
) {
|
|
||||||
let ticks = (dt.seconds * 60.0).round() as u16; // Convert from seconds to ticks at 60 ticks/sec
|
|
||||||
|
|
||||||
for (position, velocity, mut anim, mut renderable) in query.iter_mut() {
|
|
||||||
let stopped = matches!(position, Position::Stopped { .. });
|
|
||||||
|
|
||||||
// Only tick animation when moving to preserve stopped frame
|
|
||||||
if !stopped {
|
|
||||||
// Tick shared animation state
|
|
||||||
anim.time_bank += ticks;
|
|
||||||
while anim.time_bank >= anim.frame_duration {
|
|
||||||
anim.time_bank -= anim.frame_duration;
|
|
||||||
anim.current_frame += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get tiles for current direction and movement state
|
|
||||||
let tiles = if stopped {
|
|
||||||
anim.stopped_tiles.get(velocity.direction)
|
|
||||||
} else {
|
|
||||||
anim.moving_tiles.get(velocity.direction)
|
|
||||||
};
|
|
||||||
|
|
||||||
if !tiles.is_empty() {
|
|
||||||
let new_tile = tiles.get_tile(anim.current_frame);
|
|
||||||
if renderable.sprite != new_tile {
|
|
||||||
renderable.sprite = new_tile;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// System that updates `Renderable` sprites for entities with `LinearAnimation`.
|
|
||||||
#[allow(clippy::type_complexity)]
|
|
||||||
pub fn linear_render_system(
|
|
||||||
dt: Res<DeltaTime>,
|
|
||||||
mut query: Query<(&mut LinearAnimation, &mut Renderable, Has<Looping>), Or<(Without<Frozen>, With<Dying>)>>,
|
|
||||||
) {
|
|
||||||
for (mut anim, mut renderable, looping) in query.iter_mut() {
|
|
||||||
if anim.finished {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
anim.time_bank += dt.ticks as u16;
|
|
||||||
let frames_to_advance = (anim.time_bank / anim.frame_duration) as usize;
|
|
||||||
|
|
||||||
if frames_to_advance == 0 {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let total_frames = anim.tiles.len();
|
|
||||||
|
|
||||||
if !looping && anim.current_frame + frames_to_advance >= total_frames {
|
|
||||||
anim.finished = true;
|
|
||||||
anim.current_frame = total_frames - 1;
|
|
||||||
} else {
|
|
||||||
anim.current_frame += frames_to_advance;
|
|
||||||
}
|
|
||||||
|
|
||||||
anim.time_bank %= anim.frame_duration;
|
|
||||||
renderable.sprite = anim.tiles.get_tile(anim.current_frame);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// System that manages player life sprite entities.
|
/// System that manages player life sprite entities.
|
||||||
/// Spawns and despawns life sprite entities based on changes to PlayerLives resource.
|
/// Spawns and despawns life sprite entities based on changes to PlayerLives resource.
|
||||||
/// Each life sprite is positioned based on its index (0, 1, 2, etc. from left to right).
|
/// Each life sprite is positioned based on its index (0, 1, 2, etc. from left to right).
|
||||||
|
|||||||
@@ -5,8 +5,7 @@
|
|||||||
//! The `GameSprite` enum is the main entry point, and its `to_path` method
|
//! The `GameSprite` enum is the main entry point, and its `to_path` method
|
||||||
//! generates the correct path for a given sprite in the texture atlas.
|
//! generates the correct path for a given sprite in the texture atlas.
|
||||||
|
|
||||||
use crate::map::direction::Direction;
|
use crate::{map::direction::Direction, systems::Ghost};
|
||||||
use crate::systems::components::Ghost;
|
|
||||||
|
|
||||||
/// Represents the different sprites for Pac-Man.
|
/// Represents the different sprites for Pac-Man.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
|||||||
@@ -1,9 +1,5 @@
|
|||||||
use bevy_ecs::{entity::Entity, system::RunSystemOnce, world::World};
|
use bevy_ecs::{entity::Entity, system::RunSystemOnce, world::World};
|
||||||
use pacman::systems::{
|
use pacman::systems::{blinking_system, Blinking, DeltaTime, Frozen, Hidden, Renderable};
|
||||||
blinking::{blinking_system, Blinking},
|
|
||||||
components::{DeltaTime, Renderable},
|
|
||||||
Frozen, Hidden,
|
|
||||||
};
|
|
||||||
use speculoos::prelude::*;
|
use speculoos::prelude::*;
|
||||||
|
|
||||||
mod common;
|
mod common;
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
use pacman::{
|
use pacman::{
|
||||||
game::ATLAS_FRAMES,
|
game::ATLAS_FRAMES,
|
||||||
map::direction::Direction,
|
map::direction::Direction,
|
||||||
systems::components::Ghost,
|
systems::Ghost,
|
||||||
texture::sprites::{FrightenedColor, GameSprite, GhostSprite, MazeSprite, PacmanSprite},
|
texture::sprites::{FrightenedColor, GameSprite, GhostSprite, MazeSprite, PacmanSprite},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user