-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentWorld.cpp
More file actions
331 lines (285 loc) · 9.78 KB
/
StudentWorld.cpp
File metadata and controls
331 lines (285 loc) · 9.78 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
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
325
326
327
328
329
330
331
#include "StudentWorld.h"
#include "GameConstants.h"
#include "Actor.h"
#include "Level.h"
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
using namespace std;
GameWorld* createStudentWorld(string assetPath)
{
return new StudentWorld(assetPath);
}
StudentWorld::StudentWorld(string assetPath)
: GameWorld(assetPath), bonusPts(1000), levelComplete(false)
{
m_avatar = nullptr;
m_actors.clear();
m_numCrystals = 0;
}
StudentWorld::~StudentWorld(){
cleanUp();
}
int StudentWorld::init()
{
levelComplete = false;
bonusPts = 1000;
m_numCrystals = 0;
Level lev(assetPath());
//build file name
ostringstream oss;
oss << setw(2) << setfill('0') << getLevel();
string lvlFile = "level" + oss.str() + ".txt";
Level::LoadResult result = lev.loadLevel(lvlFile);
//check for file
if (result == Level::load_fail_file_not_found)
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\n";
//read level file
for(int x = 0; x < 16; x++){
for(int y = 0; y < 16; y++){
Level::MazeEntry ge = lev.getContentsOf(x,y);
switch (ge)
{
//CHECK FOR EXIT AT THE VERY BEGINNING OF THE LIST AND THEN RETRIEVE EXIT BY RETRIEVING FIRST LIST ITEM
case Level::empty:
break;
case Level::player:
m_avatar = new Avatar(this, x, y);
break;
case Level::wall:
m_actors.push_back(new Wall(this, x, y));
break;
case Level::pit:
m_actors.push_back(new Pit(this, x, y));
break;
case Level::marble:
m_actors.push_front(new Marble(this, x, y));
break;
case Level::horiz_ragebot:
m_actors.push_back(new RageBot(this, x, y, Alive::right));
break;
case Level::vert_ragebot:
m_actors.push_back(new RageBot(this, x, y, Alive::down));
break;
//TODO:IMPLEMENT THIEFBOT FACTORY
case Level::thiefbot_factory:
m_actors.push_back(new ThiefBotFactory(this, x, y, ThiefBotFactory::REGULAR));
break;
case Level::mean_thiefbot_factory:
m_actors.push_back(new ThiefBotFactory(this, x, y, ThiefBotFactory::MEAN));
break;
case Level::crystal:
m_actors.push_front(new Crystal(this, x, y));
m_numCrystals++;
break;
case Level::extra_life:
m_actors.push_front(new ExtraLifeGoodie(this, x, y));
break;
case Level::restore_health:
m_actors.push_front(new RestoreHealthGoodie(this, x, y));
break;
case Level::ammo:
m_actors.push_front(new AmmoGoodie(this, x, y));
break;
case Level::exit:
m_actors.push_back(new Exit(this, x, y));
break;
}
}
}
}
return GWSTATUS_CONTINUE_GAME;
}
int StudentWorld::move()
{
list<Actor*>::iterator it;
for(it = m_actors.begin(); it != m_actors.end(); it++){
if((*it)->isActive())
{
(*it) -> doSomething();
if(!m_avatar->isActive())
return GWSTATUS_PLAYER_DIED;
if(levelComplete == true){
increaseScore(bonusPts);
return GWSTATUS_FINISHED_LEVEL;
}
//TODO: IMPLEMENT IF PLAYER COMPLETES LEVEL
}
}
m_avatar -> doSomething();
removeDeadGameObjects();
if(bonusPts > 0) bonusPts--;
list<Actor*>::iterator it2;
if(!anyCrystals()){
for(it2 = m_actors.begin(); it2 != m_actors.end(); it2++){
if((*it2)->isExit())
(*it2)->setActiveState(true);
}
}
//TODO: UPDATE STATUS
setDisplayText();
return GWSTATUS_CONTINUE_GAME;
}
void StudentWorld::cleanUp()
{
delete m_avatar;
m_avatar = nullptr;
list<Actor*>::iterator it;
for(it = m_actors.begin(); it != m_actors.end(); it++){
delete (*it);
}
m_actors.clear();
}
//----------------------------------------------------------HELPER FUNCTIONS----------------------------------------------------------
//ACTOR AT X,Y AND BLOCKS MOVEMENT
bool StudentWorld::actorIsBlockingAtXY(double x, double y)
{
list<Actor*>::iterator it;
if(m_avatar->getX() == x && m_avatar->getY() == y)
return true;
for(it = m_actors.begin(); it != m_actors.end(); it++)
{
if((*it)->getX() == x && (*it)->getY() == y && (*it)-> blocksMovement()){
return true;
}
else ;
}
return false;
}
bool StudentWorld::actorAtXYTakesPeaHit(double x, double y){
if(actorAt(x, y) != *m_actors.end()){
if(actorAt(x, y) -> takesPeaHit()) return true;
}
return false;
}
//ACTOR AT X,Y BUT DOESN'T BLOCK MOVEMENT
bool StudentWorld::actorNotBlockingAtXY(double x, double y)
{
list<Actor*>::iterator it;
if(m_avatar->getX() == x && m_avatar->getY() == y)
return true;
for(it = m_actors.begin(); it != m_actors.end(); it++)
{
if((*it)->getX() == x && (*it)->getY() == y && !(*it) -> blocksMovement()){
if((*it)->isExit())
return false;
else return true;
}
else ;
}
return false;
}
Actor* StudentWorld::actorAt(double x, double y)
{
list<Actor*>::iterator it;
if(m_avatar->getX() == x && m_avatar->getY() == y)
return m_avatar;
for(it = m_actors.begin(); it != m_actors.end(); it++)
{
if((*it)->getX() == x && (*it)->getY() == y ){
if((*it)->blocksMovement())
return *it;
}
}
return *m_actors.end();
}
Actor* StudentWorld::goodieAt(double x, double y){
list<Actor*>::iterator it;
if(m_avatar->getX() == x && m_avatar->getY() == y)
return m_avatar;
for(it = m_actors.begin(); it != m_actors.end(); it++)
{
if((*it)->getX() == x && (*it)->getY() == y ){
if(!(*it)->blocksMovement())
return *it;
}
}
return *m_actors.end();
}
bool StudentWorld::actorAtXYisGoodie(double x, double y){
if(actorNotBlockingAtXY(x, y)){
if(goodieAt(x, y)->isGoodie()){
return true;
}
}
return false;
}
void StudentWorld::removeDeadGameObjects()
{
list<Actor*>::iterator it;
for(it = m_actors.begin(); it != m_actors.end(); it++)
{
if((*it)->isActive() == false)
{
(*it) ->setVisible(false);
delete *it;
it = m_actors.erase(it);
}
}
}
string StudentWorld::format(int score, int level, int lives, int health, int ammo, int bonus)
{
string formattedOutput = "";
ostringstream scoreOSS, levelOSS, livesOSS, healthOSS, ammoOSS, bonusOSS;
scoreOSS << setw(7) << setfill('0') << score;
levelOSS << setw(2) << setfill('0') << level;
livesOSS << setw(2) << lives;
healthOSS << setw(3) << health;
ammoOSS << setw(3) << ammo;
bonusOSS << setw(4) << bonus;
formattedOutput += "Score: " + scoreOSS.str() + " Level: " + levelOSS.str() + " Lives: " + livesOSS.str() + " Health:" + healthOSS.str() + "%" + " Ammo: " + ammoOSS.str() + " Bonus: " + bonusOSS.str();
return formattedOutput;
}
void StudentWorld::setDisplayText()
{
int score = getScore();
int level = getLevel();
unsigned int bonus = getBonus();
int lives = getLives();
int health = m_avatar->getHealth();
int ammo = m_avatar->getAmmo();
string s = format(score, level, lives, health, ammo, bonus);
setGameStatText(s);
}
void StudentWorld::addPea(int x, int y, int dir) { m_actors.push_front(new Pea(this, x, y, dir)); }
bool StudentWorld::anyCrystals() const{
if(m_numCrystals > 0) return true;
return false;
}
void StudentWorld::decCrystals(){
m_numCrystals--;
}
int StudentWorld::getAvatarDir(){
return m_avatar->getDirection();
}
void StudentWorld::addAmmoGoodie(double x, double y) { m_actors.push_front(new AmmoGoodie(this, x, y));}
void StudentWorld::addHealthGoodie(double x, double y) { m_actors.push_front(new RestoreHealthGoodie(this, x, y)); }
void StudentWorld::addLifeGoodie(double x, double y){
m_actors.push_front(new ExtraLifeGoodie(this, x, y));
}
void StudentWorld::addThiefBot(double x, double y){
list<Actor*>::iterator it = m_actors.begin();
while(!(*it)->isFactory())
it++;
m_actors.insert(it, new ThiefBot(this, x, y, 5, 10, IID_THIEFBOT));
playSound(SOUND_ROBOT_BORN);
}
void StudentWorld::addMeanThiefBot(double x, double y){
list<Actor*>::iterator it = m_actors.begin();
while(!(*it)->isFactory())
it++;
m_actors.insert(it, new MeanThiefBot(this, x, y));
playSound(SOUND_ROBOT_BORN);
}
int StudentWorld::getBonus() { return bonusPts; }
double StudentWorld::getAvatarX() { return m_avatar -> getX(); }
double StudentWorld::getAvatarY() { return m_avatar -> getY(); }
void StudentWorld::restorePlayerHealth() { m_avatar->restoreHealth(); }
void StudentWorld::restorePlayerPeas() { m_avatar-> restorePeas(); }
void StudentWorld::setLevelComplete(){ levelComplete = true; }