-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloor.cc
248 lines (230 loc) · 6.34 KB
/
floor.cc
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <fstream>
#include <iostream>
#include <iomanip>
#include "floor.h"
#include "gold.h"
#include "potion.h"
#include "Enemy.h"
Floor::Floor(std::ifstream& layout, GameObject& player) {
grid.resize(25, std::vector<Cell>(79));
int xRead = 0;
int yRead = 0;
while(yRead < 25) {
char read;
layout >> std::noskipws >> read;
if(read != '\n') {
Cell cellRead(yRead, xRead, read, player);
grid[yRead][xRead] = cellRead;
if(xRead == 78) {
xRead = 0;
++yRead;
}
else {
++xRead;
}
}
}
}
void Floor::setGrid(int y, int x, Cell set) {
grid[y][x] = set;
}
Cell* Floor::getGrid(int x, int y) {
return &grid[x][y];
}
void Floor::objectAdd(int r, int c, GameObject *obj) {
grid[r][c].add(obj);
}
void Floor::objectRemove(int r, int c){
grid[r][c].remove();
}
char Floor::getSymbol(int r, int c){
return grid[r][c].getSymbol();
}
void Floor::setDragonHoard(int r, int c, bool status){
grid[r][c].setDragonHoard(status);
}
bool Floor::getDragonHoard(int r, int c){
return grid[r][c].getDragonHoard();
}
GameObject* Floor::getObj(int r, int c) {
return grid[r][c].getObject();
}
bool Floor::isCellValid(int r, int c, bool isPlayer) {
if(isPlayer) {
return grid[r][c].getPlayerValid();
}
else {
return grid[r][c].getEnemyValid();
}
}
void Floor::generate(std::vector<Chamber> chambers, GameObject* player) {
//Player
std::srand(time(0));
bool generated = false;
int playerChamber = std::rand() % 5;
while(!generated) {
int array = std::rand() % chambers[playerChamber].getCells().size();
std::pair<int,int> coor = chambers[playerChamber].getCells()[array];
int x = coor.first;
int y = coor.second;
if(getGrid(x,y)->getPlayerValid()) {
getGrid(x,y)->setSymbol('@');
getGrid(x,y)->setPlayerValid(false);
getGrid(x,y)->setEnemyValid(false);
player->setx(x);
player->sety(y);
generated = true;
}
}
//Stairs
generated = false;
int chamber = 0;
int array;
while(!generated) {
chamber = std::rand() % 5;
if(chamber != playerChamber) {
generated = true;
}
}
generated = false;
while(!generated) {
array = std::rand() % chambers[chamber].getCells().size();
std::pair<int,int> coor = chambers[chamber].getCells()[array];
int x = coor.first;
int y = coor.second;
if(getGrid(x,y)->getPlayerValid()) {
getGrid(x,y)->setSymbol('\\');
getGrid(x,y)->setEnemyValid(false);
generated = true;
}
}
//Potions
int amountGen = 0;
while(amountGen < 10) {
int type = std::rand() % 6 + 1;
chamber = std::rand() % 5;
array = std::rand() % chambers[chamber].getCells().size();
std::pair<int,int> coor = chambers[chamber].getCells()[array];
int x = coor.first;
int y = coor.second;
if(getGrid(x,y)->getEnemyValid()) {
GameObject* potion;
if(type == 1) {
potion = new RestoreHp(x,y);
}
else if(type == 2) {
potion = new BoostAtk(x,y);
}
else if(type == 3) {
potion = new BoostDef(x,y);
}
else if(type == 4) {
potion = new PoisonHp(x,y);
}
else if(type == 5) {
potion = new WoundAtk(x,y);
}
else {
potion = new WoundDef(x,y);
}
getGrid(x,y)->add(potion);
++amountGen;
}
}
//Gold
amountGen = 0;
while(amountGen < 10) {
int type = std::rand() % 8 + 1;
chamber = std::rand() % 5;
array = std::rand() % chambers[chamber].getCells().size();
std::pair<int,int> coor = chambers[chamber].getCells()[array];
int x = coor.first;
int y = coor.second;
if(getGrid(x,y)->getEnemyValid()) {
GameObject* gold;
if(type >= 1 && type <= 5) {
gold = new Gold(x,y,2);
}
else if(type == 6) {
gold = new Gold(x,y,6);
std::vector<std::pair<int,int>> surrounding;
surrounding.emplace_back(std::make_pair(x,y+1));
surrounding.emplace_back(std::make_pair(x+1,y+1));
surrounding.emplace_back(std::make_pair(x+1,y));
surrounding.emplace_back(std::make_pair(x,y-1));
surrounding.emplace_back(std::make_pair(x-1,y-1));
surrounding.emplace_back(std::make_pair(x-1, y));
surrounding.emplace_back(std::make_pair(x-1,y+1));
surrounding.emplace_back(std::make_pair(x+1,y-1));
std::srand(time(0));
while(1) {
int random = std::rand() % 8;
int newx = surrounding[random].first;
int newy = surrounding[random].second;
if(getGrid(newx,newy)->getEnemyValid()) {
GameObject* dragon = new Dragon(newx,newy,x,y);
getGrid(newx,newy)->add(dragon);
break;
}
}
}
else {
gold = new Gold(x,y,1);
}
getGrid(x,y)->add(gold);
// setting the dragonhoard to true if the gold type is dragon gold.
if(gold->getGold() == 6){
setDragonHoard(x,y,true);
}
++amountGen;
}
} //Enemy
amountGen = 0;
while(amountGen < 20) {
int type = std::rand() % 18 + 1;
chamber = std::rand() % 5;
array = std::rand() % chambers[chamber].getCells().size();
std::pair<int,int> coor = chambers[chamber].getCells()[array];
int x = coor.first;
int y = coor.second;
if(getGrid(x,y)->getEnemyValid()) {
GameObject* enemy;
if(type >= 1 && type <= 4) {
enemy = new Human(x,y);
}
else if(type >= 5 && type <= 7) {
enemy = new Dwarf(x,y);
}
else if(type >= 8 && type <= 12) {
enemy = new Halfling(x,y);
}
else if(type >= 13 && type <= 14) {
enemy = new Elf(x,y);
}
else if(type >= 15 && type <= 16) {
enemy = new Orc(x,y);
}
else if(type >= 17 && type <= 18) {
enemy = new Merchant(x,y);
}
getGrid(x,y)->add(enemy);
++amountGen;
}
}
}
void Floor::print(GameObject &player) {
for(unsigned int i=0; i<grid.size(); ++i) {
for(unsigned int j=0; j<grid[i].size(); ++j) {
std::cout << grid[i][j].getSymbol();
}
std::cout << std::endl;
}
//Player stats
std::cout << "Race : " << player.getRace() << " Gold : " << player.getGold();
std::cout.width(55);
std::cout << std::right << "Floor " << player.getLevel() + 1 << std::endl;
std::cout << "HP : " << player.getHP() << std::endl;
std::cout << "Atk : " << player.getAtk() << std::endl;
std::cout << "Def : " << player.getDef() << std::endl;
std::cout << "Action: " << player.getMessage() << std::endl;
}