use std::mem::swap; use bevy::prelude::*; #[derive(Component, Clone, Debug)] pub struct Item { pub size: UVec2, pub position: Option, 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 { 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 } }