generated from 2ndbeam/bevy-template
feat: Implemented Inventory::can_move
- Changed Inventory::can_fit signature - Added can_move_item test
This commit is contained in:
parent
dab3134f15
commit
fee774dddd
2 changed files with 45 additions and 1 deletions
|
|
@ -15,7 +15,7 @@ impl Inventory {
|
||||||
pub fn can_fit(
|
pub fn can_fit(
|
||||||
&self,
|
&self,
|
||||||
item_query: Query<&item::Item>,
|
item_query: Query<&item::Item>,
|
||||||
contained_items: &Children,
|
contained_items: &[Entity],
|
||||||
queried_size: UVec2,
|
queried_size: UVec2,
|
||||||
queried_position: UVec2,
|
queried_position: UVec2,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
|
|
@ -36,4 +36,21 @@ impl Inventory {
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn can_move(
|
||||||
|
&self,
|
||||||
|
item_query: Query<&item::Item>,
|
||||||
|
contained_items: &[Entity],
|
||||||
|
queried_item: Entity,
|
||||||
|
queried_position: UVec2,
|
||||||
|
) -> bool {
|
||||||
|
let Ok(item::Item {size, ..}) = item_query.get(queried_item) else {
|
||||||
|
error!("Could not query inventory child ({queried_item}), probably not item?");
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
let children = contained_items.iter()
|
||||||
|
.filter_map(|e| if e.ne(&&queried_item) { Some(*e) } else { None })
|
||||||
|
.collect::<Vec<Entity>>();
|
||||||
|
self.can_fit(item_query, children.as_slice(), *size, queried_position)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
27
src/tests.rs
27
src/tests.rs
|
|
@ -115,6 +115,9 @@ mod inventory {
|
||||||
#[derive(Resource)]
|
#[derive(Resource)]
|
||||||
struct Items(Vec<Item>, usize);
|
struct Items(Vec<Item>, usize);
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
struct MovableItem(UVec2);
|
||||||
|
|
||||||
fn insert_item(
|
fn insert_item(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
mut items: ResMut<Items>,
|
mut items: ResMut<Items>,
|
||||||
|
|
@ -153,6 +156,16 @@ mod inventory {
|
||||||
items.1 += 1;
|
items.1 += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn try_to_move(
|
||||||
|
item_query: Query<&Item>,
|
||||||
|
inventory_query: Query<(&Inventory, &Children)>,
|
||||||
|
movable_item: Query<(Entity, &MovableItem)>,
|
||||||
|
) {
|
||||||
|
let (inventory, children) = inventory_query.single().unwrap();
|
||||||
|
let (movable_item, MovableItem(query_pos)) = movable_item.single().unwrap();
|
||||||
|
assert!(inventory.can_move(item_query, children, movable_item, *query_pos));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn everything_fits() {
|
fn everything_fits() {
|
||||||
let mut world = World::new();
|
let mut world = World::new();
|
||||||
|
|
@ -186,4 +199,18 @@ mod inventory {
|
||||||
world.run_system(system).expect("Error on running system");
|
world.run_system(system).expect("Error on running system");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn can_move_item() {
|
||||||
|
let mut world = World::new();
|
||||||
|
|
||||||
|
let system = world.register_system(try_to_move);
|
||||||
|
|
||||||
|
world.spawn(inventory())
|
||||||
|
.with_child((item_a(), MovableItem(UVec2::new(3, 2))))
|
||||||
|
.with_child(item_b())
|
||||||
|
.with_child(item_c());
|
||||||
|
|
||||||
|
world.run_system(system).expect("Error on running system");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue