use bevy::prelude::*; pub type NoonmywaInventory = Inventory; #[derive(Component, Reflect)] pub enum ItemType { // FIXME give it more than just a name Tile(String), Item(String), } pub struct InventoryItem { pub name: String, pub entity: Entity, } // BELOW: COMMON INVENTORY LOGIC #[derive(Component)] pub struct Inventory { pub amount_of_slots: u8, pub items: Vec, active_slot: u8, } // TODO decide whether "item" entities will have a weight or can use up more // than one capacity - what about stacks of items? // FIXME return something useful in the error case, maybe incorporate anyhow for error // handling - you know you want it impl Inventory { pub fn new(amount_of_slots: u8) -> Self { Self { amount_of_slots, items: Vec::with_capacity(amount_of_slots.into()), active_slot: 0, } } pub fn content(&self) -> Vec<(u8, Option<&Item>)> { let mut contents = Vec::with_capacity(self.amount_of_slots.into()); for slot in 0..self.amount_of_slots { let item: Option<&Item> = self.items.get(slot as usize); contents.push((slot, item)); } contents } pub fn active_slot(&self) -> u8 { self.active_slot } pub fn offer(&mut self, item: Item) -> Result<(), &str> { if self.items.len() >= self.amount_of_slots.into() { return Err("Slots exhausted"); } self.items.push(item); Ok(()) } pub fn set_active_slot(&mut self, slot: u8) { self.active_slot = slot; } pub fn get_active_ref(&self) -> (Option<&Item>, u8) { (self.items.get(self.active_slot as usize), self.active_slot) } }