-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentWorld.cpp
351 lines (302 loc) · 11.4 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "StudentWorld.h"
#include "GameConstants.h"
#include <string>
#include <iostream> // defines the overloads of the << operator
#include <sstream> // defines the type std::ostringstream
#include <iomanip> // defines the manipulator setw
#include "Actor.h"
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::StudentWorld(string assetPath)
: GameWorld(assetPath)
{
}
StudentWorld::~StudentWorld(){
cleanUp();
}
int StudentWorld::init()
{
//intialization stuff
m_player = new Socrates(this);
int numFoods = min(5*getLevel(), 25);
int numDirts = max(180 - 20*getLevel(), 20);
placeObjRand(getLevel(), obj_PIT);
placeObjRand(numFoods, obj_FOOD);
placeObjRand(numDirts, obj_DIRT);
m_pitsDone = getLevel();
m_bacteriaLeft = 0;
return GWSTATUS_CONTINUE_GAME;
}
int StudentWorld::move()
{
ostringstream oss;
// This code is here merely to allow the game to build, run, and terminate after you hit enter.
// Notice that the return value GWSTATUS_PLAYER_DIED will cause our framework to end the current level.
//call doSomethings
for(int i = 0; i < m_actors.size(); i++){
if(m_actors[i]->getExistStatus()){
m_actors[i]->doSomething();
}
if(getNumBacteriaLeft() == 0 && getPitsLeft() == 0){
playSound(SOUND_FINISHED_LEVEL);
return GWSTATUS_FINISHED_LEVEL;
}
if(!m_player->getExistStatus()){
decLives();
return GWSTATUS_PLAYER_DIED;
}
}
//deletes dead objs
for(int i = 0; i < m_actors.size(); i++){
if(!m_actors[i]->getExistStatus()){
delete m_actors[i];
m_actors[i] = m_actors[m_actors.size()-1];
m_actors.pop_back();
i--;
}
}
m_player->doSomething();
//sets up goodie spawning probabilities
int chanceFungus = max(510-getLevel()*10, 200);
int chanceGoodie = max(510-getLevel()*10, 250);
int randFungus = randInt(0, chanceFungus-1);
int randAngle = randInt(0, 359);
double randXGoodie = VIEW_WIDTH/2;
double randYGoodie = VIEW_HEIGHT/2;
int lifeTime = max(randInt(0,(300-10 * getLevel()-1)), 50);
if(randFungus <= 0){
getCoordsForLocalCircRelativeToOrigin(randXGoodie, randYGoodie, randAngle, VIEW_RADIUS);
m_actors.push_back(new Fungus(randXGoodie, randYGoodie, lifeTime, this));
}
int randomGoodie = randInt(0, chanceGoodie-1);
if(randomGoodie == 0){
randAngle = randInt(0, 359);
lifeTime = max(randInt(0, (300-10 * getLevel() - 1)), 50);
int randGoodie = randInt(0, 9);
getCoordsForLocalCircRelativeToOrigin(randXGoodie, randYGoodie, randAngle, VIEW_RADIUS);
if(randGoodie <= 0)
m_actors.push_back(new ExtraLife(randXGoodie, randYGoodie, lifeTime, this));
else if(randGoodie <= 3)
m_actors.push_back(new FlameThrowerGoodie(randXGoodie, randYGoodie, lifeTime, this));
else
m_actors.push_back(new RestoreHealth(randXGoodie, randYGoodie, lifeTime, this));
}
//sets up scoreboard
oss.fill('0');
oss << setw(6) << getScore();
string boardText = "Score: " + oss.str() + " Level: " + to_string(getLevel()) + " Lives: " + to_string(getLives()) + " Health: ";
boardText += to_string(m_player->getExistStatus()) + " Sprays: " + to_string(m_player->getNSprays()) + " Flames: " + to_string(m_player->getNFlames());
/* Score: 004500 Level: 4 Lives: 3 health: 82 Sprays: 16 Flames: 4 */
setGameStatText(boardText);
return GWSTATUS_CONTINUE_GAME;
}
void StudentWorld::cleanUp()
{
delete m_player;
m_player = nullptr;
for(long i = m_actors.size()-1; i >= 0; i--){
delete m_actors[i];
m_actors.pop_back();
}
}
int StudentWorld::getNumBacteriaLeft(){
return m_bacteriaLeft;
}
//returns true if there is overlap
bool StudentWorld::checkOverlap(double x1, double x2, double y1, double y2, int radius){
double dist = sqrt(pow(x2-x1,2) + pow(y2-y1,2));
return dist <= radius;
}
//returns true if objects were damaged
bool StudentWorld::deleteDamagableObjects(double testX, double testY, int dmg){
for(int i = 0; i < m_actors.size(); i++){
if(checkOverlap(m_actors[i]->getX(), testX, m_actors[i]->getY(), testY, SPRITE_WIDTH)){
if(m_actors[i]->getDamagedByFlame() && m_actors[i]->getExistStatus()){
//deal 5 damage to everyone
int curr = m_actors[i]->getExistStatus();
m_actors[i]->setExistStatus(curr-dmg);
return true;
}
}
}
return false;
}
void StudentWorld::createRingOfFire(){
for(int i = 0; i < 16; i++){
Direction startInt = m_player->getDirection() + i*22;
double start = startInt % 360;
double startX, startY;
//startX = m_player->getX() + cos(2.0*M_PI*start/360.0) * 2*SPRITE_RADIUS;
//startY = m_player->getY() + sin(2*M_PI*start/360.0) * 2*SPRITE_RADIUS;
startX = m_player->getX();
startY = m_player->getY();
getCoordsForLocalCircRelativeToOrigin(startX, startY, start, SPRITE_WIDTH);
m_actors.push_back(new Flame(startX, startY, start, this));
}
//cout << m_actors[m_actors.size()-1-i]->getDirection() << endl;
}
void StudentWorld::shootSpray(){
double startX, startY;
m_player->getPositionInThisDirection(m_player->getDirection(), SPRITE_WIDTH, startX, startY);
m_actors.push_back(new Spray(startX, startY, m_player->getDirection(), this));
}
bool StudentWorld::checkOverlapWithSocrates(double x, double y){
return checkOverlap(x, m_player->getX(), y, m_player->getY(), SPRITE_WIDTH);
}
void StudentWorld::restoreHealth(){
m_player->setExistStatus(100);
}
void StudentWorld::damageSocrates(int amt){
m_player->setExistStatus(m_player->getExistStatus() - amt);
}
void StudentWorld::addFlameThrowers(){
m_player->addFlames();
}
void StudentWorld::setPitsLeft(int set){
m_pitsDone = set;
}
int StudentWorld::getPitsLeft(){
return m_pitsDone;
}
bool StudentWorld::eatFoodIfThere(double bactLocX, double bactLocY){
for(int i = 0; i < m_actors.size(); i++){
//food fullfills these categories
if(m_actors[i]->attractsBacteria()){
if(checkOverlap(bactLocX, m_actors[i]->getX(), bactLocY, m_actors[i]->getY(), SPRITE_WIDTH)){
m_actors[i]->setExistStatus(false);
return true;
}
}
}
return false;
}
bool StudentWorld::overlapWithDirt(double bactLocX, double bactLocY){
for(int i = 0; i < m_actors.size(); i++){
if(m_actors[i]->getDamagedByFlame() && !m_actors[i]->isAnimate()){
//check overlap with dirt so different radius apparently
if(checkOverlap(bactLocX, m_actors[i]->getX(), bactLocY, m_actors[i]->getY(), SPRITE_WIDTH/2)){
return true;
}
}
}
return false;
}
bool StudentWorld::notWithinCircle(double newX, double newY, int rad){
//adjusts the points so that it is centered around a circle of given radius at the center of the game and does necessary calculations based off of this
double centeredX = newX - VIEW_WIDTH/2;
double centeredY = newY - VIEW_HEIGHT/2;
double r = centeredX*centeredX + centeredY*centeredY;
r = sqrt(r);
return r >= rad;
}
int StudentWorld::getDistBetween(double x1, double x2, double y1, double y2){
return sqrt(pow(x2-x1,2) + pow(y2-y1,2));
}
int StudentWorld::getDirToClosestFood(double bactLocX, double bactLocY){
int closestDir = -1;
for(int i = 0; i < m_actors.size(); i++){
if(m_actors[i]->attractsBacteria()){
if(getDistBetween(bactLocX, m_actors[i]->getX(), bactLocY, m_actors[i]->getY()) > 128)
continue;
int tempDir = getDirBetween( bactLocX, m_actors[i]->getX(), bactLocY, m_actors[i]->getY());
if(closestDir == -1 || closestDir > tempDir)
closestDir = tempDir;
}
}
return closestDir;
}
bool StudentWorld::checkIfSocratesNearby(double x, double y, int& distMax){
int temp = distMax;
distMax = getDirBetween( x, m_player->getX(), y, m_player->getY());
return getDistBetween(x, m_player->getX(), y, m_player->getY()) <= temp;
}
void StudentWorld::bacteriaDied(){
m_bacteriaLeft--;
}
//Private functions
//uses trigonometry to find correct direction pointing to x2,y2.
int StudentWorld::getDirBetween(double x1, double x2, double y1, double y2){
//relative to the player so x2,y2 is bacteria (player is reference point)
int dir = 360.0*atan(abs((y2-y1)/(x2-x1)))/(2.0*M_PI);
if(x2 - x1 < 0){
//quad 3
if(y2 - y1 < 0){
return 180 + dir;
}
//quad 2
else{
return 180 - dir;
}
}
else{
//quad 4
if(y2 - y1 < 0){
return 360 - dir;
}
//quad 1
//do nothing
}
return dir;
}
bool StudentWorld::overlapWithOtherObjects(double testX, double testY){
if(checkOverlapWithSocrates(testX, testY))
return true;
for(int i = 0; i < m_actors.size(); i++){
if(!m_actors[i]->getDamagedByFlame() || m_actors[i]->isAnimate()){
if(checkOverlap(testX, m_actors[i]->getX(), testY, m_actors[i]->getY(), SPRITE_WIDTH)){
return true;
}
}
}
return false;
}
void StudentWorld::placeObjRand(int numObj, int objType){
double randX, randY;
for(int i = 0; i < numObj; i++){
randX = randInt(0, VIEW_WIDTH);
randY = randInt(0, VIEW_HEIGHT);
if(notWithinCircle(randX, randY, 120) || overlapWithOtherObjects(randX, randY)){
i--;
continue;
}
createObj(randX, randY, objType);
}
}
//NOTE: This does not violate the spec since it isn't determining the type of any object using image ID. It is merely creating a new object of a desired type which is allowed.
void StudentWorld::createObj(int x, int y, int objType){
switch (objType) {
case obj_REGSAL:
m_bacteriaLeft++;
m_actors.push_back(new RegSalmonella(x, y, this));
break;
case obj_AGGSAL:
m_bacteriaLeft++;
m_actors.push_back(new AggSalmonella(x, y, this));
break;
case obj_ECOLI:
m_bacteriaLeft++;
m_actors.push_back(new EColi(x, y, this));
break;
case obj_PIT:
m_actors.push_back(new Pit(x, y, this));
break;
case obj_DIRT:
m_actors.push_back(new Dirt(x, y, this));
break;
case obj_FOOD:
m_actors.push_back(new Food(x, y, this));
break;
default:
cerr << "something wrong in createObj" << endl;
break;
}
}
//this function is used a lot since you often need to get information for the local points that are the radial distance away from another point. Basically it does some trigonemtry and returns points in a circle centered at any point you want (localX, localY)
void StudentWorld::getCoordsForLocalCircRelativeToOrigin(double& localX, double& localY, double localAngleDeg, double radius){
localX = localX + cos(2.0*M_PI*localAngleDeg/360.0) *radius;
localY = localY + sin(2.0*M_PI*localAngleDeg/360.0) *radius;
}