2019-05-18 16:46:14 +00:00
|
|
|
//Re-Exports
|
|
|
|
pub mod item;
|
|
|
|
|
2019-07-25 12:46:54 +00:00
|
|
|
// Reexports
|
|
|
|
pub use self::item::Item;
|
|
|
|
|
2019-08-01 15:07:42 +00:00
|
|
|
use specs::{Component, HashMapStorage, NullStorage};
|
2019-07-25 12:46:54 +00:00
|
|
|
use specs_idvs::IDVStorage;
|
2019-05-18 16:46:14 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
|
|
pub struct Inventory {
|
|
|
|
pub slots: Vec<Option<Item>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inventory {
|
2019-07-25 17:41:06 +00:00
|
|
|
pub fn slots(&self) -> &[Option<Item>] {
|
|
|
|
&self.slots
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:52:28 +00:00
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.slots.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&mut self, item: Item) -> Option<Item> {
|
|
|
|
match self.slots.iter_mut().find(|slot| slot.is_none()) {
|
|
|
|
Some(slot) => {
|
|
|
|
*slot = Some(item);
|
|
|
|
None
|
|
|
|
}
|
|
|
|
None => Some(item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:46:14 +00:00
|
|
|
// Get info about an item slot
|
|
|
|
pub fn get(&self, cell: usize) -> Option<Item> {
|
|
|
|
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<Item> {
|
|
|
|
//TODO: Check if a slot is empty first.
|
|
|
|
self.slots.get_mut(cell).and_then(|cell| cell.replace(item))
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:52:28 +00:00
|
|
|
pub fn swap_slots(&mut self, a: usize, b: usize) {
|
|
|
|
if a.max(b) < self.slots.len() {
|
|
|
|
self.slots.swap(a, b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:46:14 +00:00
|
|
|
// Remove an item from the slot
|
2019-07-25 12:46:54 +00:00
|
|
|
pub fn remove(&mut self, cell: usize) -> Option<Item> {
|
|
|
|
self.slots.get_mut(cell).and_then(|item| item.take())
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 14:02:05 +00:00
|
|
|
impl Default for Inventory {
|
|
|
|
fn default() -> Inventory {
|
2019-07-25 22:52:28 +00:00
|
|
|
let mut this = Inventory {
|
2019-08-01 08:13:34 +00:00
|
|
|
slots: vec![None; 24],
|
2019-07-25 22:52:28 +00:00
|
|
|
};
|
|
|
|
|
2019-08-01 08:13:34 +00:00
|
|
|
for _ in 0..18 {
|
|
|
|
this.insert(Item::default());
|
|
|
|
}
|
2019-07-25 22:52:28 +00:00
|
|
|
|
|
|
|
this
|
2019-07-25 14:02:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-18 16:46:14 +00:00
|
|
|
impl Component for Inventory {
|
2019-07-31 09:30:46 +00:00
|
|
|
type Storage = HashMapStorage<Self>;
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|
2019-07-25 14:48:27 +00:00
|
|
|
|
|
|
|
// ForceUpdate
|
|
|
|
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
|
|
|
|
pub struct InventoryUpdate;
|
|
|
|
|
|
|
|
impl Component for InventoryUpdate {
|
|
|
|
type Storage = NullStorage<Self>;
|
|
|
|
}
|