2019-07-31 09:37:06 +00:00
|
|
|
use specs::{Component, HashMapStorage};
|
2019-07-29 19:54:48 +00:00
|
|
|
use specs_idvs::IDVStorage;
|
2019-05-18 16:46:14 +00:00
|
|
|
|
|
|
|
//Re-Exports
|
|
|
|
pub mod item;
|
|
|
|
|
|
|
|
use item::Item;
|
|
|
|
use std::mem::swap;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
|
|
|
pub struct Inventory {
|
|
|
|
pub slots: Vec<Option<Item>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inventory {
|
|
|
|
pub fn new() -> Inventory {
|
|
|
|
Inventory {
|
|
|
|
slots: vec![None; 24],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove an item from the slot
|
|
|
|
pub fn remove(&mut self, cell: usize, item: Item) -> Option<Item> {
|
|
|
|
let mut tmp_item = Some(item);
|
|
|
|
|
|
|
|
if let Some(old_item) = self.slots.get_mut(cell) {
|
|
|
|
swap(old_item, &mut tmp_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp_item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for Inventory {
|
2019-07-31 09:30:46 +00:00
|
|
|
type Storage = HashMapStorage<Self>;
|
2019-05-18 16:46:14 +00:00
|
|
|
}
|