forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.cpp
69 lines (56 loc) · 1.39 KB
/
inventory.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "stdafx.h"
#include "inventory.h"
using namespace std;
void Inventory::addItem(PItem item) {
itemsCache.push_back(item.get());
items.push_back(move(item));
}
void Inventory::addItems(vector<PItem> v) {
for (PItem& it : v)
addItem(std::move(it));
}
PItem Inventory::removeItem(Item* itemRef) {
int ind = -1;
for (int i : All(items))
if (items[i].get() == itemRef) {
ind = i;
break;
}
CHECK(ind > -1) << "Tried to remove unknown item.";
PItem item = std::move(items[ind]);
items.erase(items.begin() + ind);
removeElement(itemsCache, itemRef);
return item;
}
vector<PItem> Inventory::removeItems(vector<Item*> items) {
vector<PItem> ret;
for (Item* item : items)
ret.push_back(removeItem(item));
return ret;
}
vector<PItem> Inventory::removeAllItems() {
itemsCache.clear();
return move(items);
}
vector<Item*> Inventory::getItems(function<bool (Item*)> predicate) const {
vector<Item*> ret;
for (const PItem& item : items)
if (predicate(item.get()))
ret.push_back(item.get());
return ret;
}
vector<Item*> Inventory::getItems() const {
return itemsCache;
}
bool Inventory::hasItem(const Item* itemRef) const {
for (const PItem& item : items)
if (item.get() == itemRef)
return true;
return false;
}
int Inventory::size() const {
return items.size();
}
bool Inventory::isEmpty() const {
return items.empty();
}