-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentWorld.cpp
More file actions
203 lines (182 loc) · 6.07 KB
/
StudentWorld.cpp
File metadata and controls
203 lines (182 loc) · 6.07 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
#include "StudentWorld.h"
#include "GameConstants.h"
#include <string>
#include <iostream>
using namespace std;
GameWorld* createStudentWorld(string assetPath)
{
return new StudentWorld(assetPath);
}
// Students: Add code to this file, StudentWorld.h, Actor.h, and Actor.cpp
//StudentWorld Definitions
StudentWorld::StudentWorld(string assetPath)
: GameWorld(assetPath) {
m_Avatar = nullptr;
levelCompleted = false;
}
void StudentWorld::cleanUp() {
//Clear newd pointers in vector
while (!m_Container.empty()) {
delete m_Container[0];
m_Container.erase(m_Container.begin());
}
delete m_Avatar; //Destruct avatar
m_Avatar = nullptr;
}
StudentWorld::~StudentWorld() {
cleanUp();
}
bool StudentWorld::isLevelCompleted() const { return levelCompleted; }
void StudentWorld::removeDeadGameObjects() {
for (int i = 0; i < m_Container.size(); i++) {
if (m_Container[i] != nullptr &&
!m_Container[i]->isAlive()) {
delete m_Container[i];
m_Container.erase(m_Container.begin() + i);
}
}
}
string StudentWorld::levelToString() {
if (getLevel() < 10) {
if (getLevel() == 1) {
int a = getLevel();
}
return "0" + to_string(getLevel());
}
else {
return to_string(getLevel());
}
}
int StudentWorld::init() {
//Initialize currentLevelBonus & containerSize
currentLevelBonus = 1000;
levelCompleted = false;
string curLevel = "level" + levelToString() + ".txt";
Level lev(assetPath());
Level::LoadResult result = lev.loadLevel(curLevel);
if (result == Level::load_fail_file_not_found ||
result == Level::load_fail_bad_format) {
return GWSTATUS_LEVEL_ERROR;
}
for (int x = 0; x <= VIEW_WIDTH - 1; x++) {
for (int y = 0; y <= VIEW_HEIGHT - 1; y++) {
Level::MazeEntry item = lev.getContentsOf(x, y);
switch (item) {
case Level::player:
m_Avatar = new Avatar(x, y, this);
break;
case Level::vert_ragebot:
m_Container.push_back(new VerticalRageBot(this, x, y));
break;
case Level::horiz_ragebot:
m_Container.push_back(new HorizontalRageBot(this, x, y));
break;
case Level::wall:
m_Container.push_back(new Wall(x,y, this));
break;
case Level::crystal:
m_Container.push_back(new Crystal(this, x, y));
break;
case Level::ammo:
m_Container.push_back(new AmmoGoodie(this, x, y));
break;
case Level::extra_life:
m_Container.push_back(new ExtraLifeGoodie(this, x, y));
break;
case Level::restore_health:
m_Container.push_back(new RestoreHealthGoodie(this, x, y));
break;
case Level::exit:
m_Container.push_back(new Exit(this, x, y));
break;
case Level::marble:
m_Container.push_back(new Marble(this, x, y));
break;
case Level::pit:
m_Container.push_back(new Pit(this, x, y));
break;
case Level::thiefbot_factory:
m_Container.push_back(new ThiefBotFactory(this, x, y, ThiefBotFactory::ProductType::REGULAR));
break;
case Level::mean_thiefbot_factory:
m_Container.push_back(new ThiefBotFactory(this, x, y, ThiefBotFactory::ProductType::MEAN));
break;
case Level::empty:
break;
default:
break;
}
}
}
return GWSTATUS_CONTINUE_GAME;
}
int StudentWorld::move()
{
setDisplayText();
//Give each actor a chance to do something
//Check Avatar health -> doSomething()
if (m_Avatar->isAlive()) {
m_Avatar->doSomething();
}
else {
delete m_Avatar;
m_Avatar = nullptr;
decLives();
return GWSTATUS_PLAYER_DIED;
}
for (int i = 0; i < m_Container.size(); i++) {
if (m_Container[i] != nullptr && m_Container[i]->isAlive()) {
m_Container[i]->doSomething();
if (!m_Container[i]->isAlive()) {
delete m_Container[i];
m_Container.erase(m_Container.begin() + i);
}
if (!m_Avatar->isAlive()) {
delete m_Avatar;
m_Avatar = nullptr;
decLives();
return GWSTATUS_PLAYER_DIED;
}
if (isLevelCompleted()) {
return GWSTATUS_FINISHED_LEVEL; //CHECK FOR COMPLETING LEVEL
}
}
else {
delete m_Container[i];
m_Container.erase(m_Container.begin() + i);
}
}
//Remove Newly Dead actors
removeDeadGameObjects();
if (!m_Avatar->isAlive()) {
delete m_Avatar;
m_Avatar = nullptr;
return GWSTATUS_PLAYER_DIED;
}
if (currentLevelBonus > 0) {
currentLevelBonus--;
}
//check player/maze location
if (isLevelCompleted()) {
return GWSTATUS_FINISHED_LEVEL;
}
return GWSTATUS_CONTINUE_GAME;
}
void StudentWorld::setDisplayText() {
int score = getScore();
int level = getLevel();
int livesLeft = getLives();
double health = double(m_Avatar->getHP());
int ammo = m_Avatar->getAmmo(); //figure out how to keep track of ammo
unsigned int bonus = getCurrentLevelBonus();
string s = formatDisplayText(score, level, livesLeft, health, ammo, bonus);
setGameStatText(s);
}
string StudentWorld::formatDisplayText(int score, int level, int lives, double health, int ammo, unsigned int bonus) {
double healthBar = (health / 20) * 100;
int healthPercent = int(healthBar);
string s = "Score: " + to_string(score) + " Level: " + to_string(level)
+ " Lives: " + to_string(lives) + " Health: " + to_string(healthPercent) + "%"
+ " Ammo: " + to_string(ammo) + " Current Level Bonus: " + to_string(bonus);
return s;
}