//Re-Exports pub mod item; // Reexports pub use self::item::{Debug, Item, Tool}; use specs::{Component, HashMapStorage, NullStorage}; use specs_idvs::IDVStorage; #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct Inventory { pub slots: Vec>, } impl Inventory { pub fn slots(&self) -> &[Option] { &self.slots } pub fn len(&self) -> usize { self.slots.len() } pub fn push(&mut self, item: Item) -> Option { match self.slots.iter_mut().find(|slot| slot.is_none()) { Some(slot) => { let old = slot.take(); *slot = Some(item); old } None => None, } } pub fn insert(&mut self, cell: usize, item: Option) -> Option { match self.slots.get_mut(cell) { Some(slot) => { let old = slot.take(); *slot = item; old } None => None, } } // Get info about an item slot pub fn get(&self, cell: usize) -> Option { self.slots.get(cell).cloned().flatten() } // Insert an item to a slot if its empty pub fn swap(&mut self, cell: usize, item: Item) -> Option { //TODO: Check if a slot is empty first. self.slots.get_mut(cell).and_then(|cell| cell.replace(item)) } pub fn swap_slots(&mut self, a: usize, b: usize) { if a.max(b) < self.slots.len() { self.slots.swap(a, b); } } // Remove an item from the slot pub fn remove(&mut self, cell: usize) -> Option { self.slots.get_mut(cell).and_then(|item| item.take()) } } impl Default for Inventory { fn default() -> Inventory { let mut inventory = Inventory { slots: vec![None; 24], }; inventory.push(Item::Debug(Debug::Teleport)); inventory.push(Item::Tool { kind: Tool::Daggers, power: 10, }); inventory.push(Item::Tool { kind: Tool::Sword, power: 10, }); inventory.push(Item::Tool { kind: Tool::Axe, power: 10, }); inventory.push(Item::Tool { kind: Tool::Hammer, power: 10, }); for _ in 0..10 { inventory.push(Item::default()); } inventory } } impl Component for Inventory { type Storage = HashMapStorage; } // ForceUpdate #[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)] pub struct InventoryUpdate; impl Component for InventoryUpdate { type Storage = NullStorage; }