-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
225 lines (215 loc) · 6.3 KB
/
Game.cpp
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
#include "Game.hpp"
Game::Game() : map(nullptr), pl(nullptr), running(false), level(0)
{
}
void Game::start()
{
Constants::out << GameAssets::game_logo << "\tPress any key to play...";
getch();
system("cls");
delete map;
delete pl;
Constants::out << "Press\tl to load existing game\nor\tn to begin a new game\n";
char c;
while ((c = getch()) != 'l' && c != 'n')
{
}
if (c == 'l')
{
try
{
load();
}
catch (const MyException &err)
{
delete pl;
delete map;
std::cerr << err.what() << '\n';
getch();
newGame();
}
}
else
newGame();
Constants::LEVEL_STATE res = run();
if (res == Constants::LEVEL_STATE::ERROR)
std::cerr << "Unexpected error occured...\n";
}
void Game::load()
{
delete pl;
pl = nullptr;
delete map;
map = nullptr;
char name[Constants::INPUT_LIMIT];
Constants::out << "\nEnter name of game to be loaded:\n";
std::cin.getline(name, Constants::INPUT_LIMIT);
pl = Game::readPlayer(name);
map = new Map(pl, name);
level = map->getLevel();
}
Player *Game::readPlayer(const String &file)
{
String path = "games\\";
(path += file) += ".dndplayer";
std::ifstream ifs(path);
if (!ifs)
throw MyException(path + " NOT FOUND...", "Player *readPlayer(const String &)");
int type;
ifs >> type;
Player *res = nullptr;
if (type == (int)Player::HERO_TYPE::HUMAN)
res = new Human(ifs);
else if (type == (int)Player::HERO_TYPE::MAGE)
res = new Mage(ifs);
else if (type == (int)Player::HERO_TYPE::WARRIOR)
res = new Warrior(ifs);
else
throw MyException("FAILED TO DETERMINE HERO TYPE...", "Player *readPlayer(const String &)");
return res;
}
void Game::newGame()
{
level = 1;
system("cls");
Constants::out << "New game:\n\tChoose a hero:\n\t1 for human\n\t2 for mage\n\t3 for warrior\n";
char chosen;
do
{
chosen = getch();
} while (chosen - '0' - 1 >= Constants::HERO_TYPES || chosen - '0' - 1 < 0);
char name[Constants::INPUT_LIMIT] = "";
Constants::out << "Choose name for your hero:\t";
std::cin.getline(name, Constants::INPUT_LIMIT);
if (!*name)
strcpy(name, "unknown");
pl = Map::getHero(chosen - '0' - 1, name);
map = new Map(pl, level);
}
Constants::LEVEL_STATE Game::run()
{
if (running)
return Constants::LEVEL_STATE::ERROR;
Constants::LEVEL_STATE tmp;
running = true;
while (running)
{
tmp = map->run();
if (tmp == Constants::LEVEL_STATE::PASS)
{
if (level < Constants::LEVEL_COUNT)
{
delete map;
map = new Map(pl, ++level);
pl->reset();
}
else
{
system("cls");
Constants::out << pl->getName() << ", you proved yourself as a real hero and will be rewarded as deserved!\nThis is the end game...";
getch();
running = false;
return Constants::LEVEL_STATE::END;
}
}
else if (tmp == Constants::LEVEL_STATE::PAUSE)
{
system("cls");
char c;
Constants::out << "Press p to resume the game;\nPress n to start a new game;\nPress s to save current progress;\nPress l to load existing game;\nPress ` to exit.";
while ((c = getch()) != 'p' && c != (char)KEYS::EXIT && c != 's' && c != 'l' && c != 'n')
{
}
if (c == KEYS::EXIT)
{
running = false;
if (!map->isSaved())
{
Constants::out << "\n\ns to save your progress\nx to quit without saving\n";
char c;
while ((c = getch()) != 's' && c != 'x' && c != (char)KEYS::EXIT)
{
}
if (c == 's')
save();
else if (c == KEYS::EXIT)
running = true;
}
}
else if (c == 's')
save();
else if (c == 'l')
{
try
{
if (!map->isSaved())
{
Constants::out << "\n\ns to save your progress\nx to quit without saving\n";
char c;
while ((c = getch()) != 's' && c != 'x' && c != (char)KEYS::EXIT)
{
}
if (c == 's')
save();
else if (c == KEYS::EXIT)
running = true;
}
load();
}
catch (const MyException &err)
{
delete pl;
delete map;
std::cerr << err.what() << '\n';
getch();
newGame();
}
}
else if (c == 'n')
{
if (!map->isSaved())
{
Constants::out << "\n\ns to save your progress\nx to discard progress\n";
char c;
while ((c = getch()) != 's' && c != 'x' && c != (char)KEYS::EXIT)
{
}
if (c == 's')
{
save();
std::cin.ignore();
}
else if (c == KEYS::EXIT)
running = true;
}
newGame();
}
}
else
{
running = false;
return tmp;
}
}
return Constants::LEVEL_STATE::ERROR;
}
void Game::save()
{
Constants::out << "\n\nEnter name of current game: ";
char name[Constants::INPUT_LIMIT];
std::cin >> name;
std::cin.ignore();
try
{
pl->save(name);
map->saveProgress(name);
}
catch (const MyException &err)
{
std::cout << err.what() << '\n';
getch();
return;
}
Constants::out << "Game successfully saved as \"" << name << "\"\n";
getch();
}