inv-sys

Latest Release Documentation

A robust and effective inventory system for games.

Features

Usage

```rust

[derive(Debug, Clone, PartialEq, Eq)]

pub enum Item { Apple, Banana, Mango, Peach, Orange }

/* * Implement the Stacksize trait for * your type that will act as your Item */ impl super::Stacksize for Item { fn getmaxstacksize(&self) -> usize { 3 } }

fn main() { let mut inv = Inv::::new(32);

// cant be placed, slot out of bounds asserteq!( inv.stackat( 666, ItemStack::new(Item::Peach, 1) ), Err(InvAccessErr::SlotOutOfBounds) );

// overflow, which is returned to you asserteq!( inv.stackat( 2, ItemStack::new(Item::Apple, 4) ), Ok(Err( StackErr::StackSizeOverflow( ItemStack::new(Item::Apple, 1) ) )) );

// stack Banana at pos 1 inv.stack_at( 1, ItemStack::new(Item::Banana, 1) ).ok();

// item cant be stacked, // item type does not match (Banana != Orange) asserteq!( inv.stackat( 1, ItemStack::new(Item::Orange, 1) ), Ok(Err( StackErr::ItemTypeDoesNotMatch( ItemStack::new(Item::Orange, 1) ) )) );

// auto stacking // this first fills slot 1 to be at the max of 3 // since slot 1 already had 1 Banana in it // the leftover will be placed in the first available slot, // which, in this case, is 0 assert!( inv.autostack( ItemStack::new(Item::Banana, 3) ).isok() );

// 1 Banana, 3 Bananas asserteq!( inv.getslot(0), Ok(&Slot::new(ItemStack::new(Item::Banana, 1))) ); asserteq!( inv.getslot(1), Ok(&Slot::new(ItemStack::new(Item::Banana, 3))) );

// you can take a stack out of its slot // first, we place 2 Mangos at slot 5 inv.stackat(5, ItemStack::new(Item::Mango, 1)).ok(); inv.autostack(ItemStack::new(Item::Mango, 1)).ok();

// now we just take the stack asserteq!( inv.takestack(5), Ok(ItemStack::new(Item::Mango, 2)) );

// slot 5 is empty now asserteq!( inv.takestack(5), Err(InvAccessErr::SlotEmpty) ); } ``` You can look at the unit tests for more examples.

Todo

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!