-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentWorld.cpp
324 lines (287 loc) · 9.31 KB
/
StudentWorld.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "StudentWorld.h"
#include "GameConstants.h"
#include <string>
using namespace std;
GameWorld* createStudentWorld(string assetPath)
{
return new StudentWorld(assetPath);
}
StudentWorld::StudentWorld(string assetPath)
: GameWorld(assetPath)
{
}
int StudentWorld::init() {
Level lev(assetPath());
ostringstream oss;
oss << "level" << setfill('0') << setw(2) << getLevel() << ".txt";
string level_file = oss.str();
Level::LoadResult result = lev.loadLevel(level_file);
if (result == Level::load_fail_file_not_found || level_file == "level99.txt")
return GWSTATUS_PLAYER_WON;
else if (result == Level::load_fail_bad_format)
return GWSTATUS_LEVEL_ERROR;
else if (result == Level::load_success)
{
cerr << "Successfully loaded level" << endl;
Level::MazeEntry ge;
for (int y = 0; y < VIEW_HEIGHT; y++) {
for (int x = 0; x < VIEW_WIDTH; x++) {
ge = lev.getContentsOf(x, y);
switch (ge) {
case Level::empty:
break;
case Level::player:
m_player = new Avatar(x, y, this);
break;
case Level::wall:
actors.push_back(new Wall(x, y, this));
break;
case Level::crystal:
actors.push_back(new Crystal(x, y, this));
break;
case Level::marble:
actors.push_back(new Marble(x, y, this));
break;
case Level::pit:
actors.push_back(new Pit(x, y, this));
break;
case Level::vert_ragebot:
actors.push_back(new VertRageBot(x, y, this));
break;
case Level::horiz_ragebot:
actors.push_back(new HorizRageBot(x, y, this));
break;
case Level::thiefbot_factory:
actors.push_back(new ThiefBotFactory(x, y, this));
break;
case Level::mean_thiefbot_factory:
actors.push_back(new MeanThiefBotFactory(x, y, this));
break;
case Level::ammo:
actors.push_back(new Ammo(x, y, this));
break;
case Level::restore_health:
actors.push_back(new Restore(x, y, this));
break;
case Level::extra_life:
actors.push_back(new ExtraLife(x, y, this));
break;
case Level::exit:
actors.push_back(new Exit(x, y, this));
break;
}
}
}
}
return GWSTATUS_CONTINUE_GAME;
}
int StudentWorld::move()
{
if (m_player->isAlive()) {
m_player->doSomething();
}
list<Actor*>::iterator it;
it = actors.begin();
while ((it) != actors.end()) {
if ((*it)->isAlive()) {
(*it)->doSomething();
if (!m_player->isAlive()) {
return GWSTATUS_PLAYER_DIED;
}
if (finishGame()) {
return GWSTATUS_PLAYER_WON;
}
if (getCompleteLevel()) {
setCompleteLevel(false);
increaseScore(getScore()+2000+getCurrentLevelBonus());
return GWSTATUS_FINISHED_LEVEL;
}
}
it++;
}
list<Actor*>::iterator it2;
it2 = actors.begin();
while ((it2) != actors.end()) {
if (!(*it2)->isAlive()) {
delete* it2;
it2 = actors.erase(it2);
}
else
{
it2++;
}
}
reduceLevelBonusByOne();
displayGameText();
return GWSTATUS_CONTINUE_GAME;
}
void StudentWorld::displayGameText() {
int score = getScore();
int level = getLevel();
int lives = getLives();
int bonus = getCurrentLevelBonus();
std::ostringstream oss;
oss.fill('0');
oss << "Score: " << setw(6) << score;
oss.fill('0');
oss << " Level: " << setw(2) << level;
oss << " Lives: " << lives;
oss << " Health: " << m_player->getHealth()*100/m_player->getMaxHealth();
oss << "% Ammo: " << m_player->getAmmo();
oss << " Bonus: " << bonus;
setGameStatText(oss.str());
}
bool StudentWorld::damageWProjectile(int X, int Y, Actor* ptr) {
list<Actor*>::iterator it;
it = actors.begin();
while (it != actors.end()) {
if ((*it) == ptr) {
it++;
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y) ) {
if ((*it)->damagable() && (*it)->isAlive()) {
if ((*it)->getHealth() > 2)
{
if((*it)->canMakeRobotSounds())
{
playSound(SOUND_ROBOT_IMPACT);
}
(*it)->setHealth((*it)->getHealth()-2);
}
else
{
if((*it)->canMakeRobotSounds())
{
playSound(SOUND_ROBOT_DIE);
}
(*it)->setAliveStatus(false);
}
return true;
}
}
it++;
}
return false;
}
bool StudentWorld::overlapActor(int X, int Y, Actor* ptr) {
list<Actor*>::iterator it;
it = actors.begin();
while (it != actors.end()) {
if ((*it) == ptr) {
it++;
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y)) {
if (!(*it)->canOverlap()) {
return true;
}
}
else {
}
it++;
}
return false;
}
bool StudentWorld::overlapActorNoPeaNoPitNoExitNoGoodies(int X, int Y, Actor* ptr) {
for (std::list<Actor*>::iterator it = actors.begin(); it != actors.end(); ++it) {
if ((*it) == ptr || (*it)->canBeShot() || (*it)->canSwallowMarbles() || (*it)->canGetToNextLevel() || (*it)->canBePickedUp()) {
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y) && !(*it)->canOverlap()) {
return true;
}
}
return false;
}
Actor* StudentWorld::overlapActorMarble(int X, int Y, Actor* ptr) {
for (std::list<Actor*>::iterator it = actors.begin(); it != actors.end(); ++it) {
if ((*it) == ptr) {
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y) && (*it)->isMovable()) {
return (*it);
}
}
return nullptr;
}
Actor* StudentWorld::overlapActorPit(int X, int Y, Actor* ptr) {
for (std::list<Actor*>::iterator it = actors.begin(); it != actors.end(); ++it) {
if ((*it) == ptr) {
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y) && (*it)->canSwallowMarbles()) {
return (*it);
}
}
return nullptr;
}
Actor* StudentWorld::overlapActorPea(int X, int Y, Actor* ptr) {
for (std::list<Actor*>::iterator it = actors.begin(); it != actors.end(); ++it) {
if ((*it) == ptr) {
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y) && (*it)->canBeShot()) {
return (*it);
}
}
return nullptr;
}
ThiefBot* StudentWorld::overlapActorThiefBot(int X, int Y, Actor* ptr) {
for (std::list<Actor*>::iterator it = actors.begin(); it != actors.end(); ++it) {
if ((*it) == ptr) {
continue;
}
if (checkActorsOverlap((*it)->getX(), (*it)->getY(), X, Y) && (*it)->canStealGoodies()) {
return static_cast<ThiefBot*>(*it);
}
}
return nullptr;
}
bool StudentWorld::checkActorsOverlap(int X1, int Y1, int X2, int Y2) {
if (abs(X1 - X2) <= 0 && abs(Y1 - Y2) <= 0) {
return true;
}
return false;
}
bool StudentWorld::playerDiedDuringThisTick() {
if (m_player != nullptr && m_player->getHealth() <= 0) {
return true;
}
return false;
}
void StudentWorld::reduceLevelBonusByOne() {
if (levelBonus > 0) {
--levelBonus;
}
}
int StudentWorld::getCurrentLevelBonus() const {
return levelBonus;
}
void StudentWorld::cleanUp()
{
list<Actor*>::iterator it;
it = actors.begin();
while (it != actors.end()) {
delete* it;
it = actors.erase(it);
}
delete m_player;
}
void StudentWorld::createProjectile(int X, int Y, int direction) {
Pea* tempPea = new Pea(IID_PEA, X, Y, direction, this);
tempPea->setDirection(direction);
actors.push_back(tempPea);
}
void StudentWorld::createThiefBot(int X, int Y, int direction, bool isMean) {
ThiefBot* tempActor = new ThiefBot(IID_THIEFBOT, X, Y, this);
tempActor->setDirection(direction);
actors.push_back(tempActor);
}
bool StudentWorld::overlapAvatar(int X, int Y) {
return checkActorsOverlap( X, Y, m_player->getX(), m_player->getY());
}
void StudentWorld::spawnThiefBot(ThiefBot* newThiefBot) {
playSound(SOUND_ROBOT_BORN);
actors.push_back(newThiefBot);
}