use basalt_types::Slot;
use crate::components::Component;
#[derive(Debug, Clone)]
pub struct CraftingGrid {
pub slots: [Slot; 9],
pub grid_size: u8,
pub output: Slot,
}
impl CraftingGrid {
pub fn empty() -> Self {
Self {
slots: std::array::from_fn(|_| Slot::empty()),
grid_size: 2,
output: Slot::empty(),
}
}
pub fn clear(&mut self) {
for slot in &mut self.slots {
*slot = Slot::empty();
}
self.output = Slot::empty();
}
}
impl Component for CraftingGrid {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_returns_grid_size_two() {
let grid = CraftingGrid::empty();
assert_eq!(grid.grid_size, 2);
}
#[test]
fn empty_has_all_empty_slots() {
let grid = CraftingGrid::empty();
for slot in &grid.slots {
assert!(slot.is_empty());
}
assert!(grid.output.is_empty());
}
#[test]
fn clear_resets_slots_but_keeps_grid_size() {
let mut grid = CraftingGrid::empty();
grid.grid_size = 3;
grid.slots[0] = Slot::new(1, 1);
grid.slots[4] = Slot::new(2, 5);
grid.output = Slot::new(3, 1);
grid.clear();
assert_eq!(grid.grid_size, 3);
for slot in &grid.slots {
assert!(slot.is_empty());
}
assert!(grid.output.is_empty());
}
}