-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinventory.cpp
More file actions
154 lines (127 loc) · 3.52 KB
/
inventory.cpp
File metadata and controls
154 lines (127 loc) · 3.52 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
using namespace std;
#include "main.h"
#include "item.h"
#include "unit.h"
#include "inventory.h"
Inventory::Inventory() {
init(INVENTORY_MAX_NUM_ITEMS);
}
Inventory::Inventory(int maxNumItems) {
init(maxNumItems);
}
void Inventory::init(int maxNumItems) {
this->maxNumItems = maxNumItems;
items = new Item *[maxNumItems];
numItems = 0;
for (int i = 0; i < maxNumItems; i++) {
items[i] = NULL;
}
}
Inventory::~Inventory() {
for (int i = 0; i < maxNumItems; i++) {
if (items[i] != NULL) {
delete items[i];
}
}
delete [] items;
items = NULL;
maxNumItems = 0;
numItems = 0;
}
void Inventory::print() {
cout << "[";
for (int i = 0; i < maxNumItems; i++) {
if (items[i] != NULL) {
items[i]->print();
}
else {
cout << " ";
}
if (i < maxNumItems - 1) {
cout << "|";
}
}
cout << "]";
}
void Inventory::printIndex() {
cout << " ";
for (int i = 0; i < capacity(); i++) {
cout << i << " ";
}
}
bool Inventory::add(Item *item) { // 빈곳에 넣는다.
for (int i = 0; i < maxNumItems; i++) {
if (items[i] == NULL) {
items[i] = item;
numItems++;
return true;
}
}
return false;
}
bool Inventory::insertItemAt(Item *item, int index) { // given index에 넣어야 함.
if (index < 0 || index >= maxNumItems) {
cout << "Inventory::insertItemAt(): error - index out of bound: " << index << endl;
exit(1);
}
if (items[index] == NULL) {
items[index] = item;
numItems++;
return true;
}
return false;
}
Item *Inventory::getItemAt(int index) {
if (index < 0 || index >= maxNumItems) {
cout << "Inventory::getItemAt(): error - index out of bound: " << index << endl;
exit(1);
}
return items[index];
}
Item *Inventory::removeItemAt(int index) { // given index의 item을 return한다. 단 그 자리에 NULL을 넣는다.
if (index < 0 || index >= maxNumItems) {
cout << "Inventory::removeItemAt(): error - index out of bound: " << index << endl;
exit(1);
}
if (items[index] != NULL) {
Item *itemToReturn = items[index];
items[index] = NULL;
numItems--;
return itemToReturn;
}
return NULL;
}
void Inventory::useItemAt(int index, Unit *unit) { // given index의 item을 use하고, 만약 그 아이템이 disposable이면 delete한다. 그 아이템이 removable(equippable)이면 delete(heap에서)하지는 않고, inventory에서만 빠진다.
if (index < 0 || index >= maxNumItems) {
cout << "Inventory::useItemAt(): error - index out of bound: " << index << endl;
exit(1);
}
if (items[index] != NULL) {
if (items[index]->use(unit)) {
if (!items[index]->isRemovable()) {
delete items[index];
}
items[index] = NULL;
numItems--;
}
}
}
bool Inventory::isFull() {
return numItems >= maxNumItems;
}
int Inventory::size() {
return numItems;
}
int Inventory::capacity() {
return maxNumItems;
}
void Inventory::pack() { // 빈 슬롯이 있다면, 모두 없앤다. 즉 앞으로 땡긴다.
for (int i = 0; i < maxNumItems-1; i++) {
if (items[i] == NULL) {
for (int j = i; j < maxNumItems-1; j++) {
items[j] = items[j+1];
}
}
}
}