Peoplemon  0.1.0
Peoplemon 3 game source documentation
Bag.hpp
Go to the documentation of this file.
1 #ifndef CORE_PLAYER_BAG_HPP
2 #define CORE_PLAYER_BAG_HPP
3 
4 #include <BLIB/Serialization.hpp>
6 #include <Core/Items/Id.hpp>
7 #include <Core/Items/Type.hpp>
8 #include <vector>
9 
10 namespace core
11 {
12 namespace player
13 {
18 class Bag {
19 public:
24  struct Item {
27 
29  unsigned int qty;
30 
35  Item();
36 
43  Item(item::Id id, unsigned int qty);
44  };
45 
52  void getByCategory(item::Category category, std::vector<Item>& result) const;
53 
60  void getByType(item::Type type, std::vector<Item>& result) const;
61 
67  void getAll(std::vector<Item>& result) const;
68 
75  unsigned int itemCount(item::Id item) const;
76 
83  bool hasItem(item::Id item) const;
84 
91  void addItem(item::Id item, unsigned int qty = 1);
92 
100  bool removeItem(item::Id item, unsigned int qty = 1);
101 
106  void clear();
107 
108 private:
109  std::vector<Item> items;
110 
111  unsigned int find(item::Id id) const;
112 
113  friend struct bl::serial::SerializableObject<Bag>;
114 };
115 
116 } // namespace player
117 } // namespace core
118 
119 namespace bl
120 {
121 namespace serial
122 {
123 template<>
124 struct SerializableObject<core::player::Bag::Item> : public SerializableObjectBase {
127 
128  SerializableField<1, Item, Id> id;
129  SerializableField<2, Item, unsigned int> qty;
130 
132  : SerializableObjectBase("BagItem")
133  , id("id", *this, &Item::id, SerializableFieldBase::Required{})
134  , qty("qty", *this, &Item::qty, SerializableFieldBase::Required{}) {}
135 };
136 
137 template<>
138 struct SerializableObject<core::player::Bag> : public SerializableObjectBase {
140 
141  SerializableField<1, Bag, std::vector<Bag::Item>> items;
142 
144  : SerializableObjectBase("Bag")
145  , items("items", *this, &Bag::items, SerializableFieldBase::Required{}) {}
146 };
147 
148 } // namespace serial
149 } // namespace bl
150 
151 #endif
Type
The type classification of an item. This is used to determine when an item may be used and how to use...
Definition: Type.hpp:17
Category
Represents a category that an item can belong to. Used for bag sorting.
Definition: Category.hpp:16
Id
Represents an item in its simplist form.
Definition: Id.hpp:24
Core classes and functionality for both the editor and game.
Basic inventory class for managing player items.
Definition: Bag.hpp:18
void getAll(std::vector< Item > &result) const
Returns all owned items.
Definition: Bag.cpp:31
void getByCategory(item::Category category, std::vector< Item > &result) const
Returns the set of owned items in the given category.
Definition: Bag.cpp:17
bool removeItem(item::Id item, unsigned int qty=1)
Removes the given item from the bag.
Definition: Bag.cpp:52
void addItem(item::Id item, unsigned int qty=1)
Adds the given item to the bag.
Definition: Bag.cpp:44
void getByType(item::Type type, std::vector< Item > &result) const
Returns the set of owned items of the given type.
Definition: Bag.cpp:24
unsigned int itemCount(item::Id item) const
Returns the number of the given item owned.
Definition: Bag.cpp:37
void clear()
Removes all items.
Definition: Bag.cpp:64
bool hasItem(item::Id item) const
Returns true if at least one of the given items is owned.
Definition: Bag.cpp:42
Simple struct representing a set of items in the bag.
Definition: Bag.hpp:24
item::Id id
The item in the bag.
Definition: Bag.hpp:26
Item()
Construct an empty item.
Definition: Bag.cpp:9
unsigned int qty
How many of the item are in the bag.
Definition: Bag.hpp:29
SerializableField< 2, Item, unsigned int > qty
Definition: Bag.hpp:129
SerializableField< 1, Bag, std::vector< Bag::Item > > items
Definition: Bag.hpp:141