-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattle.cpp
More file actions
212 lines (187 loc) · 5.34 KB
/
battle.cpp
File metadata and controls
212 lines (187 loc) · 5.34 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "battle.h"
Monster* Battle::getRandMonster() {
Monster* m;
/* Floor specific monster generation*/
switch (floorNumber/ 4 + 1) {
case 1:
switch (rand() % 3 + 1) {
case 1:
m = new AngrySpider;
break;
case 2:
m = new Goblin;
break;
case 3:
m = new GreenSlime;
break;
default:
m = new AngrySpider;
}
m->setGold(rand() % 100 + 50);
break;
case 2:
switch (rand() % 4 + 1) {
case 1:
m = new SkeletonSoldier;
break;
case 2:
m = new Chimera;
break;
case 3:
m = new Golem;
break;
case 4:
m = new Berserker;
break;
default:
m = new SkeletonSoldier;
}
m->setGold(rand() % 200 + 75);
break;
case 3:
switch (rand() % 3 + 1) {
case 1:
m = new Necromancer;
break;
case 2:
m = new BulkyOrc;
break;
case 3:
m = new SkeletonKing;
break;
default:
m = new Necromancer;
}
m->setGold(rand() % 999 + 450);
break;
default:
m = new AngrySpider;
}
monsters.push_back(m); //first save to vector
return m;
}
Battle::Battle(Character& ch, Inventory& i, int f) : Event(ch, i, "<B>"), isDead(false), floorNumber(f) {
monster = getRandMonster();
}
Battle::~Battle() {
if (monsters.size() != 0) {
for (unsigned i = 0; i < monsters.size(); i++)
delete monsters[i];
monsters.clear();
}
}
void Battle::performEvent() {
if (!isDead) {
int maxHP = monster->getHP();
std::cout << "You ran into a " << monster->getMonsterName() << "!\n" << std::endl;
while (character.getCurrHP() > 0 && monster->getHP() > 0) {
std::cout << "Enemy HP: " << monster->getHP();
if (monster->getStatus() >= 0) {
std::cout << " Enemy Status: " << monster->getStrStatus();
}
std::cout << " Your HP: " << character.getCurrHP() << "/" << character.getHP();
std::cout << " Your Mana: " << character.getCurrMana() << "/" << character.getMana() << std::endl;
std::string userInput;
int count = 0;
do {
if (count > 0) {
std::cout << "Please enter valid input\n\n";
}
std::cout << "--COMMANDS--\n";
std::cout << "Attack: 'enter key'\n";
std::cout << "Use special ability: 's'\n";
std::cout << "Run Away: 'run'\n";
std::cout << "Open Invntory: 'o'\n";
std::cout << "Input which command you want: ";
std::getline(std::cin, userInput);
std::cout << "\n";
std::transform(userInput.begin(), userInput.end(), userInput.begin(), ::tolower);
count++;
} while (userInput != "run" && userInput != "o" && userInput != "s" && userInput != "");
if (userInput == "run") {
isVisited = true;
return;
}
else if (userInput == "o") {
if (i.useInventory()) { //If the user used an item
if (monster->getHP() > 0) {
monsterAttack(maxHP);
}
}
}
else if (userInput == "s") {
int n, dmg;
std::string input;
character.displayMoves();
bool validMove;
do {
validMove = true;
std::cout << "Which move would you like to use? ";
std::getline(std::cin, input);
if (input.length() != 1) {
validMove = false;
std::cout << "Invalid input! ";
continue;
}
try {
n = stoi(input);
dmg = character.useSpecial(n, monster);
}
catch (const std::invalid_argument& e) {
std::cout << "Invalid number! ";
validMove = false;
}
catch (const std::out_of_range& e) {
std::cout << "Invalid number! ";
validMove = false;
}
} while (!validMove);
if (dmg != 0) { //0 means not enough mana
monster->takeDamage(dmg);
if (monster->getHP() > 0) {
monsterAttack(maxHP);
}
}
}
else {
std::cout << "\nYou attack the " << monster->getMonsterName() << std::endl;
monster->takeDamage(character.attack());
if (monster->getHP() > 0) {
monsterAttack(maxHP);
}
}
std::cout << std::endl;
}
isVisited = true;
if (monster->getHP() <= 0) {
isDead = true;
std::cout << "You successfully killed the " << monster->getMonsterName() << "!" << std::endl;
std::cout << "You gained " << monster->getGold() << " gold" << std::endl;
character.addMoney(monster->getGold());
}
std::cout << std::endl;
}
}
void Battle::monsterAttack(int maxHP) {
int monsterAtk = monster->attack();
if (monsterAtk < 0) { //I set it up so neg damage means special attack
std::cout << "The " << monster->getMonsterName() << " used " << monster->getAbilityName() << "!" << std::endl;
monsterAtk *= -1;
}
else if (monsterAtk > 0) { //regular attack
std::cout << "The " << monster->getMonsterName() << " attacks!" << std::endl;
}
//If monsterAtk == 0, then monster is asleep or paralyzed & didn't attack
if (!character.takeDamage(monsterAtk)) {
throw std::string("You were killed by the " + monster->getMonsterName() + "!\nGAME OVER");
}
if (monster->getStatus() == 0) { //if monster is poisoned
int dmg = static_cast<int>(maxHP * .1);
monster->takePoisonDamage(dmg);
std::cout << monster->getMonsterName() << " took " << dmg << " damage due to poison!" << std::endl;
}
}
std::string Battle::getSymbol() const {
return isDead ? " ." : (isVisited ? "<B>" : notVisitedSymbol); //show '.' if not dead, and <B> if otherwise
}
std::vector<Monster*> Battle::monsters;