generated from 2ndbeam/bevy-template
56 lines
1.5 KiB
Rust
56 lines
1.5 KiB
Rust
use bevy::prelude::*;
|
|
|
|
pub mod item;
|
|
|
|
#[derive(Component)]
|
|
pub struct Inventory {
|
|
pub size: UVec2,
|
|
}
|
|
|
|
impl Inventory {
|
|
pub fn new(size: UVec2) -> Self {
|
|
Self { size }
|
|
}
|
|
|
|
pub fn can_fit(
|
|
&self,
|
|
item_query: Query<&item::Item>,
|
|
contained_items: &[Entity],
|
|
queried_size: UVec2,
|
|
queried_position: UVec2,
|
|
) -> bool {
|
|
let item_corner = queried_size + queried_position;
|
|
if item_corner.x > self.size.x || item_corner.y > self.size.y {
|
|
return false;
|
|
}
|
|
|
|
for entity in contained_items {
|
|
let Ok(item) = item_query.get(*entity) else {
|
|
warn!("Could not query inventory child ({entity}), probably not item?");
|
|
continue;
|
|
};
|
|
|
|
if item.overlaps(queried_size, queried_position) {
|
|
return false;
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
}
|