Generic inventory system built in pure rust.

Overview

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

use game_inventory::traits::{IItem, IItemInstance, ISlot};
use game_inventory::sample_structs::{ItemInstance, Slot};
use game_inventory::helpers::add_to_inventory;
// Define your item data however you like.
#[derive(Debug)]
pub struct Item<'a> {
    pub name: &'a str,
    pub max_quantity: u16,
    pub image: Option<Vec<(u8,u8,u8,u8)>>,
    pub item_type: &'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.max_quantity > 1
    }
    fn max_quant(&self) -> u16 {
        self.max_quantity
    }
    fn name(&self) -> &str {
        self.name
    }
}
// start using it in combination with everything else!
const CHEESE: Item = Item{name:"Cheese", max_quantity:100, image:None, item_type:"Food"};
const CHEESE_INST: Option<ItemInstance> = Some(ItemInstance{item:&CHEESE, quantity:32});
const SWORD: Item = Item{name:"Sword", max_quantity:0, image:None, item_type:"Weapon"};
const SWORD_INST: Option<ItemInstance> = Some(ItemInstance{item:&SWORD, quantity:0});
let mut inventory = vec![
    Slot::new(CHEESE_INST),
    Slot::new(None),
    Slot::new(None),
    Slot::new(CHEESE_INST)
];
add_to_inventory(&mut inventory, SWORD_INST.unwrap());
assert_eq!(inventory[0].item_instance().unwrap().item().name(), CHEESE.name());
assert_eq!(inventory[0].item_instance().unwrap().quant(), CHEESE_INST.unwrap().quant());
assert_eq!(inventory[1].item_instance().unwrap().item().name(), SWORD.name());
assert!(inventory[2].item_instance().is_none());
assert_eq!(inventory[3].item_instance().unwrap().item().name(), CHEESE.name());
assert_eq!(inventory[3].item_instance().unwrap().quant(), CHEESE_INST.unwrap().quant());
You might also like...
This is a lightweight audio-video player built in Rust using FFmpeg libraries. It demonstrates the usage of FFmpeg with Rust to play back video files.

FFmpeg Rust Video Player This is a lightweight audio-video player built in Rust using FFmpeg libraries. It demonstrates the usage of FFmpeg with Rust

Simple daemon built with Rust to track metrics.

Marvin - Metrics Tracker What I cannot create, I do not understand. — Richard Feynman Simple daemon built with Rust to track metrics. The goal is run

Public aircraft & flightroute api Built in Rust for Docker, using PostgreSQL & Redis

api.adsbdb.com public aircraft & flightroute api Built in Rust for Docker, using PostgreSQL & Redis See typescript branch for original typescript vers

Charted's email service built in Rust that can be connected via gRPC

email-service is a small microservice to help transfer emails towards other people without trying to implement it in different languages. This is used in charted-server for member invitations, passwordless authentication, and more.

A traditional web forum built in Rust with modern technology to be fast, secure, scalable, and stable.

Volksforo A traditional web forum built in Rust with modern technology to be fast, secure, scalable, and stable. Stack Rust actix-web askama ScyllaDB

x86-64 Malware Crypter built in Rust for Windows with Anti-VM, powered by memexec

Rust Crypter x86-64 Malware Crypter built in Rust for Windows with Anti-VM, powered by memexec Usage Put your Portable Executable in /crypt/ and renam

 Gecko is a high-level, general-purpose programming language built on top of the LLVM project.
Gecko is a high-level, general-purpose programming language built on top of the LLVM project.

Gecko is a high-level, general-purpose programming language built on top of the LLVM project. Gecko Technology & principles Gecko is a general-purpose

Nyah is a programming language runtime built for high performance and comes with a scripting language.

🐱 Nyah ( Unfinished ) Nyah is a programming language runtime built for high performance and comes with a scripting language. 🎖️ Status Nyah is not c

Streaming I/O for Linux built on DMA Buffers

dmastorage High-performance I/O for Linux based on DMA Buffers ALPHA QUALITY SOFTWARE Warning DmaStorage is still just a concept and shouldn't be used

Comments
  • 0.2.0

    0.2.0

    Intended updates:

    • inventory_contains_item does not need to have optional item_instance input
    • add_to_inventory does not need to have optional item_instance input
    • add quant_in_inventory
    • add empty_quant_in_inventory
    • add remove_from_inventory
    • Move as many tests as possible to be doctests
    • Refactor slot management functions to return a result instead of swapping the items for better user customization.
    • Remove IDebugItem
    • Add basic usage example to the readme. Closes #7 Closes #3 Closes #6 Closes #4
    opened by Lubba-64 3
  • Add more examples to the documentation.

    Add more examples to the documentation.

    There absolutely should be more examples for this crate in the documentation, or we should just move a bunch of the tests to docstrings. my only issue is that the tests can be kind of verbose because of all of the specific checks that have to be done, so I'm not super sure what the best way to approach this is.

    documentation 
    opened by Lubba-64 2
  • total_quant_in_inventory

    total_quant_in_inventory

    If the remove_item_from_inventory is going to be used, it would probably be helpful to be able to get the total quantity of a specific item in an entire inventory, just so you can verify that you can indeed remove that many of x item from your inventory.

    enhancement 
    opened by Lubba-64 0
  • Does not support multi slot items

    Does not support multi slot items

    There are games like subnautica, and escape from tarkov, that have items that take up multiple slots worth of space. I was unaware of this potential configuration when making this.

    This is not a feature that is planned, this is just something the crate does not support right now but could in the future. I don't think there are any good ways of adding this in without making some breaking changes.

    The best way I can think of for doing this is splitting the library up into containers, each with a generic implementation for a different type of inventory system. Doing it like this would just mean importing stuff from standard or spatial or whatever other generic inventory ends up being created, like so:

    use game_inventory::spatial::*;
    // or...
    use game_inventory::standard::*;
    
    enhancement wontfix 
    opened by Lubba-64 1
Owner
null
Generic abstractions for combining and nesting reduction patterns for iterables.

reductor Generic abstractions for combining and nesting reduction patterns for iterables. Docs: https//docs.rs/reductor Before & After: Before fn proc

Yotam Ofek 2 Jul 9, 2022
An i386 operation system written in pure rust for fun and no profit.

OrustS An i386 operation system written in pure rust (for fun and no profit). This operation system is under active developing. Checklist implement a

M4tsuri 10 Aug 12, 2022
Elemental System Designs is an open source project to document system architecture design of popular apps and open source projects that we want to study

Elemental System Designs is an open source project to document system architecture design of popular apps and open source projects that we want to study

Jason Shin 9 Apr 10, 2022
Highly experimental, pure-Rust big integer library

grou-num (Pronounced "groo", from the Chiac meaning "big") This package is a highly experimental, unstable big integer library. I would not recommend

Patrick Poitras 1 Dec 18, 2021
Pure-Rust DTLS

dustls, a pure-rust DTLS implementation A DTLSv1.2 implementation in Rust, reusing rustls for cryptographic primitives and most message payload format

Jonathan de Jong 10 Nov 28, 2022
An implementation of Olm and Megolm in pure Rust.

A Rust implementation of Olm and Megolm vodozemac is a Rust implementation of libolm, a cryptographic library used for end-to-end encryption in Matrix

matrix.org 66 Dec 26, 2022
Pure Rust Implementation of secp256k1.

SECP256K1 implementation in pure Rust Cargo Documentation SECP256K1 implementation with no_std support. Currently we have implementation for: Convert

Parity Technologies 141 Dec 21, 2022
An mdBook single PDF generator using pure Rust and some Node.js

mdbook-compress An mdBook backend renderer to generate a single PDF file for a full book. There are other similar projects, but most rely on chrome in

nxe 44 Jan 20, 2023
A pure-rust(with zero dependencies) fenwick tree, for the efficient computation of dynamic prefix sums.

indexset A pure-rust(with zero dependencies, no-std) fenwick tree, for the efficient computation of dynamic prefix sums. Background Did you ever have

Bruno Rucy Carneiro Alves de Lima 2 Jul 13, 2023
High-order Virtual Machine (HVM) is a pure functional compile target that is lazy, non-garbage-collected and massively parallel

High-order Virtual Machine (HVM) High-order Virtual Machine (HVM) is a pure functional compile target that is lazy, non-garbage-collected and massivel

null 5.5k Jan 2, 2023