game_inventory
A framework for generalizing inventory logic and abstracting it away from
item data in your specific game.
See more examples and specific documentation about this crate on docs.rs.
Basic usage
```rs
use gameinventory::traits::{IItem, IItemInstance, ISlot};
use gameinventory::samplestructs::{ItemInstance, Slot};
use gameinventory::helpers::addtoinventory;
// Define your item data however you like.
[derive(Debug)]
pub struct Item<'a> {
pub name: &'a str,
pub maxquantity: u16,
pub image: Optiontype: &'a str
}
// implement IItem for it so it can interact with the rest of the system.
impl<'a> IItem for Item<'a> {
fn stackable(&self) -> bool {
self.maxquantity > 1
}
fn maxquant(&self) -> u16 {
self.maxquantity
}
fn name(&self) -> &str {
self.name
}
}
// start using it in combination with everything else!
const CHEESE: Item = Item{name:"Cheese", maxquantity:100, image:None, itemtype:"Food"};
const CHEESEINST: Option = Some(ItemInstance{item:&CHEESE, quantity:32});
const SWORD: Item = Item{name:"Sword", maxquantity:0, image:None, itemtype:"Weapon"};
const SWORDINST: Option = Some(ItemInstance{item:&SWORD, quantity:0});
let mut inventory = vec![
Slot::new(CHEESEINST),
Slot::new(None),
Slot::new(None),
Slot::new(CHEESEINST)
];
addtoinventory(&mut inventory, SWORDINST.unwrap());
asserteq!(inventory[0].iteminstance().unwrap().item().name(), CHEESE.name());
asserteq!(inventory[0].iteminstance().unwrap().quant(), CHEESEINST.unwrap().quant());
asserteq!(inventory[1].iteminstance().unwrap().item().name(), SWORD.name());
assert!(inventory[2].iteminstance().isnone());
asserteq!(inventory[3].iteminstance().unwrap().item().name(), CHEESE.name());
asserteq!(inventory[3].iteminstance().unwrap().quant(), CHEESEINST.unwrap().quant());
```