-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cpp
141 lines (103 loc) · 2.16 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
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
#include "stdafx.h"
#include "Inventory.h"
//private: Functions:
void Inventory::initialize()
{
this->numberOfItems = 0;
this->itemArray = new Item*[this->capacity];
this->nullify();
}
void Inventory::nullify(const unsigned int from)
{
for (size_t i = from; i < static_cast<size_t>(this->capacity); i++)
{
this->itemArray[i] = nullptr;
}
}
void Inventory::freeMemory()
{
for (size_t i = 0; i < static_cast<size_t>(this->numberOfItems); i++)
{
delete this->itemArray[i];
}
delete[] this->itemArray;
}
//Constructors and Destructor:
Inventory::Inventory(const unsigned int capacity)
{
this->capacity = capacity;
this->initialize();
}
Inventory::~Inventory()
{
this->freeMemory();
}
//Accessors:
const unsigned int& Inventory::getSize() const
{
return this->numberOfItems;
}
const unsigned int& Inventory::getSizeMax() const
{
return this->capacity;
}
const bool Inventory::isEmpty() const
{
return this->numberOfItems == 0;
}
//Modifiers:
//
//Functions:
void Inventory::clearInventory()
{
for (size_t i = 0; i < static_cast<size_t>(this->numberOfItems); i++)
{
delete this->itemArray[i];
}
this->numberOfItems = 0;
this->nullify();
}
const bool Inventory::add(Item* item)
{
if (this->numberOfItems < this->capacity)
{
this->itemArray[this->numberOfItems++] = item->clone();
//numberOfItems will be increased after assigning it to itemArray as a number of element
return true;
}
return false;
}
const bool Inventory::remove(const int index)
{
if (this->numberOfItems > 0)
{
if (index < 0 || index > static_cast<int>(this->capacity))
{
std::cout << "ERROR::Inventory::remove::Index is out of range" << std::endl;
return false;
}
else if (this->itemArray[index] != nullptr)
{
delete this->itemArray[index];
this->itemArray[index] = nullptr;
this->numberOfItems--;
std::cout << "Inventory::remove::Item deleted from slot: " << index << std::endl;
return true;
}
}
return false;
}
const bool Inventory::saveToFile(const std::string& file_path)
{
return false;
}
const bool Inventory::loadFromFile(const std::string& file_path)
{
return false;
}
void Inventory::update()
{
}
void Inventory::render()
{
}