Compare commits

...

3 Commits

14 changed files with 599 additions and 435 deletions

View File

@@ -8,22 +8,22 @@ use crate::error::{GameError, GameResult, TextureError};
use crate::events::GameEvent;
use crate::map::builder::Map;
use crate::systems::blinking::Blinking;
use crate::systems::movement::{Movable, MovementState, Position};
use crate::systems::movement::{BufferedDirection, Position, Velocity};
use crate::systems::player::player_movement_system;
use crate::systems::profiling::SystemId;
use crate::systems::{
audio::{audio_system, AudioEvent, AudioResource},
blinking::blinking_system,
collision::collision_system,
components::{
AudioState, Collider, DeltaTime, DirectionalAnimated, EntityType, GhostBehavior, GhostBundle, GhostCollider, GhostType,
GlobalState, ItemBundle, ItemCollider, PacmanCollider, PlayerBundle, PlayerControlled, RenderDirty, Renderable,
ScoreResource,
AudioState, Collider, DeltaTime, DirectionalAnimated, EntityType, Ghost, GhostBundle, GhostCollider, GlobalState,
ItemBundle, ItemCollider, PacmanCollider, PlayerBundle, PlayerControlled, RenderDirty, Renderable, ScoreResource,
},
control::player_system,
debug::{debug_render_system, DebugState, DebugTextureResource},
ghost::ghost_ai_system,
ghost::ghost_movement_system,
input::input_system,
item::item_system,
movement::movement_system,
player::player_control_system,
profiling::{profile, SystemTimings},
render::{directional_render_system, dirty_render_system, render_system, BackbufferResource, MapTextureResource},
};
@@ -158,16 +158,12 @@ impl Game {
let player = PlayerBundle {
player: PlayerControlled,
position: Position {
node: pacman_start_node,
edge_progress: None,
},
movement_state: MovementState::Stopped,
movable: Movable {
position: Position::Stopped { node: pacman_start_node },
velocity: Velocity {
speed: 1.15,
current_direction: Direction::Left,
requested_direction: Some(Direction::Left), // Start moving left immediately
direction: Direction::Left,
},
buffered_direction: BufferedDirection::None,
sprite: Renderable {
sprite: SpriteAtlas::get_tile(&atlas, "pacman/full.png")
.ok_or_else(|| GameError::Texture(TextureError::AtlasTileNotFound("pacman/full.png".to_string())))?,
@@ -212,20 +208,20 @@ impl Game {
);
schedule.add_systems(
(
profile("input", input_system),
profile("player", player_system),
profile("ghost_ai", ghost_ai_system),
profile("movement", movement_system),
profile("collision", collision_system),
profile("item", item_system),
profile("audio", audio_system),
profile("blinking", blinking_system),
profile("directional_render", directional_render_system),
profile("dirty_render", dirty_render_system),
profile("render", render_system),
profile("debug_render", debug_render_system),
profile(SystemId::Input, input_system),
profile(SystemId::PlayerControls, player_control_system),
profile(SystemId::PlayerMovement, player_movement_system),
profile(SystemId::Ghost, ghost_movement_system),
profile(SystemId::Collision, collision_system),
profile(SystemId::Item, item_system),
profile(SystemId::Audio, audio_system),
profile(SystemId::Blinking, blinking_system),
profile(SystemId::DirectionalRender, directional_render_system),
profile(SystemId::DirtyRender, dirty_render_system),
profile(SystemId::Render, render_system),
profile(SystemId::DebugRender, debug_render_system),
profile(
"present",
SystemId::Present,
|mut canvas: NonSendMut<&mut Canvas<Window>>,
backbuffer: NonSendMut<BackbufferResource>,
debug_state: Res<DebugState>,
@@ -269,10 +265,7 @@ impl Game {
};
let mut item = world.spawn(ItemBundle {
position: Position {
node: node_id,
edge_progress: None,
},
position: Position::Stopped { node: node_id },
sprite: Renderable {
sprite,
layer: 1,
@@ -300,10 +293,10 @@ impl Game {
let ghost_start_positions = {
let map = world.resource::<Map>();
[
(GhostType::Blinky, map.start_positions.blinky),
(GhostType::Pinky, map.start_positions.pinky),
(GhostType::Inky, map.start_positions.inky),
(GhostType::Clyde, map.start_positions.clyde),
(Ghost::Blinky, map.start_positions.blinky),
(Ghost::Pinky, map.start_positions.pinky),
(Ghost::Inky, map.start_positions.inky),
(Ghost::Clyde, map.start_positions.clyde),
]
};
@@ -363,17 +356,11 @@ impl Game {
}
GhostBundle {
ghost_type,
ghost_behavior: GhostBehavior::default(),
position: Position {
node: start_node,
edge_progress: None,
},
movement_state: MovementState::Stopped,
movable: Movable {
ghost: ghost_type,
position: Position::Stopped { node: start_node },
velocity: Velocity {
speed: ghost_type.base_speed(),
current_direction: Direction::Left,
requested_direction: Some(Direction::Left), // Start with some movement
direction: Direction::Left,
},
sprite: Renderable {
sprite: SpriteAtlas::get_tile(atlas, &format!("ghost/{}/left_a.png", ghost_type.as_str())).ok_or_else(

View File

@@ -19,7 +19,10 @@ pub fn collision_system(
// Check PACMAN × ITEM collisions
for (pacman_entity, pacman_pos, pacman_collider) in pacman_query.iter() {
for (item_entity, item_pos, item_collider) in item_query.iter() {
match (pacman_pos.get_pixel_pos(&map.graph), item_pos.get_pixel_pos(&map.graph)) {
match (
pacman_pos.get_pixel_position(&map.graph),
item_pos.get_pixel_position(&map.graph),
) {
(Ok(pacman_pixel), Ok(item_pixel)) => {
// Calculate the distance between the two entities's precise pixel positions
let distance = pacman_pixel.distance(item_pixel);

View File

@@ -3,7 +3,7 @@ use bitflags::bitflags;
use crate::{
entity::graph::TraversalFlags,
systems::movement::{Movable, MovementState, Position},
systems::movement::{BufferedDirection, Position, Velocity},
texture::{animated::AnimatedTexture, sprite::AtlasTile},
};
@@ -11,61 +11,42 @@ use crate::{
#[derive(Default, Component)]
pub struct PlayerControlled;
/// The four classic ghost types.
#[derive(Component, Debug, Clone, Copy, PartialEq, Eq)]
pub enum GhostType {
pub enum Ghost {
Blinky,
Pinky,
Inky,
Clyde,
}
impl GhostType {
impl Ghost {
/// Returns the ghost type name for atlas lookups.
pub fn as_str(self) -> &'static str {
match self {
GhostType::Blinky => "blinky",
GhostType::Pinky => "pinky",
GhostType::Inky => "inky",
GhostType::Clyde => "clyde",
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 {
GhostType::Blinky => 1.0,
GhostType::Pinky => 0.95,
GhostType::Inky => 0.9,
GhostType::Clyde => 0.85,
Ghost::Blinky => 1.0,
Ghost::Pinky => 0.95,
Ghost::Inky => 0.9,
Ghost::Clyde => 0.85,
}
}
/// Returns the ghost's color for debug rendering.
pub fn debug_color(&self) -> sdl2::pixels::Color {
match self {
GhostType::Blinky => sdl2::pixels::Color::RGB(255, 0, 0), // Red
GhostType::Pinky => sdl2::pixels::Color::RGB(255, 182, 255), // Pink
GhostType::Inky => sdl2::pixels::Color::RGB(0, 255, 255), // Cyan
GhostType::Clyde => sdl2::pixels::Color::RGB(255, 182, 85), // Orange
}
}
}
/// Ghost AI behavior component - controls randomized movement decisions.
#[derive(Component)]
pub struct GhostBehavior {
/// Timer for making new direction decisions
pub decision_timer: f32,
/// Interval between direction decisions (in seconds)
pub decision_interval: f32,
}
impl Default for GhostBehavior {
fn default() -> Self {
Self {
decision_timer: 0.0,
decision_interval: 0.5, // Make decisions every half second
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
}
}
}
@@ -135,8 +116,8 @@ pub struct ItemCollider;
pub struct PlayerBundle {
pub player: PlayerControlled,
pub position: Position,
pub movement_state: MovementState,
pub movable: Movable,
pub velocity: Velocity,
pub buffered_direction: BufferedDirection,
pub sprite: Renderable,
pub directional_animated: DirectionalAnimated,
pub entity_type: EntityType,
@@ -155,11 +136,9 @@ pub struct ItemBundle {
#[derive(Bundle)]
pub struct GhostBundle {
pub ghost_type: GhostType,
pub ghost_behavior: GhostBehavior,
pub ghost: Ghost,
pub position: Position,
pub movement_state: MovementState,
pub movable: Movable,
pub velocity: Velocity,
pub sprite: Renderable,
pub directional_animated: DirectionalAnimated,
pub entity_type: EntityType,

View File

@@ -1,58 +0,0 @@
use bevy_ecs::{
event::{EventReader, EventWriter},
prelude::ResMut,
query::With,
system::Query,
};
use crate::{
error::GameError,
events::{GameCommand, GameEvent},
systems::components::{AudioState, GlobalState, PlayerControlled},
systems::debug::DebugState,
systems::movement::Movable,
};
// Handles player input and control
pub fn player_system(
mut events: EventReader<GameEvent>,
mut state: ResMut<GlobalState>,
mut debug_state: ResMut<DebugState>,
mut audio_state: ResMut<AudioState>,
mut players: Query<&mut Movable, With<PlayerControlled>>,
mut errors: EventWriter<GameError>,
) {
// Get the player's movable component (ensuring there is only one player)
let mut movable = match players.single_mut() {
Ok(movable) => movable,
Err(e) => {
errors.write(GameError::InvalidState(format!(
"No/multiple entities queried for player system: {}",
e
)));
return;
}
};
// Handle events
for event in events.read() {
if let GameEvent::Command(command) = event {
match command {
GameCommand::MovePlayer(direction) => {
movable.requested_direction = Some(*direction);
}
GameCommand::Exit => {
state.exit = true;
}
GameCommand::ToggleDebug => {
*debug_state = debug_state.next();
}
GameCommand::MuteAudio => {
audio_state.muted = !audio_state.muted;
tracing::info!("Audio {}", if audio_state.muted { "muted" } else { "unmuted" });
}
_ => {}
}
}
}
}

View File

@@ -76,20 +76,14 @@ fn render_timing_display(
let font = ttf_context.load_font("assets/site/TerminalVector.ttf", 12).unwrap();
// Format timing information using the formatting module
let timing_text = timings.format_timing_display();
// Split text by newlines and render each line separately
let lines: Vec<&str> = timing_text.lines().collect();
if lines.is_empty() {
return;
}
let lines = timings.format_timing_display();
let line_height = 14; // Approximate line height for 12pt font
let padding = 10;
// Calculate background dimensions
let max_width = lines
.iter()
.filter(|&&l| !l.is_empty()) // Don't consider empty lines for width
.filter(|l| !l.is_empty()) // Don't consider empty lines for width
.map(|line| font.size_of(line).unwrap().0)
.max()
.unwrap_or(0);
@@ -193,7 +187,7 @@ pub fn debug_render_system(
DebugState::Collision => {
debug_canvas.set_draw_color(Color::GREEN);
for (collider, position) in colliders.iter() {
let pos = position.get_pixel_pos(&map.graph).unwrap();
let pos = position.get_pixel_position(&map.graph).unwrap();
// Transform position and size using common methods
let (x, y) = transform_position((pos.x, pos.y), output_size, logical_size);

View File

@@ -1,6 +1,9 @@
use num_width::NumberWidth;
use smallvec::SmallVec;
use std::time::Duration;
use strum::EnumCount;
use crate::systems::profiling::SystemId;
// Helper to split a duration into a integer, decimal, and unit
fn get_value(duration: &Duration) -> (u64, u32, &'static str) {
@@ -34,7 +37,9 @@ fn get_value(duration: &Duration) -> (u64, u32, &'static str) {
}
/// Formats timing data into a vector of strings with proper alignment
pub fn format_timing_display(timing_data: impl IntoIterator<Item = (String, Duration, Duration)>) -> SmallVec<[String; 12]> {
pub fn format_timing_display(
timing_data: impl IntoIterator<Item = (String, Duration, Duration)>,
) -> SmallVec<[String; SystemId::COUNT]> {
let mut iter = timing_data.into_iter().peekable();
if iter.peek().is_none() {
return SmallVec::new();
@@ -98,5 +103,5 @@ pub fn format_timing_display(timing_data: impl IntoIterator<Item = (String, Dura
max_std_int_width = max_std_int_width,
max_std_decimal_width = max_std_decimal_width
)
}).collect::<SmallVec<[String; 12]>>()
}).collect::<SmallVec<[String; SystemId::COUNT]>>()
}

View File

@@ -3,11 +3,11 @@ use rand::prelude::*;
use smallvec::SmallVec;
use crate::{
entity::direction::Direction,
entity::{direction::Direction, graph::Edge},
map::builder::Map,
systems::{
components::{DeltaTime, EntityType, GhostBehavior, GhostType},
movement::{Movable, Position},
components::{DeltaTime, Ghost},
movement::{Position, Velocity},
},
};
@@ -15,63 +15,55 @@ use crate::{
///
/// This system runs on all ghosts and makes periodic decisions about
/// which direction to move in when they reach intersections.
pub fn ghost_ai_system(
pub fn ghost_movement_system(
map: Res<Map>,
delta_time: Res<DeltaTime>,
mut ghosts: Query<(&mut GhostBehavior, &mut Movable, &Position, &EntityType, &GhostType)>,
mut ghosts: Query<(&Ghost, &mut Velocity, &mut Position)>,
) {
for (mut ghost_behavior, mut movable, position, entity_type, _ghost_type) in ghosts.iter_mut() {
// Only process ghosts
if *entity_type != EntityType::Ghost {
continue;
}
for (_ghost, mut velocity, mut position) in ghosts.iter_mut() {
let mut distance = velocity.speed * 60.0 * delta_time.0;
loop {
match *position {
Position::Stopped { node: current_node } => {
let intersection = &map.graph.adjacency_list[current_node];
let opposite = velocity.direction.opposite();
// Update decision timer
ghost_behavior.decision_timer += delta_time.0;
let mut non_opposite_options: SmallVec<[Edge; 3]> = SmallVec::new();
// Check if we should make a new direction decision
let should_decide = ghost_behavior.decision_timer >= ghost_behavior.decision_interval;
let at_intersection = position.is_at_node();
// Collect all available directions that ghosts can traverse
for edge in Direction::DIRECTIONS.iter().flat_map(|d| intersection.get(*d)) {
if edge.traversal_flags.contains(crate::entity::graph::TraversalFlags::GHOST)
&& edge.direction != opposite
{
non_opposite_options.push(edge);
}
}
if should_decide && at_intersection {
choose_random_direction(&map, &mut movable, position);
ghost_behavior.decision_timer = 0.0;
}
}
}
let new_edge: Edge = if non_opposite_options.is_empty() {
if let Some(edge) = intersection.get(opposite) {
edge
} else {
break;
}
} else {
*non_opposite_options.choose(&mut SmallRng::from_os_rng()).unwrap()
};
/// Chooses a random available direction for a ghost at an intersection.
///
/// This function mirrors the behavior from the old ghost implementation,
/// preferring not to reverse direction unless it's the only option.
fn choose_random_direction(map: &Map, movable: &mut Movable, position: &Position) {
let current_node = position.current_node();
let intersection = &map.graph.adjacency_list[current_node];
// Collect all available directions that ghosts can traverse
let mut available_directions = SmallVec::<[Direction; 4]>::new();
for direction in Direction::DIRECTIONS {
if let Some(edge) = intersection.get(direction) {
// Check if ghosts can traverse this edge
if edge.traversal_flags.contains(crate::entity::graph::TraversalFlags::GHOST) {
available_directions.push(direction);
velocity.direction = new_edge.direction;
*position = Position::Moving {
from: current_node,
to: new_edge.target,
remaining_distance: new_edge.distance,
};
}
Position::Moving { .. } => {
if let Some(overflow) = position.tick(distance) {
distance = overflow;
} else {
break;
}
}
}
}
}
// Choose a random direction (avoid reversing unless necessary)
if !available_directions.is_empty() {
let mut rng = SmallRng::from_os_rng();
// Filter out the opposite direction if possible, but allow it if we have limited options
let opposite = movable.current_direction.opposite();
let filtered_directions: Vec<_> = available_directions
.iter()
.filter(|&&dir| dir != opposite || available_directions.len() <= 2)
.collect();
if let Some(&random_direction) = filtered_directions.choose(&mut rng) {
movable.requested_direction = Some(*random_direction);
}
}
}

View File

@@ -1,6 +1,10 @@
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use bevy_ecs::{event::EventWriter, prelude::Res, resource::Resource, system::NonSendMut};
use bevy_ecs::{
event::EventWriter,
resource::Resource,
system::{NonSendMut, ResMut},
};
use sdl2::{event::Event, keyboard::Keycode, EventPump};
use crate::{
@@ -11,6 +15,8 @@ use crate::{
#[derive(Debug, Clone, Resource)]
pub struct Bindings {
key_bindings: HashMap<Keycode, GameCommand>,
movement_keys: HashSet<Keycode>,
last_movement_key: Option<Keycode>,
}
impl Default for Bindings {
@@ -35,23 +41,74 @@ impl Default for Bindings {
key_bindings.insert(Keycode::Escape, GameCommand::Exit);
key_bindings.insert(Keycode::Q, GameCommand::Exit);
Self { key_bindings }
let movement_keys = HashSet::from([
Keycode::W,
Keycode::A,
Keycode::S,
Keycode::D,
Keycode::Up,
Keycode::Down,
Keycode::Left,
Keycode::Right,
]);
Self {
key_bindings,
movement_keys,
last_movement_key: None,
}
}
}
pub fn input_system(bindings: Res<Bindings>, mut writer: EventWriter<GameEvent>, mut pump: NonSendMut<&'static mut EventPump>) {
pub fn input_system(
mut bindings: ResMut<Bindings>,
mut writer: EventWriter<GameEvent>,
mut pump: NonSendMut<&'static mut EventPump>,
) {
let mut movement_key_pressed = false;
for event in pump.poll_iter() {
match event {
Event::Quit { .. } => {
writer.write(GameEvent::Command(GameCommand::Exit));
}
Event::KeyDown { keycode: Some(key), .. } => {
Event::KeyUp {
repeat: false,
keycode: Some(key),
..
} => {
// If the last movement key was released, then forget it.
if let Some(last_movement_key) = bindings.last_movement_key {
if last_movement_key == key {
bindings.last_movement_key = None;
}
}
}
Event::KeyDown {
keycode: Some(key),
repeat: false,
..
} => {
let command = bindings.key_bindings.get(&key).copied();
if let Some(command) = command {
writer.write(GameEvent::Command(command));
}
if bindings.movement_keys.contains(&key) {
movement_key_pressed = true;
bindings.last_movement_key = Some(key);
}
}
_ => {}
}
}
if let Some(last_movement_key) = bindings.last_movement_key {
if !movement_key_pressed {
let command = bindings.key_bindings.get(&last_movement_key).copied();
if let Some(command) = command {
writer.write(GameEvent::Command(command));
}
}
}
}

View File

@@ -7,12 +7,12 @@ pub mod audio;
pub mod blinking;
pub mod collision;
pub mod components;
pub mod control;
pub mod debug;
pub mod formatting;
pub mod ghost;
pub mod input;
pub mod item;
pub mod movement;
pub mod player;
pub mod profiling;
pub mod render;

View File

@@ -1,49 +1,40 @@
use crate::entity::direction::Direction;
use crate::entity::graph::Graph;
use crate::entity::{direction::Direction, graph::Edge};
use crate::error::{EntityError, GameError, GameResult};
use crate::map::builder::Map;
use crate::systems::components::{DeltaTime, EntityType};
use crate::error::{EntityError, GameResult};
use bevy_ecs::component::Component;
use bevy_ecs::event::EventWriter;
use bevy_ecs::system::{Query, Res};
use glam::Vec2;
/// A unique identifier for a node, represented by its index in the graph's storage.
pub type NodeId = usize;
/// Progress along an edge between two nodes.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EdgeProgress {
pub target_node: NodeId,
/// Progress from 0.0 (at source node) to 1.0 (at target node)
pub progress: f32,
/// A component that represents the speed and cardinal direction of an entity.
/// Speed is static, only applied when the entity has an edge to traverse.
/// Direction is dynamic, but is controlled externally.
#[derive(Component, Debug, Copy, Clone, PartialEq)]
pub struct Velocity {
pub speed: f32,
pub direction: Direction,
}
/// A component that represents a direction change that is only remembered for a period of time.
/// This is used to allow entities to change direction before they reach their current target node (which consumes their buffered direction).
#[derive(Component, Debug, Copy, Clone, PartialEq)]
pub enum BufferedDirection {
None,
Some { direction: Direction, remaining_time: f32 },
}
/// Pure spatial position component - works for both static and dynamic entities.
#[derive(Component, Debug, Copy, Clone, PartialEq, Default)]
pub struct Position {
/// The current/primary node this entity is at or traveling from
pub node: NodeId,
/// If Some, entity is traveling between nodes. If None, entity is stationary at node.
pub edge_progress: Option<EdgeProgress>,
}
/// Explicit movement state - only for entities that can move.
#[derive(Component, Debug, Clone, Copy, PartialEq, Default)]
pub enum MovementState {
#[default]
Stopped,
Moving {
direction: Direction,
#[derive(Component, Debug, Copy, Clone, PartialEq)]
pub enum Position {
Stopped {
node: NodeId,
},
Moving {
from: NodeId,
to: NodeId,
remaining_distance: f32,
},
}
/// Movement capability and parameters - only for entities that can move.
#[derive(Component, Debug, Clone, Copy)]
pub struct Movable {
pub speed: f32,
pub current_direction: Direction,
pub requested_direction: Option<Direction>,
}
impl Position {
@@ -55,26 +46,32 @@ impl Position {
/// # Errors
///
/// Returns an `EntityError` if the node or edge is not found.
pub fn get_pixel_pos(&self, graph: &Graph) -> GameResult<Vec2> {
let pos = match &self.edge_progress {
None => {
pub fn get_pixel_position(&self, graph: &Graph) -> GameResult<Vec2> {
let pos = match &self {
Position::Stopped { node } => {
// Entity is stationary at a node
let node = graph.get_node(self.node).ok_or(EntityError::NodeNotFound(self.node))?;
let node = graph.get_node(*node).ok_or(EntityError::NodeNotFound(*node))?;
node.position
}
Some(edge_progress) => {
Position::Moving {
from,
to,
remaining_distance,
} => {
// Entity is traveling between nodes
let from_node = graph.get_node(self.node).ok_or(EntityError::NodeNotFound(self.node))?;
let to_node = graph
.get_node(edge_progress.target_node)
.ok_or(EntityError::NodeNotFound(edge_progress.target_node))?;
let from_node = graph.get_node(*from).ok_or(EntityError::NodeNotFound(*from))?;
let to_node = graph.get_node(*to).ok_or(EntityError::NodeNotFound(*to))?;
let edge = graph
.find_edge(*from, *to)
.ok_or(EntityError::EdgeNotFound { from: *from, to: *to })?;
// For zero-distance edges (tunnels), progress >= 1.0 means we're at the target
if edge_progress.progress >= 1.0 {
if edge.distance == 0.0 {
to_node.position
} else {
// Interpolate position based on progress
from_node.position + (to_node.position - from_node.position) * edge_progress.progress
let progress = 1.0 - (*remaining_distance / edge.distance);
from_node.position.lerp(to_node.position, progress)
}
}
};
@@ -84,183 +81,218 @@ impl Position {
pos.y + crate::constants::BOARD_PIXEL_OFFSET.y as f32,
))
}
}
#[allow(dead_code)]
impl Position {
/// Moves the position by a given distance towards it's current target node.
///
/// Returns the overflow distance, if any.
pub fn tick(&mut self, distance: f32) -> Option<f32> {
if distance <= 0.0 || self.is_at_node() {
return None;
}
match self {
Position::Moving {
to, remaining_distance, ..
} => {
// If the remaining distance is less than or equal the distance, we'll reach the target
if *remaining_distance <= distance {
let overflow: Option<f32> = if *remaining_distance != distance {
Some(distance - *remaining_distance)
} else {
None
};
*self = Position::Stopped { node: *to };
return overflow;
}
*remaining_distance -= distance;
None
}
_ => unreachable!(),
}
}
/// Returns `true` if the position is exactly at a node (not traveling).
pub fn is_at_node(&self) -> bool {
self.edge_progress.is_none()
matches!(self, Position::Stopped { .. })
}
/// Returns the `NodeId` of the current node (source of travel if moving).
pub fn current_node(&self) -> NodeId {
self.node
match self {
Position::Stopped { node } => *node,
Position::Moving { from, .. } => *from,
}
}
/// Returns the `NodeId` of the destination node, if currently traveling.
pub fn target_node(&self) -> Option<NodeId> {
self.edge_progress.as_ref().map(|ep| ep.target_node)
match self {
Position::Stopped { .. } => None,
Position::Moving { to, .. } => Some(*to),
}
}
/// Returns `true` if the entity is traveling between nodes.
pub fn is_moving(&self) -> bool {
self.edge_progress.is_some()
matches!(self, Position::Moving { .. })
}
}
fn can_traverse(entity_type: EntityType, edge: Edge) -> bool {
let entity_flags = entity_type.traversal_flags();
edge.traversal_flags.contains(entity_flags)
}
// pub fn movement_system(
// map: Res<Map>,
// delta_time: Res<DeltaTime>,
// mut entities: Query<(&mut Position, &mut Movable, &EntityType)>,
// mut errors: EventWriter<GameError>,
// ) {
// for (mut position, mut movable, entity_type) in entities.iter_mut() {
// let distance = movable.speed * 60.0 * delta_time.0;
pub fn movement_system(
map: Res<Map>,
delta_time: Res<DeltaTime>,
mut entities: Query<(&mut MovementState, &mut Movable, &mut Position, &EntityType)>,
mut errors: EventWriter<GameError>,
) {
for (mut movement_state, mut movable, mut position, entity_type) in entities.iter_mut() {
let distance = movable.speed * 60.0 * delta_time.0;
// match *position {
// Position::Stopped { .. } => {
// // Check if we have a requested direction to start moving
// if let Some(requested_direction) = movable.requested_direction {
// if let Some(edge) = map.graph.find_edge_in_direction(position.current_node(), requested_direction) {
// if can_traverse(*entity_type, edge) {
// // Start moving in the requested direction
// let progress = if edge.distance > 0.0 {
// distance / edge.distance
// } else {
// // Zero-distance edge (tunnels) - immediately teleport
// tracing::debug!(
// "Entity entering tunnel from node {} to node {}",
// position.current_node(),
// edge.target
// );
// 1.0
// };
match *movement_state {
MovementState::Stopped => {
// Check if we have a requested direction to start moving
if let Some(requested_direction) = movable.requested_direction {
if let Some(edge) = map.graph.find_edge_in_direction(position.node, requested_direction) {
if can_traverse(*entity_type, edge) {
// Start moving in the requested direction
let progress = if edge.distance > 0.0 {
distance / edge.distance
} else {
// Zero-distance edge (tunnels) - immediately teleport
tracing::debug!("Entity entering tunnel from node {} to node {}", position.node, edge.target);
1.0
};
// *position = Position::Moving {
// from: position.current_node(),
// to: edge.target,
// remaining_distance: progress,
// };
// movable.current_direction = requested_direction;
// movable.requested_direction = None;
// }
// } else {
// errors.write(
// EntityError::InvalidMovement(format!(
// "No edge found in direction {:?} from node {}",
// requested_direction,
// position.current_node()
// ))
// .into(),
// );
// }
// }
// }
// Position::Moving {
// from,
// to,
// remaining_distance,
// } => {
// // Continue moving or handle node transitions
// let current_node = *from;
// if let Some(edge) = map.graph.find_edge(current_node, *to) {
// // Extract target node before mutable operations
// let target_node = *to;
position.edge_progress = Some(EdgeProgress {
target_node: edge.target,
progress,
});
movable.current_direction = requested_direction;
movable.requested_direction = None;
*movement_state = MovementState::Moving {
direction: requested_direction,
};
}
} else {
errors.write(
EntityError::InvalidMovement(format!(
"No edge found in direction {:?} from node {}",
requested_direction, position.node
))
.into(),
);
}
}
}
MovementState::Moving { direction } => {
// Continue moving or handle node transitions
let current_node = position.node;
if let Some(edge_progress) = &mut position.edge_progress {
// Extract target node before mutable operations
let target_node = edge_progress.target_node;
// // Get the current edge for distance calculation
// let edge = map.graph.find_edge(current_node, target_node);
// Get the current edge for distance calculation
let edge = map.graph.find_edge(current_node, target_node);
// if let Some(edge) = edge {
// // Update progress along the edge
// if edge.distance > 0.0 {
// *remaining_distance += distance / edge.distance;
// } else {
// // Zero-distance edge (tunnels) - immediately complete
// *remaining_distance = 1.0;
// }
if let Some(edge) = edge {
// Update progress along the edge
if edge.distance > 0.0 {
edge_progress.progress += distance / edge.distance;
} else {
// Zero-distance edge (tunnels) - immediately complete
edge_progress.progress = 1.0;
}
// if *remaining_distance >= 1.0 {
// // Reached the target node
// let overflow = if edge.distance > 0.0 {
// (*remaining_distance - 1.0) * edge.distance
// } else {
// // Zero-distance edge - use remaining distance for overflow
// distance
// };
// *position = Position::Stopped { node: target_node };
if edge_progress.progress >= 1.0 {
// Reached the target node
let overflow = if edge.distance > 0.0 {
(edge_progress.progress - 1.0) * edge.distance
} else {
// Zero-distance edge - use remaining distance for overflow
distance
};
position.node = target_node;
position.edge_progress = None;
// let mut continued_moving = false;
let mut continued_moving = false;
// // Try to use requested direction first
// if let Some(requested_direction) = movable.requested_direction {
// if let Some(next_edge) = map.graph.find_edge_in_direction(position.node, requested_direction) {
// if can_traverse(*entity_type, next_edge) {
// let next_progress = if next_edge.distance > 0.0 {
// overflow / next_edge.distance
// } else {
// // Zero-distance edge - immediately complete
// 1.0
// };
// Try to use requested direction first
if let Some(requested_direction) = movable.requested_direction {
if let Some(next_edge) = map.graph.find_edge_in_direction(position.node, requested_direction) {
if can_traverse(*entity_type, next_edge) {
let next_progress = if next_edge.distance > 0.0 {
overflow / next_edge.distance
} else {
// Zero-distance edge - immediately complete
1.0
};
// *position = Position::Moving {
// from: position.current_node(),
// to: next_edge.target,
// remaining_distance: next_progress,
// };
// movable.current_direction = requested_direction;
// movable.requested_direction = None;
// continued_moving = true;
// }
// }
// }
position.edge_progress = Some(EdgeProgress {
target_node: next_edge.target,
progress: next_progress,
});
movable.current_direction = requested_direction;
movable.requested_direction = None;
*movement_state = MovementState::Moving {
direction: requested_direction,
};
continued_moving = true;
}
}
}
// // If no requested direction or it failed, try to continue in current direction
// if !continued_moving {
// if let Some(next_edge) = map.graph.find_edge_in_direction(position.node, direction) {
// if can_traverse(*entity_type, next_edge) {
// let next_progress = if next_edge.distance > 0.0 {
// overflow / next_edge.distance
// } else {
// // Zero-distance edge - immediately complete
// 1.0
// };
// If no requested direction or it failed, try to continue in current direction
if !continued_moving {
if let Some(next_edge) = map.graph.find_edge_in_direction(position.node, direction) {
if can_traverse(*entity_type, next_edge) {
let next_progress = if next_edge.distance > 0.0 {
overflow / next_edge.distance
} else {
// Zero-distance edge - immediately complete
1.0
};
// *position = Position::Moving {
// from: position.current_node(),
// to: next_edge.target,
// remaining_distance: next_progress,
// };
// // Keep current direction and movement state
// continued_moving = true;
// }
// }
// }
position.edge_progress = Some(EdgeProgress {
target_node: next_edge.target,
progress: next_progress,
});
// Keep current direction and movement state
continued_moving = true;
}
}
}
// If we couldn't continue moving, stop
if !continued_moving {
*movement_state = MovementState::Stopped;
movable.requested_direction = None;
}
}
} else {
// Edge not found - this is an inconsistent state
errors.write(
EntityError::InvalidMovement(format!(
"Inconsistent state: Moving on non-existent edge from {} to {}",
current_node, target_node
))
.into(),
);
*movement_state = MovementState::Stopped;
position.edge_progress = None;
}
} else {
// Movement state says moving but no edge progress - this shouldn't happen
errors.write(EntityError::InvalidMovement("Entity in Moving state but no edge progress".to_string()).into());
*movement_state = MovementState::Stopped;
}
}
}
}
}
// // If we couldn't continue moving, stop
// if !continued_moving {
// *movement_state = MovementState::Stopped;
// movable.requested_direction = None;
// }
// }
// } else {
// // Edge not found - this is an inconsistent state
// errors.write(
// EntityError::InvalidMovement(format!(
// "Inconsistent state: Moving on non-existent edge from {} to {}",
// current_node, target_node
// ))
// .into(),
// );
// *movement_state = MovementState::Stopped;
// position.edge_progress = None;
// }
// } else {
// // Movement state says moving but no edge progress - this shouldn't happen
// errors.write(EntityError::InvalidMovement("Entity in Moving state but no edge progress".to_string()).into());
// *movement_state = MovementState::Stopped;
// }
// }
// }
// }
// }

143
src/systems/player.rs Normal file
View File

@@ -0,0 +1,143 @@
use bevy_ecs::{
event::{EventReader, EventWriter},
prelude::ResMut,
query::With,
system::{Query, Res},
};
use crate::{
entity::graph::Edge,
error::GameError,
events::{GameCommand, GameEvent},
map::builder::Map,
systems::{
components::{AudioState, DeltaTime, EntityType, GlobalState, PlayerControlled},
debug::DebugState,
movement::{BufferedDirection, Position, Velocity},
},
};
// Handles player input and control
pub fn player_control_system(
mut events: EventReader<GameEvent>,
mut state: ResMut<GlobalState>,
mut debug_state: ResMut<DebugState>,
mut audio_state: ResMut<AudioState>,
mut players: Query<&mut BufferedDirection, With<PlayerControlled>>,
mut errors: EventWriter<GameError>,
) {
// Get the player's movable component (ensuring there is only one player)
let mut buffered_direction = match players.single_mut() {
Ok(buffered_direction) => buffered_direction,
Err(e) => {
errors.write(GameError::InvalidState(format!(
"No/multiple entities queried for player system: {}",
e
)));
return;
}
};
// Handle events
for event in events.read() {
if let GameEvent::Command(command) = event {
match command {
GameCommand::MovePlayer(direction) => {
*buffered_direction = BufferedDirection::Some {
direction: *direction,
remaining_time: 0.25,
};
}
GameCommand::Exit => {
state.exit = true;
}
GameCommand::ToggleDebug => {
*debug_state = debug_state.next();
}
GameCommand::MuteAudio => {
audio_state.muted = !audio_state.muted;
tracing::info!("Audio {}", if audio_state.muted { "muted" } else { "unmuted" });
}
_ => {}
}
}
}
}
fn can_traverse(entity_type: EntityType, edge: Edge) -> bool {
let entity_flags = entity_type.traversal_flags();
edge.traversal_flags.contains(entity_flags)
}
pub fn player_movement_system(
map: Res<Map>,
delta_time: Res<DeltaTime>,
mut entities: Query<(&mut Position, &mut Velocity, &mut BufferedDirection), With<PlayerControlled>>,
// mut errors: EventWriter<GameError>,
) {
for (mut position, mut velocity, mut buffered_direction) in entities.iter_mut() {
// Decrement the buffered direction remaining time
if let BufferedDirection::Some {
direction,
remaining_time,
} = *buffered_direction
{
if remaining_time <= 0.0 {
*buffered_direction = BufferedDirection::None;
} else {
*buffered_direction = BufferedDirection::Some {
direction,
remaining_time: remaining_time - delta_time.0,
};
}
}
let mut distance = velocity.speed * 60.0 * delta_time.0;
loop {
match *position {
Position::Stopped { .. } => {
// If there is a buffered direction, travel it's edge first if available.
if let BufferedDirection::Some { direction, .. } = *buffered_direction {
// If there's no edge in that direction, ignore the buffered direction.
if let Some(edge) = map.graph.find_edge_in_direction(position.current_node(), direction) {
// If there is an edge in that direction (and it's traversable), start moving towards it and consume the buffered direction.
if can_traverse(EntityType::Player, edge) {
velocity.direction = edge.direction;
*position = Position::Moving {
from: position.current_node(),
to: edge.target,
remaining_distance: edge.distance,
};
*buffered_direction = BufferedDirection::None;
}
}
}
// If there is no buffered direction (or it's not yet valid), continue in the current direction.
if let Some(edge) = map.graph.find_edge_in_direction(position.current_node(), velocity.direction) {
if can_traverse(EntityType::Player, edge) {
velocity.direction = edge.direction;
*position = Position::Moving {
from: position.current_node(),
to: edge.target,
remaining_distance: edge.distance,
};
}
} else {
// No edge in our current direction either, erase the buffered direction and stop.
*buffered_direction = BufferedDirection::None;
break;
}
}
Position::Moving { .. } => {
if let Some(overflow) = position.tick(distance) {
distance = overflow;
} else {
break;
}
}
}
}
}
}

View File

@@ -3,14 +3,44 @@ use bevy_ecs::system::{IntoSystem, System};
use circular_buffer::CircularBuffer;
use micromap::Map;
use parking_lot::{Mutex, RwLock};
use smallvec::SmallVec;
use std::fmt::Display;
use std::time::Duration;
use strum::EnumCount;
use strum_macros::{EnumCount, IntoStaticStr};
use thousands::Separable;
use crate::systems::formatting;
/// The maximum number of systems that can be profiled. Must not be exceeded, or it will panic.
const MAX_SYSTEMS: usize = 13;
const MAX_SYSTEMS: usize = SystemId::COUNT;
/// The number of durations to keep in the circular buffer.
const TIMING_WINDOW_SIZE: usize = 30;
#[derive(EnumCount, IntoStaticStr, Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum SystemId {
Input,
PlayerControls,
Ghost,
Movement,
Audio,
Blinking,
DirectionalRender,
DirtyRender,
Render,
DebugRender,
Present,
Collision,
Item,
PlayerMovement,
}
impl Display for SystemId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", Into::<&'static str>::into(self).to_ascii_lowercase())
}
}
#[derive(Resource, Default, Debug)]
pub struct SystemTimings {
/// Map of system names to a queue of durations, using a circular buffer.
@@ -20,18 +50,18 @@ pub struct SystemTimings {
///
/// Also, we use a micromap::Map as the number of systems is generally quite small.
/// Just make sure to set the capacity appropriately, or it will panic.
pub timings: RwLock<Map<&'static str, Mutex<CircularBuffer<TIMING_WINDOW_SIZE, Duration>>, MAX_SYSTEMS>>,
pub timings: RwLock<Map<SystemId, Mutex<CircularBuffer<TIMING_WINDOW_SIZE, Duration>>, MAX_SYSTEMS>>,
}
impl SystemTimings {
pub fn add_timing(&self, name: &'static str, duration: Duration) {
pub fn add_timing(&self, id: SystemId, duration: Duration) {
// acquire a upgradable read lock
let mut timings = self.timings.upgradable_read();
// happy path, the name is already in the map (no need to mutate the hashmap)
if timings.contains_key(name) {
if timings.contains_key(&id) {
let queue = timings
.get(name)
.get(&id)
.expect("System name not found in map after contains_key check");
let mut queue = queue.lock();
@@ -41,16 +71,16 @@ impl SystemTimings {
// otherwise, acquire a write lock and insert a new queue
timings.with_upgraded(|timings| {
let queue = timings.entry(name).or_insert_with(|| Mutex::new(CircularBuffer::new()));
let queue = timings.entry(id).or_insert_with(|| Mutex::new(CircularBuffer::new()));
queue.lock().push_back(duration);
});
}
pub fn get_stats(&self) -> Map<&'static str, (Duration, Duration), MAX_SYSTEMS> {
pub fn get_stats(&self) -> Map<SystemId, (Duration, Duration), MAX_SYSTEMS> {
let timings = self.timings.read();
let mut stats = Map::new();
for (name, queue) in timings.iter() {
for (id, queue) in timings.iter() {
if queue.lock().is_empty() {
continue;
}
@@ -65,7 +95,7 @@ impl SystemTimings {
let std_dev = variance.sqrt();
stats.insert(
*name,
*id,
(
Duration::from_secs_f64(mean / 1000.0),
Duration::from_secs_f64(std_dev / 1000.0),
@@ -101,7 +131,7 @@ impl SystemTimings {
)
}
pub fn format_timing_display(&self) -> String {
pub fn format_timing_display(&self) -> SmallVec<[String; SystemId::COUNT]> {
let stats = self.get_stats();
let (total_avg, total_std) = self.get_total_stats();
@@ -126,11 +156,11 @@ impl SystemTimings {
}
// Use the formatting module to format the data
crate::systems::formatting::format_timing_display(timing_data).join("\n")
formatting::format_timing_display(timing_data)
}
}
pub fn profile<S, M>(name: &'static str, system: S) -> impl FnMut(&mut bevy_ecs::world::World)
pub fn profile<S, M>(id: SystemId, system: S) -> impl FnMut(&mut bevy_ecs::world::World)
where
S: IntoSystem<(), (), M> + 'static,
{
@@ -147,7 +177,7 @@ where
let duration = start.elapsed();
if let Some(timings) = world.get_resource::<SystemTimings>() {
timings.add_timing(name, duration);
timings.add_timing(id, duration);
}
}
}

View File

@@ -1,7 +1,7 @@
use crate::error::{GameError, TextureError};
use crate::map::builder::Map;
use crate::systems::components::{DeltaTime, DirectionalAnimated, RenderDirty, Renderable};
use crate::systems::movement::{Movable, MovementState, Position};
use crate::systems::movement::{Position, Velocity};
use crate::texture::sprite::SpriteAtlas;
use bevy_ecs::entity::Entity;
use bevy_ecs::event::EventWriter;
@@ -26,12 +26,12 @@ pub fn dirty_render_system(
/// This runs before the render system so it can update the sprite based on the current direction of travel, as well as whether the entity is moving.
pub fn directional_render_system(
dt: Res<DeltaTime>,
mut renderables: Query<(&MovementState, &Movable, &mut DirectionalAnimated, &mut Renderable)>,
mut renderables: Query<(&Position, &Velocity, &mut DirectionalAnimated, &mut Renderable)>,
mut errors: EventWriter<GameError>,
) {
for (movement_state, movable, mut texture, mut renderable) in renderables.iter_mut() {
let stopped = matches!(movement_state, MovementState::Stopped);
let current_direction = movable.current_direction;
for (position, velocity, mut texture, mut renderable) in renderables.iter_mut() {
let stopped = matches!(position, Position::Stopped { .. });
let current_direction = velocity.direction;
let texture = if stopped {
texture.stopped_textures[current_direction.as_usize()].as_mut()
@@ -96,7 +96,7 @@ pub fn render_system(
continue;
}
let pos = position.get_pixel_pos(&map.graph);
let pos = position.get_pixel_position(&map.graph);
match pos {
Ok(pos) => {
let dest = crate::helpers::centered_with_size(

View File

@@ -1,4 +1,4 @@
use pacman::systems::profiling::SystemTimings;
use pacman::systems::profiling::{SystemId, SystemTimings};
use std::time::Duration;
#[test]
@@ -6,12 +6,12 @@ fn test_timing_statistics() {
let timings = SystemTimings::default();
// Add some test data
timings.add_timing("test_system", Duration::from_millis(10));
timings.add_timing("test_system", Duration::from_millis(12));
timings.add_timing("test_system", Duration::from_millis(8));
timings.add_timing(SystemId::PlayerControls, Duration::from_millis(10));
timings.add_timing(SystemId::PlayerControls, Duration::from_millis(12));
timings.add_timing(SystemId::PlayerControls, Duration::from_millis(8));
let stats = timings.get_stats();
let (avg, std_dev) = stats.get("test_system").unwrap();
let (avg, std_dev) = stats.get(&SystemId::PlayerControls).unwrap();
// Average should be 10ms, standard deviation should be small
assert!((avg.as_millis() as f64 - 10.0).abs() < 1.0);