-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
149 lines (123 loc) · 4.03 KB
/
Player.cpp
File metadata and controls
149 lines (123 loc) · 4.03 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
#ifndef PLAYER_C
#define PLAYER_C
#include <iostream>
#include <map>
#include "Entity.cpp"
#include "Equipable.cpp"
#include "Inventory.cpp"
class Player : public Entity{
private:
std::map<std::string, Equipable*> equipped;
Inventory* inventory;
public:
Player(std::string name) : Entity(name,100, 100, 10, 5) {
equipped["head"] = nullptr;
equipped["chest"] = nullptr;
equipped["legs"] = nullptr;
equipped["feet"] = nullptr;
equipped["hand"] = nullptr;
inventory = new Inventory();
}
// ============= INVENTORY ===========
void addItem(Equipable* weapon) {
inventory->addItem(weapon);
}
void removeItem(Equipable* weapon) {
inventory->removeItem(weapon);
}
void showInventory() {
inventory->display();
std::cout << "\n";
std::cout << "1. Equip an item\n";
std::cout << "2. View an item\n";
std::cout << "3. Go back\n";
std::cout << "\nEnter your choice: ";
int choice; std::cin >> choice;
while (choice < 1 && choice > 3) {
std::cout << "Invalid choice\n";
std::cout << "Choose again: ";
std::cin >> choice;
}
if (choice == 1) {
std::cout << "Enter the name of the item you want to equip: ";
std::string name; std::cin >> name;
Equipable* item = inventory->getItem(name);
if (item) {
this->equipItem(item);
}
else {
std::cout << "You don't have " << name << " in your inventory\n";
}
}
else if (choice == 2) {
std::cout << "Enter the name of the item you want to view: ";
std::string name; std::cin >> name;
Equipable* item = inventory->getItem(name);
if (item) {
item->viewDetails();
}
else {
std::cout << "You don't have " << name << " in your inventory\n";
}
}
else return;
}
void buyItem(Equipable* item) {
addItem(item);
this->updateGold(item->getValue(), false);
}
// =============== STATS ============
void showStats() const {
Entity::showStats();
std::cout << "Gold: " << this->getGold() << "\n";
std::cout << "Experience: " << this->getExperience() << "\n";
std::cout << std::endl;
std::cout << "\n1. View equipped item.\n";
std::cout << "2. Go back.\n";
std::cout << "\nEnter your choice: ";
int choice; std::cin >> choice;
while (choice < 1 && choice > 2) {
std::cout << "Invalid choice\n";
std::cout << "Choose again: ";
std::cin >> choice;
}
if (choice == 1) this->showEquipped();
else return;
}
// ============== Equipables =================
void showEquipped() const {
std::cout << "=========" << this->getName() << "'s equipped items ==========\n";
std::cout << "Head: " << (equipped.at("head") == nullptr ? "Nothing" : equipped.at("head")->getName()) << "\n";
std::cout << "Chest: " << (equipped.at("chest") == nullptr ? "Nothing" : equipped.at("chest")->getName()) << "\n";
std::cout << "Legs: " << (equipped.at("legs") == nullptr ? "Nothing" : equipped.at("legs")->getName()) << "\n";
std::cout << "Feet: " << (equipped.at("feet") == nullptr ? "Nothing" : equipped.at("feet")->getName()) << "\n";
std::cout << "Hand: " << (equipped.at("hand") == nullptr ? "Nothing" : equipped.at("hand")->getName()) << "\n";
std::cout << std::endl;
}
void equipItem(Equipable* item) {
std::string slot = item->getSlot();
// if the slot is already equipped, remove the item and add to inventory
if (equipped[slot]) {
Equipable* current = removeEquipped(slot);
this->addItem(current);
}
// equip the item at slot and remove the item from inventory
this->equipped[slot] = item;
this->updateStats(item);
std::cout << "You have equipped " << item->getName() << " at " << slot << "\n";
this->removeItem(item);
}
Equipable* removeEquipped(std::string slot) {
Equipable* current = this->equipped[slot];
this->equipped[slot] = nullptr;
std::cout << "You have removed " << current->getName() << " from " << slot << "\n";
this->updateStats(current, false);
return current;
}
void updateStats(Equipable* item, bool adding=true) {
this->updateDMG(item->getDMG(), adding);
this->updateDef(item->getDef(), adding);
this->updateHP(item->getDurability(), adding);
}
};
#endif // !PLAYER_C