feat: Added Inventory and Item components

- Moved input tests into nested module
- Added inventory tests
This commit is contained in:
Alexey 2026-03-06 16:41:17 +03:00
commit dab3134f15
4 changed files with 260 additions and 72 deletions

35
src/inventory/item.rs Normal file
View file

@ -0,0 +1,35 @@
use bevy::prelude::*;
#[derive(Component, Clone)]
pub struct Item {
pub size: UVec2,
pub position: Option<UVec2>,
}
impl Item {
pub fn new(size: UVec2) -> Self {
Self { size, position: None }
}
pub fn new_positioned(size: UVec2, position: UVec2) -> Self {
Self { size, position: Some(position) }
}
pub fn rect(&self) -> Option<URect> {
let Some(position) = self.position else {
return None;
};
Some(URect::from_corners(position, position + self.size))
}
pub fn overlaps(&self, other_size: UVec2, other_position: UVec2) -> bool {
let Some(rect) = self.rect() else {
return false;
};
let other_rect = URect::from_corners(other_position, other_position + other_size);
!rect.intersect(other_rect).is_empty()
}
}

39
src/inventory/mod.rs Normal file
View file

@ -0,0 +1,39 @@
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: &Children,
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
}
}