bevy-expedition-demo/src/inventory/item.rs
2ndbeam c46fa75e54 feat!: Item processing
- Added try_insert_item system
- Added rotated field on Item
- Implemented UI item drawing with scale and rotation

BREAKING CHANGE: Item.swap_size renamed to Item.rotate
    Item.clone_swapped renamed to Item.clone_rotated
2026-03-11 14:55:27 +03:00

51 lines
1.2 KiB
Rust

use std::mem::swap;
use bevy::prelude::*;
#[derive(Component, Clone, Debug)]
pub struct Item {
pub size: UVec2,
pub position: Option<UVec2>,
pub rotated: bool,
}
impl Item {
pub fn new(size: UVec2) -> Self {
Self { size, position: None, rotated: false }
}
pub fn new_positioned(size: UVec2, position: UVec2) -> Self {
Self { size, position: Some(position), rotated: false }
}
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()
}
/// Swap size.x with size.y
pub fn rotate(&mut self) {
swap(&mut self.size.x, &mut self.size.y);
self.rotated = !self.rotated;
}
/// Get clone of item with swapped size
pub fn clone_rotated(&self) -> Self {
let mut new = self.clone();
new.rotate();
new
}
}