-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cpp
50 lines (42 loc) · 925 Bytes
/
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
#include "Inventory.h"
#include "item.h"
Inventory::Inventory(int capacity, int currentSize) : capacity(capacity), currentSize(currentSize) {}
bool Inventory::addItemToInventory(Item* item) {
if (currentSize == capacity || item == nullptr) {
return 0;
}
for (auto& c : items) {
if (c.first->getName() == item->getName()) {
++c.second;
++currentSize;
return 1;
}
}
Item* newItem = item->getCopy();
items[newItem] = 1;
return 1;
}
bool Inventory::useItem(const std::string& item, Player& player) {
for (auto c : items) {
if (c.first->getName() == item) {
if (c.second == 0) {
return 0;
}
--c.second;
--currentSize;
return 1;
}
}
return 0;
}
int Inventory::getCurrentSize() {
return currentSize;
}
int Inventory::getCapacity() {
return capacity;
}
Inventory::~Inventory() {
for (auto& c : items) {
delete c.first;
}
}