aboutsummaryrefslogtreecommitdiff
path: root/src/shared/item/items.hh
blob: ba718c57d331e53871ec91d1a015c1b8e098fff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef SHARED_ITEM_ITEMS_HH_
#define SHARED_ITEM_ITEMS_HH_

#include <memory>

#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<item>;
using items_t = std::array<item_t, 50>;

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