mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-13 12:12:25 -06:00
Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| da3c8e8284 | |||
| 9c0711a54c |
@@ -146,159 +146,3 @@ impl Position {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
|
||||||
// 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
|
|
||||||
// };
|
|
||||||
|
|
||||||
// *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;
|
|
||||||
|
|
||||||
// // 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 *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 };
|
|
||||||
|
|
||||||
// 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
|
|
||||||
// };
|
|
||||||
|
|
||||||
// *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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // 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;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ pub fn player_control_system(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ fn get_formatted_output() -> impl IntoIterator<Item = String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_formatting_alignment() {
|
fn test_complex_formatting_alignment() {
|
||||||
let mut colon_positions = vec![];
|
let mut colon_positions = vec![];
|
||||||
let mut first_decimal_positions = vec![];
|
let mut first_decimal_positions = vec![];
|
||||||
let mut second_decimal_positions = vec![];
|
let mut second_decimal_positions = vec![];
|
||||||
@@ -93,3 +93,80 @@ fn test_formatting_alignment() {
|
|||||||
second_unit_positions
|
second_unit_positions
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_timing_display_basic() {
|
||||||
|
let timing_data = vec![
|
||||||
|
("render".to_string(), Duration::from_micros(1500), Duration::from_micros(200)),
|
||||||
|
("input".to_string(), Duration::from_micros(300), Duration::from_micros(50)),
|
||||||
|
("physics".to_string(), Duration::from_nanos(750), Duration::from_nanos(100)),
|
||||||
|
];
|
||||||
|
|
||||||
|
let formatted = format_timing_display(timing_data);
|
||||||
|
|
||||||
|
// Should have 3 lines (one for each system)
|
||||||
|
assert_eq!(formatted.len(), 3);
|
||||||
|
|
||||||
|
// Each line should contain the system name
|
||||||
|
assert!(formatted.iter().any(|line| line.contains("render")));
|
||||||
|
assert!(formatted.iter().any(|line| line.contains("input")));
|
||||||
|
assert!(formatted.iter().any(|line| line.contains("physics")));
|
||||||
|
|
||||||
|
// Each line should contain timing information with proper units
|
||||||
|
for line in formatted.iter() {
|
||||||
|
assert!(line.contains(":"), "Line should contain colon separator: {}", line);
|
||||||
|
assert!(line.contains("±"), "Line should contain ± symbol: {}", line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_timing_display_empty() {
|
||||||
|
let timing_data = vec![];
|
||||||
|
let formatted = format_timing_display(timing_data);
|
||||||
|
assert!(formatted.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_timing_display_units() {
|
||||||
|
let timing_data = vec![
|
||||||
|
("seconds".to_string(), Duration::from_secs(2), Duration::from_millis(100)),
|
||||||
|
("millis".to_string(), Duration::from_millis(15), Duration::from_micros(200)),
|
||||||
|
("micros".to_string(), Duration::from_micros(500), Duration::from_nanos(50)),
|
||||||
|
("nanos".to_string(), Duration::from_nanos(250), Duration::from_nanos(25)),
|
||||||
|
];
|
||||||
|
|
||||||
|
let formatted = format_timing_display(timing_data);
|
||||||
|
|
||||||
|
// Check that appropriate units are used
|
||||||
|
let all_lines = formatted.join(" ");
|
||||||
|
assert!(all_lines.contains("s"), "Should contain seconds unit");
|
||||||
|
assert!(all_lines.contains("ms"), "Should contain milliseconds unit");
|
||||||
|
assert!(all_lines.contains("µs"), "Should contain microseconds unit");
|
||||||
|
assert!(all_lines.contains("ns"), "Should contain nanoseconds unit");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_format_timing_display_alignment() {
|
||||||
|
let timing_data = vec![
|
||||||
|
("short".to_string(), Duration::from_micros(100), Duration::from_micros(10)),
|
||||||
|
(
|
||||||
|
"very_long_name".to_string(),
|
||||||
|
Duration::from_micros(200),
|
||||||
|
Duration::from_micros(20),
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
let formatted = format_timing_display(timing_data);
|
||||||
|
|
||||||
|
// Find colon positions - they should be aligned
|
||||||
|
let colon_positions: Vec<usize> = formatted.iter().map(|line| line.find(':').unwrap_or(0)).collect();
|
||||||
|
|
||||||
|
// All colons should be at the same position (aligned)
|
||||||
|
if colon_positions.len() > 1 {
|
||||||
|
let first_pos = colon_positions[0];
|
||||||
|
assert!(
|
||||||
|
colon_positions.iter().all(|&pos| pos == first_pos),
|
||||||
|
"Colons should be aligned at the same position"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
100
tests/player.rs
Normal file
100
tests/player.rs
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
use pacman::map::direction::Direction;
|
||||||
|
use pacman::map::graph::{Edge, TraversalFlags};
|
||||||
|
use pacman::systems::components::EntityType;
|
||||||
|
use pacman::systems::player::can_traverse;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_player_on_all_edges() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 1,
|
||||||
|
distance: 10.0,
|
||||||
|
direction: Direction::Up,
|
||||||
|
traversal_flags: TraversalFlags::ALL,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(can_traverse(EntityType::Player, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_player_on_pacman_only_edges() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 1,
|
||||||
|
distance: 10.0,
|
||||||
|
direction: Direction::Right,
|
||||||
|
traversal_flags: TraversalFlags::PACMAN,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(can_traverse(EntityType::Player, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_player_blocked_on_ghost_only_edges() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 1,
|
||||||
|
distance: 10.0,
|
||||||
|
direction: Direction::Left,
|
||||||
|
traversal_flags: TraversalFlags::GHOST,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(!can_traverse(EntityType::Player, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_ghost_on_all_edges() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 2,
|
||||||
|
distance: 15.0,
|
||||||
|
direction: Direction::Down,
|
||||||
|
traversal_flags: TraversalFlags::ALL,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(can_traverse(EntityType::Ghost, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_ghost_on_ghost_only_edges() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 2,
|
||||||
|
distance: 15.0,
|
||||||
|
direction: Direction::Up,
|
||||||
|
traversal_flags: TraversalFlags::GHOST,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(can_traverse(EntityType::Ghost, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_ghost_blocked_on_pacman_only_edges() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 2,
|
||||||
|
distance: 15.0,
|
||||||
|
direction: Direction::Right,
|
||||||
|
traversal_flags: TraversalFlags::PACMAN,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert!(!can_traverse(EntityType::Ghost, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_can_traverse_static_entities_flags() {
|
||||||
|
let edge = Edge {
|
||||||
|
target: 3,
|
||||||
|
distance: 8.0,
|
||||||
|
direction: Direction::Left,
|
||||||
|
traversal_flags: TraversalFlags::ALL,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Static entities have empty traversal flags but can still "traverse"
|
||||||
|
// in the sense that empty flags are contained in any flag set
|
||||||
|
// This is the expected behavior since empty ⊆ any set
|
||||||
|
assert!(can_traverse(EntityType::Pellet, edge));
|
||||||
|
assert!(can_traverse(EntityType::PowerPellet, edge));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_entity_type_traversal_flags() {
|
||||||
|
assert_eq!(EntityType::Player.traversal_flags(), TraversalFlags::PACMAN);
|
||||||
|
assert_eq!(EntityType::Ghost.traversal_flags(), TraversalFlags::GHOST);
|
||||||
|
assert_eq!(EntityType::Pellet.traversal_flags(), TraversalFlags::empty());
|
||||||
|
assert_eq!(EntityType::PowerPellet.traversal_flags(), TraversalFlags::empty());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user