-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventory.cpp
More file actions
163 lines (141 loc) · 4.68 KB
/
Copy pathInventory.cpp
File metadata and controls
163 lines (141 loc) · 4.68 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
155
156
157
158
159
160
161
162
#include "Header.h"
//====================================================Inventory_functions=======================================================================================================
int MyInventory::ResizeInventoryBig()
{
int new_capacity = capacity + 1;
Item** temp = new Item*[new_capacity];
for (int i = 0; i < count; i++)
temp[i] = items[i];
delete[] items;
items = temp;
capacity = new_capacity;
return 0;
}
void MyInventory::DisplayInventory() const //Shows items in inventory
{
if (count == 0)
{
std::cout << "\nInventory is empty!" << std::endl;
return;
}
std::cout << "\n\n=== Your Inventory ================" << std::endl;
for (int i = 0; i < count; i++)
{
std::cout << "\nID: " << items[i]->GetItemId() << "\nName: " <<items[i]->GetName() << " (Type: " << items[i]->GetType() << ")" << std::endl;
items[i]->ShowInfo();
}
std::cout << "\n==================================" << std::endl;
}
int MyInventory::AddItem(Item* item) {
if (count >= capacity) ResizeInventoryBig();
item->SetItemId(id);
items[count++] = item;
std::cout << "Added item with ID: " << id << std::endl;
id++;
return 0;
}
int MyInventory::DelItem(int itemId) {
for (int i = 0; i < count; i++) {
if (items[i]->GetItemId() == itemId) {
delete items[i];
for (int j = i; j < count - 1; j++) {
items[j] = items[j + 1];
}
count--;
std::cout << "Item with ID " << itemId << " deleted." << std::endl;
return 0;
}
}
std::cout << "Item with ID " << itemId << " not found!" << std::endl;
return -1;
}
void MyInventory::InventoryFunctions() //For displaying functions , like : show items , delete items; (MENU)
{
while(true)
{
int choice =0;
std::cout << "\n\n" << std::endl;
std::cout <<"==================Inventory================="<< std::endl;
std::cout << "Size: " << count << std::endl;
std::cout <<"=================Functions=================="<< std::endl;
std::cout <<"Disaply all items in : 1 \nDelete item from id : 2 \nEquip item from id : 3 " << std::endl;
std::cout << "Exit: 0" << std::endl;
std::cout <<"============================================="<< std::endl;
std::cout << "\n\n" << std::endl;
while (true)
{//Show inventory
std::cout << "Choice: ";
std::cin >>choice;
if (choice == 1) DisplayInventory();
else if (choice == 2)
{//Display all items
int choice_2;
std::cout << "Eneter id of item to delete : " ;
std::cin>> choice_2;
DelItem(choice_2);
}
else if (choice == 3)
{//Equip item
int itemId;
std::cout << "Enter item ID to equip: ";
std::cin >> itemId;
//Search items in inventory
for (int i = 0; i < count; i++)
{
if (items[i]->GetItemId() == itemId)
{
if (items[i]->GetType() == "Weapon")
{
owner->EquipWeapon(dynamic_cast<Weapon*>(items[i]));
}
else if (items[i]->GetType() == "Armor")
{
owner->EquipArmor(dynamic_cast<Armory*>(items[i]));
}
else
{
std::cout << "This item cannot be equipped!" << std::endl;
}
continue;
}
}
}
else if (choice == 4) {
int itemId;
std::cout << "Enter item ID to use: ";
std::cin >> itemId;
bool found = false;
for (int i = 0; i < count; i++) {
if (items[i]->GetItemId() == itemId) {
found = true;
if (items[i]->GetType() == "Potion")
{
Potion* potion = dynamic_cast<Potion*>(items[i]);
// Важливо: тут потрібен owner, але ApplyEffect вказує на nullptr з Use()
// Без фікса в Potion.cpp тут буде падіння/повернення
potion->ApplyEffect(owner);
DelItem(itemId);
break;
}
}
}
if (!found) {
std::cout << "Item with ID " << itemId << " not found!" << std::endl;
}
}
}
}
}
void MyInventory::Clear()
{
for (int i = 0; i < count; i++)
{
delete items[i];
}
count = 0;
capacity = 0;
id = 1;
delete[] items;
items = nullptr;
}
//==============================================For_other_characters================================================================