mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 05:15:49 -06:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d72b6eec06 | |||
| ae42f6ead0 | |||
| 471b118efd | |||
| 13a9c165f7 |
@@ -28,7 +28,6 @@ pub trait CommonPlatform {
|
||||
fn get_canvas_size(&self) -> Option<(u32, u32)>;
|
||||
|
||||
/// Loads raw asset data using the appropriate platform-specific method.
|
||||
|
||||
fn get_asset_bytes(&self, asset: Asset) -> Result<Cow<'static, [u8]>, AssetError>;
|
||||
}
|
||||
|
||||
|
||||
@@ -70,6 +70,17 @@ impl EntityType {
|
||||
_ => 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.
|
||||
|
||||
@@ -8,6 +8,17 @@ use crate::{
|
||||
},
|
||||
};
|
||||
|
||||
/// Determines if a collision between two entity types should be handled by the item system.
|
||||
///
|
||||
/// Returns `true` if one entity is a player and the other is a collectible item.
|
||||
#[allow(dead_code)]
|
||||
pub fn is_valid_item_collision(entity1: EntityType, entity2: EntityType) -> bool {
|
||||
match (entity1, entity2) {
|
||||
(EntityType::Player, entity) | (entity, EntityType::Player) => entity.is_collectible(),
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn item_system(
|
||||
mut commands: Commands,
|
||||
mut collision_events: EventReader<GameEvent>,
|
||||
@@ -29,20 +40,17 @@ pub fn item_system(
|
||||
|
||||
// Get the item type and update score
|
||||
if let Ok((item_ent, entity_type)) = item_query.get(item_entity) {
|
||||
match entity_type {
|
||||
EntityType::Pellet => {
|
||||
score.0 += 10;
|
||||
if let Some(score_value) = entity_type.score_value() {
|
||||
score.0 += score_value;
|
||||
|
||||
// Remove the collected item
|
||||
commands.entity(item_ent).despawn();
|
||||
|
||||
// Trigger audio if appropriate
|
||||
if entity_type.is_collectible() {
|
||||
events.write(AudioEvent::PlayEat);
|
||||
}
|
||||
EntityType::PowerPellet => {
|
||||
score.0 += 50;
|
||||
}
|
||||
_ => continue,
|
||||
}
|
||||
|
||||
// Remove the collected item
|
||||
commands.entity(item_ent).despawn();
|
||||
|
||||
events.write(AudioEvent::PlayEat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use pacman::map::direction::Direction;
|
||||
#[test]
|
||||
fn test_game_command_variants() {
|
||||
// Test that all GameCommand variants can be created
|
||||
let commands = vec![
|
||||
let commands = [
|
||||
GameCommand::Exit,
|
||||
GameCommand::MovePlayer(Direction::Up),
|
||||
GameCommand::MovePlayer(Direction::Down),
|
||||
@@ -38,17 +38,6 @@ fn test_game_command_equality() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_command_copy_clone() {
|
||||
let original = GameCommand::MovePlayer(Direction::Up);
|
||||
let copied = original;
|
||||
let cloned = original.clone();
|
||||
|
||||
assert_eq!(original, copied);
|
||||
assert_eq!(original, cloned);
|
||||
assert_eq!(copied, cloned);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_game_event_variants() {
|
||||
let command_event = GameEvent::Command(GameCommand::Exit);
|
||||
|
||||
278
tests/item.rs
Normal file
278
tests/item.rs
Normal file
@@ -0,0 +1,278 @@
|
||||
use bevy_ecs::{event::Events, prelude::*, system::RunSystemOnce, world::World};
|
||||
|
||||
use pacman::{
|
||||
events::GameEvent,
|
||||
map::builder::Map,
|
||||
systems::{
|
||||
audio::AudioEvent,
|
||||
components::{AudioState, EntityType, ItemCollider, PacmanCollider, ScoreResource},
|
||||
item::{is_valid_item_collision, item_system},
|
||||
movement::Position,
|
||||
},
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_calculate_score_for_item() {
|
||||
assert!(EntityType::Pellet.score_value() < EntityType::PowerPellet.score_value());
|
||||
assert!(EntityType::Pellet.score_value().is_some());
|
||||
assert!(EntityType::PowerPellet.score_value().is_some());
|
||||
assert!(EntityType::Pellet.score_value().unwrap() < EntityType::PowerPellet.score_value().unwrap());
|
||||
assert!(EntityType::Player.score_value().is_none());
|
||||
assert!(EntityType::Ghost.score_value().is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_collectible_item() {
|
||||
// Collectible
|
||||
assert!(EntityType::Pellet.is_collectible());
|
||||
assert!(EntityType::PowerPellet.is_collectible());
|
||||
|
||||
// Non-collectible
|
||||
assert!(!EntityType::Player.is_collectible());
|
||||
assert!(!EntityType::Ghost.is_collectible());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_valid_item_collision() {
|
||||
// Player-item collisions should be valid
|
||||
assert!(is_valid_item_collision(EntityType::Player, EntityType::Pellet));
|
||||
assert!(is_valid_item_collision(EntityType::Player, EntityType::PowerPellet));
|
||||
assert!(is_valid_item_collision(EntityType::Pellet, EntityType::Player));
|
||||
assert!(is_valid_item_collision(EntityType::PowerPellet, EntityType::Player));
|
||||
|
||||
// Non-player-item collisions should be invalid
|
||||
assert!(!is_valid_item_collision(EntityType::Player, EntityType::Ghost));
|
||||
assert!(!is_valid_item_collision(EntityType::Ghost, EntityType::Pellet));
|
||||
assert!(!is_valid_item_collision(EntityType::Pellet, EntityType::PowerPellet));
|
||||
assert!(!is_valid_item_collision(EntityType::Player, EntityType::Player));
|
||||
}
|
||||
|
||||
fn create_test_world() -> World {
|
||||
let mut world = World::new();
|
||||
|
||||
// Add required resources
|
||||
world.insert_resource(ScoreResource(0));
|
||||
world.insert_resource(AudioState::default());
|
||||
world.insert_resource(Events::<GameEvent>::default());
|
||||
world.insert_resource(Events::<AudioEvent>::default());
|
||||
world.insert_resource(Events::<pacman::error::GameError>::default());
|
||||
|
||||
// Add a minimal test map
|
||||
world.insert_resource(create_test_map());
|
||||
|
||||
world
|
||||
}
|
||||
|
||||
fn create_test_map() -> Map {
|
||||
use pacman::constants::RAW_BOARD;
|
||||
Map::new(RAW_BOARD).expect("Failed to create test map")
|
||||
}
|
||||
|
||||
fn spawn_test_pacman(world: &mut World) -> Entity {
|
||||
world
|
||||
.spawn((Position::Stopped { node: 0 }, EntityType::Player, PacmanCollider))
|
||||
.id()
|
||||
}
|
||||
|
||||
fn spawn_test_item(world: &mut World, item_type: EntityType) -> Entity {
|
||||
world.spawn((Position::Stopped { node: 1 }, item_type, ItemCollider)).id()
|
||||
}
|
||||
|
||||
fn send_collision_event(world: &mut World, entity1: Entity, entity2: Entity) {
|
||||
let mut events = world.resource_mut::<Events<GameEvent>>();
|
||||
events.send(GameEvent::Collision(entity1, entity2));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_pellet_collection() {
|
||||
let mut world = create_test_world();
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
let pellet = spawn_test_item(&mut world, EntityType::Pellet);
|
||||
|
||||
// Send collision event
|
||||
send_collision_event(&mut world, pacman, pellet);
|
||||
|
||||
// Run the item system
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Check that score was updated
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, 10);
|
||||
|
||||
// Check that the pellet was despawned (query should return empty)
|
||||
let item_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::Pellet))
|
||||
.count();
|
||||
assert_eq!(item_count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_power_pellet_collection() {
|
||||
let mut world = create_test_world();
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
let power_pellet = spawn_test_item(&mut world, EntityType::PowerPellet);
|
||||
|
||||
send_collision_event(&mut world, pacman, power_pellet);
|
||||
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Check that score was updated with power pellet value
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, 50);
|
||||
|
||||
// Check that the power pellet was despawned (query should return empty)
|
||||
let item_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::PowerPellet))
|
||||
.count();
|
||||
assert_eq!(item_count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_multiple_collections() {
|
||||
let mut world = create_test_world();
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
let pellet1 = spawn_test_item(&mut world, EntityType::Pellet);
|
||||
let pellet2 = spawn_test_item(&mut world, EntityType::Pellet);
|
||||
let power_pellet = spawn_test_item(&mut world, EntityType::PowerPellet);
|
||||
|
||||
// Send multiple collision events
|
||||
send_collision_event(&mut world, pacman, pellet1);
|
||||
send_collision_event(&mut world, pacman, pellet2);
|
||||
send_collision_event(&mut world, pacman, power_pellet);
|
||||
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Check final score: 2 pellets (20) + 1 power pellet (50) = 70
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, 70);
|
||||
|
||||
// Check that all items were despawned
|
||||
let pellet_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::Pellet))
|
||||
.count();
|
||||
let power_pellet_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::PowerPellet))
|
||||
.count();
|
||||
assert_eq!(pellet_count, 0);
|
||||
assert_eq!(power_pellet_count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_ignores_non_item_collisions() {
|
||||
let mut world = create_test_world();
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
|
||||
// Create a ghost entity (not an item)
|
||||
let ghost = world.spawn((Position::Stopped { node: 2 }, EntityType::Ghost)).id();
|
||||
|
||||
// Initial score
|
||||
let initial_score = world.resource::<ScoreResource>().0;
|
||||
|
||||
// Send collision event between pacman and ghost
|
||||
send_collision_event(&mut world, pacman, ghost);
|
||||
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Score should remain unchanged
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, initial_score);
|
||||
|
||||
// Ghost should still exist (not despawned)
|
||||
let ghost_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::Ghost))
|
||||
.count();
|
||||
assert_eq!(ghost_count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_wrong_collision_order() {
|
||||
let mut world = create_test_world();
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
let pellet = spawn_test_item(&mut world, EntityType::Pellet);
|
||||
|
||||
// Send collision event with entities in reverse order
|
||||
send_collision_event(&mut world, pellet, pacman);
|
||||
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Should still work correctly
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, 10);
|
||||
let pellet_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::Pellet))
|
||||
.count();
|
||||
assert_eq!(pellet_count, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_no_collision_events() {
|
||||
let mut world = create_test_world();
|
||||
let _pacman = spawn_test_pacman(&mut world);
|
||||
let _pellet = spawn_test_item(&mut world, EntityType::Pellet);
|
||||
|
||||
let initial_score = world.resource::<ScoreResource>().0;
|
||||
|
||||
// Run system without any collision events
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Nothing should change
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, initial_score);
|
||||
let pellet_count = world
|
||||
.query::<&EntityType>()
|
||||
.iter(&world)
|
||||
.filter(|&entity_type| matches!(entity_type, EntityType::Pellet))
|
||||
.count();
|
||||
assert_eq!(pellet_count, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_collision_with_missing_entity() {
|
||||
let mut world = create_test_world();
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
|
||||
// Create a fake entity ID that doesn't exist
|
||||
let fake_entity = Entity::from_raw(999);
|
||||
|
||||
send_collision_event(&mut world, pacman, fake_entity);
|
||||
|
||||
// System should handle gracefully and not crash
|
||||
world
|
||||
.run_system_once(item_system)
|
||||
.expect("System should handle missing entities gracefully");
|
||||
|
||||
// Score should remain unchanged
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_item_system_preserves_existing_score() {
|
||||
let mut world = create_test_world();
|
||||
|
||||
// Set initial score
|
||||
world.insert_resource(ScoreResource(100));
|
||||
|
||||
let pacman = spawn_test_pacman(&mut world);
|
||||
let pellet = spawn_test_item(&mut world, EntityType::Pellet);
|
||||
|
||||
send_collision_event(&mut world, pacman, pellet);
|
||||
|
||||
world.run_system_once(item_system).expect("System should run successfully");
|
||||
|
||||
// Score should be initial + pellet value
|
||||
let score = world.resource::<ScoreResource>();
|
||||
assert_eq!(score.0, 110);
|
||||
}
|
||||
195
tests/movement.rs
Normal file
195
tests/movement.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
use glam::Vec2;
|
||||
use pacman::map::direction::Direction;
|
||||
use pacman::map::graph::{Graph, Node};
|
||||
use pacman::systems::movement::{BufferedDirection, Position, Velocity};
|
||||
|
||||
fn create_test_graph() -> Graph {
|
||||
let mut graph = Graph::new();
|
||||
|
||||
// Add a few test nodes
|
||||
let node0 = graph.add_node(Node {
|
||||
position: Vec2::new(0.0, 0.0),
|
||||
});
|
||||
let node1 = graph.add_node(Node {
|
||||
position: Vec2::new(16.0, 0.0),
|
||||
});
|
||||
let node2 = graph.add_node(Node {
|
||||
position: Vec2::new(0.0, 16.0),
|
||||
});
|
||||
|
||||
// Connect them
|
||||
graph.connect(node0, node1, false, None, Direction::Right).unwrap();
|
||||
graph.connect(node0, node2, false, None, Direction::Down).unwrap();
|
||||
|
||||
graph
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_is_at_node() {
|
||||
let stopped_pos = Position::Stopped { node: 0 };
|
||||
let moving_pos = Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 8.0,
|
||||
};
|
||||
|
||||
assert!(stopped_pos.is_at_node());
|
||||
assert!(!moving_pos.is_at_node());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_current_node() {
|
||||
let stopped_pos = Position::Stopped { node: 5 };
|
||||
let moving_pos = Position::Moving {
|
||||
from: 3,
|
||||
to: 7,
|
||||
remaining_distance: 12.0,
|
||||
};
|
||||
|
||||
assert_eq!(stopped_pos.current_node(), 5);
|
||||
assert_eq!(moving_pos.current_node(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_tick_no_movement_when_stopped() {
|
||||
let mut pos = Position::Stopped { node: 0 };
|
||||
let result = pos.tick(5.0);
|
||||
|
||||
assert!(result.is_none());
|
||||
assert_eq!(pos, Position::Stopped { node: 0 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_tick_no_movement_when_zero_distance() {
|
||||
let mut pos = Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 10.0,
|
||||
};
|
||||
let result = pos.tick(0.0);
|
||||
|
||||
assert!(result.is_none());
|
||||
assert_eq!(
|
||||
pos,
|
||||
Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 10.0,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_tick_partial_movement() {
|
||||
let mut pos = Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 10.0,
|
||||
};
|
||||
let result = pos.tick(3.0);
|
||||
|
||||
assert!(result.is_none());
|
||||
assert_eq!(
|
||||
pos,
|
||||
Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 7.0,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_tick_exact_arrival() {
|
||||
let mut pos = Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 5.0,
|
||||
};
|
||||
let result = pos.tick(5.0);
|
||||
|
||||
assert!(result.is_none());
|
||||
assert_eq!(pos, Position::Stopped { node: 1 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_tick_overshoot_with_overflow() {
|
||||
let mut pos = Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 3.0,
|
||||
};
|
||||
let result = pos.tick(8.0);
|
||||
|
||||
assert_eq!(result, Some(5.0));
|
||||
assert_eq!(pos, Position::Stopped { node: 1 });
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_get_pixel_position_stopped() {
|
||||
let graph = create_test_graph();
|
||||
let pos = Position::Stopped { node: 0 };
|
||||
|
||||
let pixel_pos = pos.get_pixel_position(&graph).unwrap();
|
||||
let expected = Vec2::new(
|
||||
0.0 + pacman::constants::BOARD_PIXEL_OFFSET.x as f32,
|
||||
0.0 + pacman::constants::BOARD_PIXEL_OFFSET.y as f32,
|
||||
);
|
||||
|
||||
assert_eq!(pixel_pos, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_position_get_pixel_position_moving() {
|
||||
let graph = create_test_graph();
|
||||
let pos = Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 8.0, // Halfway through a 16-unit edge
|
||||
};
|
||||
|
||||
let pixel_pos = pos.get_pixel_position(&graph).unwrap();
|
||||
// Should be halfway between (0,0) and (16,0), so at (8,0) plus offset
|
||||
let expected = Vec2::new(
|
||||
8.0 + pacman::constants::BOARD_PIXEL_OFFSET.x as f32,
|
||||
0.0 + pacman::constants::BOARD_PIXEL_OFFSET.y as f32,
|
||||
);
|
||||
|
||||
assert_eq!(pixel_pos, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_velocity_basic_properties() {
|
||||
let velocity = Velocity {
|
||||
speed: 2.5,
|
||||
direction: Direction::Up,
|
||||
};
|
||||
|
||||
assert_eq!(velocity.speed, 2.5);
|
||||
assert_eq!(velocity.direction, Direction::Up);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_buffered_direction_none() {
|
||||
let buffered = BufferedDirection::None;
|
||||
assert_eq!(buffered, BufferedDirection::None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_buffered_direction_some() {
|
||||
let buffered = BufferedDirection::Some {
|
||||
direction: Direction::Left,
|
||||
remaining_time: 0.5,
|
||||
};
|
||||
|
||||
if let BufferedDirection::Some {
|
||||
direction,
|
||||
remaining_time,
|
||||
} = buffered
|
||||
{
|
||||
assert_eq!(direction, Direction::Left);
|
||||
assert_eq!(remaining_time, 0.5);
|
||||
} else {
|
||||
panic!("Expected BufferedDirection::Some");
|
||||
}
|
||||
}
|
||||
484
tests/player.rs
484
tests/player.rs
@@ -1,7 +1,64 @@
|
||||
use pacman::map::direction::Direction;
|
||||
use pacman::map::graph::{Edge, TraversalFlags};
|
||||
use pacman::systems::components::EntityType;
|
||||
use pacman::systems::player::can_traverse;
|
||||
use bevy_ecs::{event::Events, prelude::*, system::RunSystemOnce, world::World};
|
||||
|
||||
use pacman::{
|
||||
events::{GameCommand, GameEvent},
|
||||
map::{
|
||||
builder::Map,
|
||||
direction::Direction,
|
||||
graph::{Edge, TraversalFlags},
|
||||
},
|
||||
systems::{
|
||||
components::{AudioState, DeltaTime, EntityType, GlobalState, PlayerControlled},
|
||||
debug::DebugState,
|
||||
movement::{BufferedDirection, Position, Velocity},
|
||||
player::{can_traverse, player_control_system, player_movement_system},
|
||||
},
|
||||
};
|
||||
|
||||
// Test helper functions for ECS setup
|
||||
fn create_test_world() -> World {
|
||||
let mut world = World::new();
|
||||
|
||||
// Add resources
|
||||
world.insert_resource(GlobalState { exit: false });
|
||||
world.insert_resource(DebugState::Off);
|
||||
world.insert_resource(AudioState::default());
|
||||
world.insert_resource(DeltaTime(1.0 / 60.0)); // 60 FPS
|
||||
world.insert_resource(Events::<GameEvent>::default());
|
||||
world.insert_resource(Events::<pacman::error::GameError>::default());
|
||||
|
||||
// Create a simple test map with nodes and edges
|
||||
let test_map = create_test_map();
|
||||
world.insert_resource(test_map);
|
||||
|
||||
world
|
||||
}
|
||||
|
||||
fn create_test_map() -> Map {
|
||||
// Use the actual RAW_BOARD from constants.rs
|
||||
use pacman::constants::RAW_BOARD;
|
||||
Map::new(RAW_BOARD).expect("Failed to create test map")
|
||||
}
|
||||
|
||||
fn spawn_test_player(world: &mut World) -> Entity {
|
||||
world
|
||||
.spawn((
|
||||
PlayerControlled,
|
||||
Position::Stopped { node: 0 },
|
||||
Velocity {
|
||||
speed: 1.0,
|
||||
direction: Direction::Right,
|
||||
},
|
||||
BufferedDirection::None,
|
||||
EntityType::Player,
|
||||
))
|
||||
.id()
|
||||
}
|
||||
|
||||
fn send_game_event(world: &mut World, command: GameCommand) {
|
||||
let mut events = world.resource_mut::<Events<GameEvent>>();
|
||||
events.send(GameEvent::Command(command));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_can_traverse_player_on_all_edges() {
|
||||
@@ -98,3 +155,422 @@ fn test_entity_type_traversal_flags() {
|
||||
assert_eq!(EntityType::Pellet.traversal_flags(), TraversalFlags::empty());
|
||||
assert_eq!(EntityType::PowerPellet.traversal_flags(), TraversalFlags::empty());
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// ECS System Tests
|
||||
// ============================================================================
|
||||
|
||||
#[test]
|
||||
fn test_player_control_system_move_command() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send move command
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Up));
|
||||
|
||||
// Run the system
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that buffered direction was updated
|
||||
let mut query = world.query::<&BufferedDirection>();
|
||||
let buffered_direction = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *buffered_direction {
|
||||
BufferedDirection::Some {
|
||||
direction,
|
||||
remaining_time,
|
||||
} => {
|
||||
assert_eq!(direction, Direction::Up);
|
||||
assert_eq!(remaining_time, 0.25);
|
||||
}
|
||||
BufferedDirection::None => panic!("Expected buffered direction to be set"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_control_system_exit_command() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send exit command
|
||||
send_game_event(&mut world, GameCommand::Exit);
|
||||
|
||||
// Run the system
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that exit flag was set
|
||||
let state = world.resource::<GlobalState>();
|
||||
assert!(state.exit);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_control_system_toggle_debug() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send toggle debug command
|
||||
send_game_event(&mut world, GameCommand::ToggleDebug);
|
||||
|
||||
// Run the system
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that debug state changed
|
||||
let debug_state = world.resource::<DebugState>();
|
||||
assert_eq!(*debug_state, DebugState::Graph);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_control_system_mute_audio() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send mute audio command
|
||||
send_game_event(&mut world, GameCommand::MuteAudio);
|
||||
|
||||
// Run the system
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that audio was muted
|
||||
let audio_state = world.resource::<AudioState>();
|
||||
assert!(audio_state.muted);
|
||||
|
||||
// Send mute audio command again to unmute - need fresh events
|
||||
world.resource_mut::<Events<GameEvent>>().clear(); // Clear previous events
|
||||
send_game_event(&mut world, GameCommand::MuteAudio);
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that audio was unmuted
|
||||
let audio_state = world.resource::<AudioState>();
|
||||
assert!(!audio_state.muted, "Audio should be unmuted after second toggle");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_control_system_no_player_entity() {
|
||||
let mut world = create_test_world();
|
||||
// Don't spawn a player entity
|
||||
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Up));
|
||||
|
||||
// Run the system - should write an error
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that an error was written (we can't easily check Events without manual management,
|
||||
// so for this test we just verify the system ran without panicking)
|
||||
// In a real implementation, you might expose error checking through the ECS world
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_movement_system_buffered_direction_expires() {
|
||||
let mut world = create_test_world();
|
||||
let player = spawn_test_player(&mut world);
|
||||
|
||||
// Set a buffered direction with short time
|
||||
world.entity_mut(player).insert(BufferedDirection::Some {
|
||||
direction: Direction::Up,
|
||||
remaining_time: 0.01, // Very short time
|
||||
});
|
||||
|
||||
// Set delta time to expire the buffered direction
|
||||
world.insert_resource(DeltaTime(0.02));
|
||||
|
||||
// Run the system
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that buffered direction expired or remaining time decreased significantly
|
||||
let mut query = world.query::<&BufferedDirection>();
|
||||
let buffered_direction = query.single(&world).expect("Player should exist");
|
||||
match *buffered_direction {
|
||||
BufferedDirection::None => {} // Expected - fully expired
|
||||
BufferedDirection::Some { remaining_time, .. } => {
|
||||
assert!(
|
||||
remaining_time <= 0.0,
|
||||
"Buffered direction should be expired or have non-positive time"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_movement_system_start_moving_from_stopped() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Player starts at node 0, facing right (towards node 1)
|
||||
// Should start moving when system runs
|
||||
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that player started moving
|
||||
let mut query = world.query::<&Position>();
|
||||
let position = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *position {
|
||||
Position::Moving { from, .. } => {
|
||||
assert_eq!(from, 0, "Player should start from node 0");
|
||||
// Don't assert exact target node since the real map has different connectivity
|
||||
}
|
||||
Position::Stopped { .. } => {} // May stay stopped if no valid edge in current direction
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_movement_system_buffered_direction_change() {
|
||||
let mut world = create_test_world();
|
||||
let player = spawn_test_player(&mut world);
|
||||
|
||||
// Set a buffered direction to go down (towards node 2)
|
||||
world.entity_mut(player).insert(BufferedDirection::Some {
|
||||
direction: Direction::Down,
|
||||
remaining_time: 1.0,
|
||||
});
|
||||
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that player started moving down instead of right
|
||||
let mut query = world.query::<(&Position, &Velocity, &BufferedDirection)>();
|
||||
let (position, _velocity, _buffered_direction) = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *position {
|
||||
Position::Moving { from, to, .. } => {
|
||||
assert_eq!(from, 0);
|
||||
assert_eq!(to, 2); // Should be moving to node 2 (down)
|
||||
}
|
||||
Position::Stopped { .. } => panic!("Player should have started moving"),
|
||||
}
|
||||
|
||||
// Check if the movement actually happened based on the real map connectivity
|
||||
// The buffered direction might not be consumed if there's no valid edge in that direction
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_movement_system_no_valid_edge() {
|
||||
let mut world = create_test_world();
|
||||
let player = spawn_test_player(&mut world);
|
||||
|
||||
// Set velocity to direction with no edge
|
||||
world.entity_mut(player).insert(Velocity {
|
||||
speed: 1.0,
|
||||
direction: Direction::Up, // No edge up from node 0
|
||||
});
|
||||
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Player should remain stopped
|
||||
let mut query = world.query::<&Position>();
|
||||
let position = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *position {
|
||||
Position::Stopped { node } => assert_eq!(node, 0),
|
||||
Position::Moving { .. } => panic!("Player shouldn't be able to move without valid edge"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_movement_system_continue_moving() {
|
||||
let mut world = create_test_world();
|
||||
let player = spawn_test_player(&mut world);
|
||||
|
||||
// Set player to already be moving
|
||||
world.entity_mut(player).insert(Position::Moving {
|
||||
from: 0,
|
||||
to: 1,
|
||||
remaining_distance: 50.0,
|
||||
});
|
||||
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that player continued moving and distance decreased
|
||||
let mut query = world.query::<&Position>();
|
||||
let position = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *position {
|
||||
Position::Moving { remaining_distance, .. } => {
|
||||
assert!(remaining_distance < 50.0); // Should have moved
|
||||
}
|
||||
Position::Stopped { .. } => {
|
||||
// If player reached destination, that's also valid
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Integration Tests
|
||||
// ============================================================================
|
||||
|
||||
#[test]
|
||||
fn test_full_player_input_to_movement_flow() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send move command
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Down));
|
||||
|
||||
// Run control system to process input
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Run movement system to execute movement
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check final state - player should be moving down
|
||||
let mut query = world.query::<(&Position, &Velocity, &BufferedDirection)>();
|
||||
let (position, _velocity, _buffered_direction) = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *position {
|
||||
Position::Moving { from, to, .. } => {
|
||||
assert_eq!(from, 0);
|
||||
assert_eq!(to, 2); // Moving to node 2 (down)
|
||||
}
|
||||
Position::Stopped { .. } => panic!("Player should be moving"),
|
||||
}
|
||||
|
||||
// Check that player moved in the buffered direction if possible
|
||||
// In the real map, the buffered direction may not be consumable if there's no valid edge
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_buffered_direction_timing() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send move command
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Up));
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Run movement system multiple times with small delta times
|
||||
world.insert_resource(DeltaTime(0.1)); // 0.1 seconds
|
||||
|
||||
// First run - buffered direction should still be active
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
let mut query = world.query::<&BufferedDirection>();
|
||||
let buffered_direction = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *buffered_direction {
|
||||
BufferedDirection::Some { remaining_time, .. } => {
|
||||
assert!(remaining_time > 0.0);
|
||||
assert!(remaining_time < 0.25);
|
||||
}
|
||||
BufferedDirection::None => panic!("Buffered direction should still be active"),
|
||||
}
|
||||
|
||||
// Run again to fully expire the buffered direction
|
||||
world.insert_resource(DeltaTime(0.2)); // Total 0.3 seconds, should expire
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
let buffered_direction = query.single(&world).expect("Player should exist");
|
||||
assert_eq!(*buffered_direction, BufferedDirection::None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_multiple_rapid_direction_changes() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Send multiple rapid direction changes
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Up));
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Down));
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Left));
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Only the last direction should be buffered
|
||||
let mut query = world.query::<&BufferedDirection>();
|
||||
let buffered_direction = query.single(&world).expect("Player should exist");
|
||||
|
||||
match *buffered_direction {
|
||||
BufferedDirection::Some { direction, .. } => {
|
||||
assert_eq!(direction, Direction::Left);
|
||||
}
|
||||
BufferedDirection::None => panic!("Expected buffered direction"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_player_state_persistence_across_systems() {
|
||||
let mut world = create_test_world();
|
||||
let _player = spawn_test_player(&mut world);
|
||||
|
||||
// Test that multiple commands can be processed - but need to handle events properly
|
||||
// Clear any existing events first
|
||||
world.resource_mut::<Events<GameEvent>>().clear();
|
||||
|
||||
// Toggle debug mode
|
||||
send_game_event(&mut world, GameCommand::ToggleDebug);
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
let debug_state_after_toggle = *world.resource::<DebugState>();
|
||||
|
||||
// Clear events and mute audio
|
||||
world.resource_mut::<Events<GameEvent>>().clear();
|
||||
send_game_event(&mut world, GameCommand::MuteAudio);
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
let audio_muted_after_toggle = world.resource::<AudioState>().muted;
|
||||
|
||||
// Clear events and move player
|
||||
world.resource_mut::<Events<GameEvent>>().clear();
|
||||
send_game_event(&mut world, GameCommand::MovePlayer(Direction::Down));
|
||||
world
|
||||
.run_system_once(player_control_system)
|
||||
.expect("System should run successfully");
|
||||
world
|
||||
.run_system_once(player_movement_system)
|
||||
.expect("System should run successfully");
|
||||
|
||||
// Check that all state changes persisted
|
||||
// Variables already captured above during individual tests
|
||||
let mut query = world.query::<&Position>();
|
||||
let position = *query.single(&world).expect("Player should exist");
|
||||
|
||||
// Check that the state changes persisted individually
|
||||
assert_eq!(debug_state_after_toggle, DebugState::Graph, "Debug state should have toggled");
|
||||
assert!(audio_muted_after_toggle, "Audio should be muted");
|
||||
|
||||
// Player position depends on actual map connectivity
|
||||
match position {
|
||||
Position::Moving { .. } => {} // Good - player is moving
|
||||
Position::Stopped { .. } => {} // Also ok - might not have valid edge in that direction
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user