From e4483eca01b48b943cd0461e24a74ae1a3139ed4 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 21:57:46 +1100 Subject: Update to most recent version (old initial commit) --- src/shared/item/items.hh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/shared/item/items.hh (limited to 'src/shared/item/items.hh') diff --git a/src/shared/item/items.hh b/src/shared/item/items.hh new file mode 100644 index 0000000..ba718c5 --- /dev/null +++ b/src/shared/item/items.hh @@ -0,0 +1,54 @@ +#ifndef SHARED_ITEM_ITEMS_HH_ +#define SHARED_ITEM_ITEMS_HH_ + +#include + +#include "shared/item/block.hh" +#include "shared/item/item.hh" +#include "shared/net/proto.hh" +#include "shared/world/block.hh" + +namespace shared { +namespace item { + +// Use this function over constructor to create a derived item. + +using item_t = std::shared_ptr; +using items_t = std::array; + +using make_item_func_t = item_t (*)(const item::type_t&, const std::uint32_t&); +item_t make_item(const item::type_t& type, + const std::uint32_t& quantity) noexcept; +item_t make_item(const proto::item& item) noexcept; + +// Class for an inventory (or possibly a chest in the future, etc). +class items { +public: + items_t contents = {0}; + +public: + items() noexcept {} + items(const proto::item_array& proto) noexcept; + +public: + // Adds or removes an arbitrary amount of items to the player. + // Returns true of false based on whether the operation completed fully. + bool maybe_add(const item::type_t& type, const std::uint32_t& quantity, + const make_item_func_t& = make_item) noexcept; + bool maybe_remove(const item::type_t& type, + const std::uint32_t& quantity) noexcept; + + // Adds a single existing item, puts in new slot if necessary. + void increment(const std::uint32_t& index, + const make_item_func_t& = make_item) noexcept; + // Removes a single existing item from an index, clears if needed. + void decrement(const std::uint32_t& index) noexcept; + +public: + void pack(proto::item_array* const proto) const noexcept; +}; + +} // namespace item +} // namespace shared + +#endif -- cgit v1.2.3