veloren/common/src/comp/inventory/mod.rs

133 lines
3.3 KiB
Rust
Raw Normal View History

pub mod item;
2019-07-25 12:46:54 +00:00
// Reexports
pub use item::{Debug, Item, Tool};
2019-07-25 12:46:54 +00:00
2019-08-01 15:07:42 +00:00
use specs::{Component, HashMapStorage, NullStorage};
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Inventory {
pub slots: Vec<Option<Item>>,
}
impl Inventory {
pub fn slots(&self) -> &[Option<Item>] {
&self.slots
}
2019-07-25 22:52:28 +00:00
pub fn len(&self) -> usize {
self.slots.len()
}
/// Adds a new item to the first empty slot of the inventory. Returns the item again if no free
/// slot was found.
pub fn push(&mut self, item: Item) -> Option<Item> {
2019-07-25 22:52:28 +00:00
match self.slots.iter_mut().find(|slot| slot.is_none()) {
Some(slot) => {
*slot = Some(item);
None
2019-07-25 22:52:28 +00:00
}
None => Some(item),
}
}
/// Replaces an item in a specific slot of the inventory. Returns the old item or the same item again if that slot
/// was not found.
pub fn insert(&mut self, cell: usize, item: Item) -> Result<Option<Item>, Item> {
match self.slots.get_mut(cell) {
Some(slot) => {
let old = slot.take();
*slot = Some(item);
Ok(old)
}
None => Err(item),
2019-07-25 22:52:28 +00:00
}
}
2019-09-25 22:53:43 +00:00
pub fn is_full(&self) -> bool {
self.slots.iter().all(|slot| slot.is_some())
}
2019-08-30 20:42:43 +00:00
/// Get content of a slot
pub fn get(&self, cell: usize) -> Option<&Item> {
self.slots.get(cell).and_then(Option::as_ref)
}
2019-08-30 20:42:43 +00:00
/// Swap the items inside of two slots
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-08-30 20:42:43 +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-07-25 14:02:05 +00:00
impl Default for Inventory {
fn default() -> Inventory {
2019-08-26 18:05:13 +00:00
let mut inventory = Inventory {
slots: vec![None; 24],
2019-07-25 22:52:28 +00:00
};
2019-08-29 17:42:25 +00:00
inventory.push(Item::Debug(Debug::Boost));
2019-10-11 04:30:34 +00:00
inventory.push(Item::Debug(Debug::Possess));
2019-09-16 15:58:40 +00:00
inventory.push(Item::Tool {
kind: Tool::Bow,
power: 10,
2019-10-09 19:28:05 +00:00
stamina: 0,
strength: 0,
dexterity: 0,
intelligence: 0,
2019-09-16 15:58:40 +00:00
});
inventory.push(Item::Tool {
2019-10-09 19:28:05 +00:00
kind: Tool::Dagger,
2019-08-26 18:05:13 +00:00
power: 10,
2019-10-09 19:28:05 +00:00
stamina: 0,
strength: 0,
dexterity: 0,
intelligence: 0,
2019-08-26 18:05:13 +00:00
});
inventory.push(Item::Tool {
2019-08-26 18:05:13 +00:00
kind: Tool::Sword,
power: 10,
2019-10-09 19:28:05 +00:00
stamina: 0,
strength: 0,
dexterity: 0,
intelligence: 0,
2019-08-26 18:05:13 +00:00
});
inventory.push(Item::Tool {
2019-08-26 18:05:13 +00:00
kind: Tool::Axe,
power: 10,
2019-10-09 19:28:05 +00:00
stamina: 0,
strength: 0,
dexterity: 0,
intelligence: 0,
2019-08-26 18:05:13 +00:00
});
inventory.push(Item::Tool {
2019-08-26 18:05:13 +00:00
kind: Tool::Hammer,
power: 10,
2019-10-09 19:28:05 +00:00
stamina: 0,
strength: 0,
dexterity: 0,
intelligence: 0,
2019-08-26 18:05:13 +00:00
});
2019-07-25 22:52:28 +00:00
2019-08-26 18:05:13 +00:00
inventory
2019-07-25 14:02:05 +00:00
}
}
impl Component for Inventory {
2019-07-31 09:30:46 +00:00
type Storage = HashMapStorage<Self>;
}
// ForceUpdate
#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize)]
pub struct InventoryUpdate;
impl Component for InventoryUpdate {
type Storage = NullStorage<Self>;
}