-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.cpp
More file actions
184 lines (165 loc) · 5.46 KB
/
Copy pathPlayer.cpp
File metadata and controls
184 lines (165 loc) · 5.46 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Player.h"
// Enter methods for player
Player::Player(string name, string description, float HP, float ATK, float DEF, float ACC, float CRT, int wealth)
: Entity(name, description, HP, ATK, DEF, ACC, CRT, 0, 0)
{
this->wealth = wealth;
}
void Player::equipItem(string itemName) {
Item* target = nullptr;
for (Item& item : inv) {
if(item.getShortDescription().compare(itemName) == 0) {
target = &item;
}
}
if(target == nullptr) {
cout << "No such Item." << endl;
} else {
switch(target->getType()) {
case Weapon:
setEquippedWeapon(*target);
target->setEquipped(true);
cout << "You equipped: " << target->getShortDescription() << "!\n" << endl;
break;
case Armor:
setEquippedArmor(*target);
target->setEquipped(true);
cout << "You equipped: " << target->getShortDescription() << "!\n" << endl;
break;
case Accessory:
setEquippedAccessory(*target);
target->setEquipped(true);
cout << "You equipped: " << target->getShortDescription() << "!\n" << endl;
break;
default:
cout << "You cannot equip this item!" << endl;
}
}
}
void Player::showInventory() {
char choice = 'A';
cout << "\nInventory size: " << inv.size() << endl;
for(Item &item : inv) {
cout << choice << ") " << item << " - " << item.getTypeAsString();
if(item.getEquipped()) {
cout << " - " << "equipped";
}
cout << endl;
choice++;
}
}
void Player::showStats() {
cout << "\nHP: " << getHP() << endl;
cout << "Attack: " << getATK() << endl;
cout << "Defence: " << getDEF() << endl;
cout << "Money: " << getWealth() << endl;
cout << endl;
}
Item* Player::putItem(string itemName) {
//inv.erase(std::remove(inv.begin(), inv.end(), target), inv.end()); // Erase-remove idiom
Item* itemPtr = nullptr;
for(auto it = inv.begin(); it != inv.end(); it++) {
if((*it).getShortDescription().compare(itemName) == 0){
itemPtr = new Item((*it));
if((*it).getEquipped()) {
switch((*it).getType()) {
case Weapon:
setATK((getATK() - (*it).getATK()) + 10);
break;
case Accessory:
setATK(getATK() - (*it).getATK());
setDEF(getDEF() - (*it).getDEF());
break;
case Consumable:
break;
case KeyItem:
break;
case Armor:
setDEF((getDEF() - (*it).getDEF()) +10);
}
(*it).setEquipped(false);
}
inv.erase(it);
break;
}
}
return itemPtr;
}
int Player::itemPresentInInventory() {
if (inv.size() < 1) {
return -1;
}
else {
return 1;
}
}
int Player::isItemInInventory(string inString)
{
int sizeItems = (inv.size());
if (inv.size() < 1) {
return -1;
}
else if (inv.size() > 0) {
int x = 0;
for (int n = sizeItems; n > 0; n--) {
// compare inString with short description
int tempFlag = inString.compare( inv[x].getShortDescription());
if (tempFlag == 0) {
return x;
}
x++;
}
}
return -1;
}
void Player::showWealth() {
cout << "Money: " << getWealth() << endl;
cout << endl;
}
void Player::use(string itemName) {
Item* target = nullptr;
for (Item& item : inv) {
if(item.getShortDescription().compare(itemName) == 0) {
target = &item;
}
}
if(target == nullptr) {
cout << "No such Item found" << endl;
} else {
if(target->getType() == Consumable) {
cout << "You used: " << target->getShortDescription() << "!\n";
if(getHP() < 100) {
setEquippedHealth(*target);
target->setEquipped(true);
putItem(target->getShortDescription());
} else {
cout << "Already at full health" << endl;;
}
} else {
cout << "You cannot use this item!" << endl;;
}
}
}
void Player::attack() {
if(ACC >= (double)(rand())/RAND_MAX) {
if(CRT >= (double)(rand())/RAND_MAX) {
cout << "Critial Hit" << endl;
(*currentEnemy).HP = max(0, (*currentEnemy).HP - (ATK*2));
}
else {
if((*currentEnemy).DEF >= ATK) {
cout << "Your Attack hit but damage dealt has been halved due to armor" << endl;
(*currentEnemy).HP = max(0, (*currentEnemy).HP - (ATK/2));
}
else {
cout << "Your Attack hit" << endl;
(*currentEnemy).HP = max(0, (*currentEnemy).HP - ATK);
}
}
}
else {
cout << "Your attack missed" << endl;
}
cout << "Enemy HP: " << (*currentEnemy).HP << endl;
cout << endl;
}