inv-sys
A robust and effective inventory system for games.
Features
- simple but robust API
- automatic stacking functionality
- taking stacks
- finding slots
- iterator
- max stacksize via trait
- sorting
- and more!
Usage
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Item {
Apple,
Banana,
Mango,
Peach,
Orange
}
impl Stacksize for Item {
fn get_max_stacksize(&self) -> usize {
3
}
}
fn main() {
let mut inv = Inv::<Item>::new(32);
assert_eq!(
inv.stack_at(
666, ItemStack::new(Item::Peach, 1)
),
Err(InvAccessErr::SlotOutOfBounds)
);
assert_eq!(
inv.stack_at(
2, ItemStack::new(Item::Apple, 4)
),
Ok(Err(
StackErr::StackSizeOverflow(
ItemStack::new(Item::Apple, 1)
)
))
);
inv.stack_at(
1, ItemStack::new(Item::Banana, 1)
).ok();
assert_eq!(
inv.stack_at(
1, ItemStack::new(Item::Orange, 1)
),
Ok(Err(
StackErr::ItemTypeDoesNotMatch(
ItemStack::new(Item::Orange, 1)
)
))
);
assert!(
inv.auto_stack(
ItemStack::new(Item::Banana, 3)
).is_ok()
);
assert_eq!(
inv.get_slot(0),
Ok(&Slot::new(ItemStack::new(Item::Banana, 1)))
);
assert_eq!(
inv.get_slot(1),
Ok(&Slot::new(ItemStack::new(Item::Banana, 3)))
);
inv.stack_at(5, ItemStack::new(Item::Mango, 1)).ok();
inv.auto_stack(ItemStack::new(Item::Mango, 1)).ok();
assert_eq!(
inv.take_stack(5),
Ok(ItemStack::new(Item::Mango, 2))
);
assert_eq!(
inv.take_stack(5),
Err(InvAccessErr::SlotEmpty)
);
}
You can look at the unit tests for more examples.
Todo
- sorting
- any requests? please just submit an issue, thanks!
Contributions
Feel free to open an issue/PR explaining possible improvements or changes
Help
Also, please do not hesitate and open an issue when needed. I am happy to help!