Peoplemon  0.1.0
Peoplemon 3 game source documentation
Bag.cpp
Go to the documentation of this file.
1 #include <Core/Player/Bag.hpp>
2 
3 #include <Core/Items/Item.hpp>
4 
5 namespace core
6 {
7 namespace player
8 {
10 : id(item::Id::Unknown)
11 , qty(0) {}
12 
13 Bag::Item::Item(item::Id id, unsigned int qty)
14 : id(id)
15 , qty(qty) {}
16 
17 void Bag::getByCategory(item::Category c, std::vector<Item>& result) const {
18  result.clear();
19  for (const Item& i : items) {
20  if (item::Item::getCategory(i.id) == c) result.push_back(i);
21  }
22 }
23 
24 void Bag::getByType(item::Type t, std::vector<Item>& result) const {
25  result.clear();
26  for (const Item& i : items) {
27  if (item::Item::getType(i.id) == t) result.push_back(i);
28  }
29 }
30 
31 void Bag::getAll(std::vector<Item>& result) const {
32  result.clear();
33  result.reserve(items.size());
34  for (const Item& i : items) { result.push_back(i); }
35 }
36 
37 unsigned int Bag::itemCount(item::Id id) const {
38  const unsigned int i = find(id);
39  return i < items.size() ? items[i].qty : 0;
40 }
41 
42 bool Bag::hasItem(item::Id item) const { return itemCount(item) > 0; }
43 
44 void Bag::addItem(item::Id id, unsigned int qty) {
45  const unsigned int i = find(id);
46  if (i < items.size()) { items[i].qty += qty; }
47  else {
48  items.emplace_back(id, qty);
49  }
50 }
51 
52 bool Bag::removeItem(item::Id id, unsigned int qty) {
53  const unsigned int i = find(id);
54  if (i < items.size()) {
55  if (items[i].qty >= qty) {
56  items[i].qty -= qty;
57  if (items[i].qty == 0) items.erase(items.begin() + i);
58  return true;
59  }
60  }
61  return false;
62 }
63 
64 void Bag::clear() { items.clear(); }
65 
66 unsigned int Bag::find(item::Id id) const {
67  for (unsigned int i = 0; i < items.size(); ++i) {
68  if (items[i].id == id) return i;
69  }
70  return items.size();
71 }
72 
73 } // namespace player
74 } // namespace core
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.
static Category getCategory(Id item)
Returns the category of the given item.
Definition: Item.cpp:49
static Type getType(Id item)
Returns the type of the given item.
Definition: Item.cpp:76
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()
Construct an empty item.
Definition: Bag.cpp:9