Compare commits

..

3 Commits

Author SHA1 Message Date
d72b6eec06 test: add item testing 2025-08-18 09:32:35 -05:00
ae42f6ead0 chore: solve clippy warnings 2025-08-18 00:06:47 -05:00
471b118efd test: add tests for item systems & movement types 2025-08-18 00:04:07 -05:00
6 changed files with 505 additions and 25 deletions

View File

@@ -28,7 +28,6 @@ pub trait CommonPlatform {
fn get_canvas_size(&self) -> Option<(u32, u32)>; fn get_canvas_size(&self) -> Option<(u32, u32)>;
/// Loads raw asset data using the appropriate platform-specific method. /// Loads raw asset data using the appropriate platform-specific method.
fn get_asset_bytes(&self, asset: Asset) -> Result<Cow<'static, [u8]>, AssetError>; fn get_asset_bytes(&self, asset: Asset) -> Result<Cow<'static, [u8]>, AssetError>;
} }

View File

@@ -70,6 +70,17 @@ impl EntityType {
_ => TraversalFlags::empty(), // Static entities don't traverse _ => 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. /// A component for entities that have a sprite, with a layer for ordering.

View File

@@ -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( pub fn item_system(
mut commands: Commands, mut commands: Commands,
mut collision_events: EventReader<GameEvent>, mut collision_events: EventReader<GameEvent>,
@@ -29,20 +40,17 @@ pub fn item_system(
// Get the item type and update score // Get the item type and update score
if let Ok((item_ent, entity_type)) = item_query.get(item_entity) { if let Ok((item_ent, entity_type)) = item_query.get(item_entity) {
match entity_type { if let Some(score_value) = entity_type.score_value() {
EntityType::Pellet => { score.0 += score_value;
score.0 += 10;
// 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);
} }
} }
} }

View File

@@ -4,7 +4,7 @@ use pacman::map::direction::Direction;
#[test] #[test]
fn test_game_command_variants() { fn test_game_command_variants() {
// Test that all GameCommand variants can be created // Test that all GameCommand variants can be created
let commands = vec![ let commands = [
GameCommand::Exit, GameCommand::Exit,
GameCommand::MovePlayer(Direction::Up), GameCommand::MovePlayer(Direction::Up),
GameCommand::MovePlayer(Direction::Down), 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] #[test]
fn test_game_event_variants() { fn test_game_event_variants() {
let command_event = GameEvent::Command(GameCommand::Exit); let command_event = GameEvent::Command(GameCommand::Exit);

278
tests/item.rs Normal file
View 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
View 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");
}
}