-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cc
168 lines (153 loc) · 5.58 KB
/
game.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
#include "game.h"
#include <string>
#include <fstream>
#include <sstream>
// Valid commands during game
const char N = '8', S = '2', E = '6', W = '4' , NE = '9', NW = '7', SE = '3', SW = '1';
const char Use = 'u', Atk = 'a', restart = 'r', quit = 'q';
Game::Game(char race, std::string file){
td = std::make_shared<TextDisplay>();
pc = f.spawnPlayer(race);
td->setPC(pc);
td->addAction("Game Started as " + pc->getRace() + " ! ");
for (int count = 0; count < 5; count++){
allFloors.emplace_back(std::make_shared<Floor>(file));
allFloors[count]->setPC(pc);
allFloors[count]->setTD(td);
}
f.genFloor(allFloors);
allFloors[levelCount]->notifyObserver();
std::cout << *td;
mvprintw(25, 70, "%s", "Floor 1");
td->clearAction();
}
Game::Game(char race, bool isSpecified, std::string file) {
td = std::make_shared<TextDisplay>();
pc = f.spawnPlayer(race);
td->setPC(pc);
td->addAction("Game Started as " + pc->getRace() + " ! ");
std::ifstream fs{file};
for (int count = 0; count < 5; count++){
allFloors.emplace_back(std::make_shared<Floor>(pc,fs));
allFloors[count]->setPC(pc);
allFloors[count]->setTD(td);
}
allFloors[levelCount]->notifyObserver();
std::cout << *td;
mvprintw(25, 70, "%s", "Floor 1");
td->clearAction();
}
int Game::takeCommand(){
if (levelCount >= 5){
return 1;
}
if (pc->isDead()) {
gameOver();
return 1;
}
bool valid = true;
int move = 0;
char action = getch();
if (action == N || action == (char)KEY_UP) move = allFloors[levelCount]->playerMove("N");
else if (action == S || action == (char)KEY_DOWN) move = allFloors[levelCount]->playerMove("S");
else if (action == E || action == (char)KEY_RIGHT) move = allFloors[levelCount]->playerMove("E");
else if (action == W || action == (char)KEY_LEFT) move = allFloors[levelCount]->playerMove("W");
else if (action == NE) move = allFloors[levelCount]->playerMove("NE");
else if (action == NW) move = allFloors[levelCount]->playerMove("NW");
else if (action == SE) move = allFloors[levelCount]->playerMove("SE");
else if (action == SW) move = allFloors[levelCount]->playerMove("SW");
else if (action == Use){
action = getch();
if (action == N || action == (char)KEY_UP) allFloors[levelCount]->playerUse("N");
else if (action == S || action == (char)KEY_DOWN) allFloors[levelCount]->playerUse("S");
else if (action == E || action == (char)KEY_RIGHT) allFloors[levelCount]->playerUse("E");
else if (action == W || action == (char)KEY_LEFT) allFloors[levelCount]->playerUse("W");
else if (action == NE) allFloors[levelCount]->playerUse("NE");
else if (action == NW) allFloors[levelCount]->playerUse("NW");
else if (action == SE) allFloors[levelCount]->playerUse("SE");
else if (action == SW) allFloors[levelCount]->playerUse("SW");
else {
td->addAction("Invalid Operation!");
valid = false;
}
} else if (action == Atk) {
action = getch();
if (action == N || action == (char)KEY_UP) allFloors[levelCount]->playerAtk("N");
else if (action == S || action == (char)KEY_DOWN) allFloors[levelCount]->playerAtk("S");
else if (action == E || action == (char)KEY_RIGHT) allFloors[levelCount]->playerAtk("E");
else if (action == W || action == (char)KEY_LEFT) allFloors[levelCount]->playerAtk("W");
else if (action == NE) allFloors[levelCount]->playerAtk("NE");
else if (action == NW) allFloors[levelCount]->playerAtk("NW");
else if (action == SE) allFloors[levelCount]->playerAtk("SE");
else if (action == SW) allFloors[levelCount]->playerAtk("SW");
else {
td->addAction("Invalid Operation!");
valid = false;
}
} else if (action == restart) {
return 5;
} else if (action == quit){
return 4;
} else {
td->addAction("Invalid Operation!");
valid = false;
}
if (move == 1) return nextFloor();
if (move == 3) valid = false;
if (valid) {
allFloors[levelCount]->moveEnemies();
allFloors[levelCount]->checkEvents();
}
std::cout << *td;
std::stringstream s;
s << "Floor " << levelCount + 1;
mvprintw(25, 70, "%s", s.str().c_str());
s.str("");
td->clearAction();
return 0;
}
void Game::resetGame(){
levelCount = 0;
}
int Game::nextFloor() {
levelCount++;
if (levelCount >= 5){
td->addAction("PC has won this Game!");
gameWon();
return 1;
}
pc->setKilledMerch(false);
allFloors[levelCount]->notifyObserver();
td->addAction("PC has entered Floor " + std::to_string(levelCount + 1) + ". ");
pc->removePotion();
std::stringstream s;
s << "Floor " << levelCount + 1;
std::cout << *td;
mvprintw(25, 70, "%s", s.str().c_str());
s.str("");
return 0;
}
void Game::gameOver(){
clear();
mvprintw(0, 0, "%s", "GAME OVER!!! ");
mvprintw(1, 0, "%s", "YOU DIED.");
double score = pc->getTreasure();
if (pc->getRace() == "Human"){
score *= 1.5;
}
std::stringstream s;
s << "Your score is: " << score;
mvprintw(2, 0, "%s", s.str().c_str());
}
void Game::gameWon(){
clear();
mvprintw(0, 0, "%s", "GAME WON!!!");
mvprintw(1, 0, "%s", "YOU HAVE ESCAPED FROM THE DUNGEON!!!");
double score = pc->getTreasure();
if (pc->getRace() == "Human"){
score *= 1.5;
}
std::stringstream s;
s << "Your score is: " << score;
mvprintw(2, 0, "%s", s.str().c_str());
}