-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actor.cpp
466 lines (406 loc) · 9.88 KB
/
Actor.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#include "Actor.h"
#include "StudentWorld.h"
#include <iostream>
using namespace std;
// Students: Add code to this file, Actor.h, StudentWorld.h, and StudentWorld.cpp
double toRadians(int degrees);
int toDegrees(int radians);
double findAngleRadians(double x1, double y1, double x2, double y2, int add = 0);
//ACTOR===========================================
Actor::Actor(int imageID, double startX, double startY, StudentWorld* sw, int dir, int depth, int health)
:GraphObject(imageID, startX, startY, dir, depth) {
m_health = health;
m_gw = sw;
}
Actor::~Actor() {
}
void Actor::updateHealth(int health) {
m_health += health;
}
int Actor::getHealth() const {
return m_health;
}
StudentWorld* Actor::getWorld() const {
return m_gw;
}
bool Actor::isAlive() const {
return m_health > 0;
}
//SOCRATES=======================================
Socrates::Socrates(StudentWorld* sw)
:Actor(IID_PLAYER, 0, VIEW_HEIGHT / 2, sw, 0, 0, 100) {
m_sprayCharge = 20;
m_flameCharge = 5;
m_didNothing = false;
}
//SOCRATES=========
Socrates::~Socrates() {
}
void Socrates::doSomething() {
if (!this->isAlive()) {
return;
}
int ch;
if (getWorld()->getKey(ch)) {
m_didNothing = false;
//double x = 0;
//double y = 0;
//double angle = 0;
//switch (ch) {
//case 'a':
//case 'w':
//case KEY_PRESS_UP:
//case KEY_PRESS_LEFT:
// angle = findAngleRadians(getX(), getY(), VIEW_WIDTH / 2, VIEW_HEIGHT / 2, 5);
// x = VIEW_WIDTH / 2 + 128 * cos(angle);
// y = VIEW_HEIGHT / 2 + 128 * sin(toRadians(angle));
// cout << x << " " << y << endl;
// if (getDirection() == 0) {
// setDirection(359);
// }
// else {
// setDirection(getDirection() + 5);
// }
// moveTo(x, y);
// break;
//case 'd':
//case 's':
//case KEY_PRESS_DOWN:
//case KEY_PRESS_RIGHT:
// findAngleRadians(getX(), getY(), VIEW_WIDTH / 2, VIEW_HEIGHT / 2, -5);
// x = VIEW_WIDTH / 2 + 128 * cos(toRadians(angle));
// y = VIEW_HEIGHT / 2 + 128 * sin(toRadians(angle));
// if (getDirection() == 359) {
// setDirection(0);
// }
// else {
// setDirection(getDirection() - 5);
// }
// moveTo(x, y);
double xDis = getX() - VIEW_RADIUS;
double yDis = getY() - (VIEW_HEIGHT / 2);
double angleFromPosXaxis = toRadians(90);
if (xDis != 0)
angleFromPosXaxis = atan(yDis / xDis);
//quadrant to angle determination
int quad = 0;
if (xDis == 0) {
if (yDis >= 0) {
quad = 2;
}
else {
quad = 4;
}
}
if ((yDis / xDis) >= 0) {
if (xDis >= 0) {
quad = 0;
}
else {
quad = 2;
}
}
else {
if (xDis > 0) {
quad = 4;
}
else {
quad = 2;
}
}
double newX = 0;
double newY = 0;
switch (ch) {
case 'a':
case 'w':
case KEY_PRESS_UP:
case KEY_PRESS_LEFT:
//(getDirection() < toRadians(90)) ? GraphObject::moveAngle(toRadians(359 - (90 - getDirection()))) : GraphObject::moveAngle(toRadians(getDirection() - 90));
//cout << (128 * (cos(angleFromPosXaxis + toRadians(quad * 90.0 - 1)))) << endl;
newX = VIEW_WIDTH / 2 + (128 * (cos(angleFromPosXaxis + toRadians(quad * 90.0 + 5))));
newY = VIEW_HEIGHT / 2 + (128 * sin((angleFromPosXaxis + toRadians(quad * 90.0 + 5))));
if (getDirection() == 0) {
setDirection(359);
}
else {
setDirection(getDirection() + 5);
}
moveTo(newX, newY);
break;
case 'd':
case 's':
case KEY_PRESS_DOWN:
case KEY_PRESS_RIGHT:
newX = VIEW_WIDTH / 2 + (128 * (cos(angleFromPosXaxis + toRadians(quad * 90.0 - 5))));
newY = VIEW_HEIGHT / 2 + (128 * sin((angleFromPosXaxis + toRadians(quad * 90.0 - 5))));
if (getDirection() == 359) {
setDirection(0);
}
else {
setDirection(getDirection() - 5);
}
moveTo(newX, newY);
break;
case KEY_PRESS_SPACE:
if (m_sprayCharge > 0) {
Actor* temp = new Spray(getX(), getY(), getWorld(), getDirection());
getWorld()->addActor(temp);
getWorld()->playSound(SOUND_PLAYER_SPRAY);
m_sprayCharge--;
}
break;
case KEY_PRESS_ENTER:
if (m_flameCharge > 0) {
for (int i = -7; i < 8; i++) {
int angle = getDirection() + 22 * i;
if (angle < 0) {
angle += 359;
}
else if (angle > 359) {
angle -= 359;
}
Actor* temp = new Flames(getX(), getY(), getWorld(), angle);
getWorld()->addActor(temp);
}
getWorld()->playSound(SOUND_PLAYER_FIRE);
m_flameCharge--;
}
break;
}
}
else if (m_didNothing) {
if (m_sprayCharge < 20) {
m_sprayCharge++;
}
}
else {
m_didNothing = true;
}
}
int Socrates::getSpray() const {
return m_sprayCharge;
}
int Socrates::getFlames() const {
return m_flameCharge;
}
void Socrates::addFlames() {
m_flameCharge += 5;
}
//BACTERIA===========
Bacteria::Bacteria(int id, double startX, double startY, StudentWorld* sw, int health)
: Actor(id, startX, startY, sw, 90, 0, health) {
}
int Bacteria::getFood() const {
return m_foodEaten;
}
int Bacteria::getMove() const {
return m_ovementPlan;
}
void Bacteria::newAngle() {
}
void Bacteria::setFood(int change) {
m_foodEaten += change;
}
void Bacteria::move(int change) {
m_ovementPlan += change;
}
RegSalmon::RegSalmon(double startX, double startY, StudentWorld* sw)
: Bacteria(IID_SALMONELLA, startX, startY, sw, 4) {
}
void RegSalmon::doSomething() {
if (!isAlive()) {
return;
}
if (getWorld()->interact(this) == 1) {
}
else if (getFood() == 3) {
int newX = getX() + (getX() < VIEW_WIDTH / 2) ? SPRITE_RADIUS : getX() == VIEW_WIDTH / 2 ? 0 : -SPRITE_RADIUS;
int newY = getY() + (getY() < VIEW_HEIGHT / 2) ? SPRITE_RADIUS : getY() == VIEW_HEIGHT / 2 ? 0 : -SPRITE_RADIUS;
getWorld()->addActor(new RegSalmon(newX, newY, getWorld()));
setFood(-3);
}
else if (getWorld()->interact(this) == 0) {
setFood(-1);
}
if (getMove() > 0) {
move(-1);
if (getWorld()->interact(this) == 2) {
newAngle();
return;
}
cout << "new angle" << endl;
moveAngle(getDirection(), 1);
}
else {
int x = -1;
int y = -1;
getWorld()->findFood(this, x, y);
if (x == -1 || y == -1) {
cout << "yes" << endl;
newAngle();
return;
}
else {
if (getWorld()->interact(this) == 2) {
cout << " no" << endl;
newAngle();
}
}
}
}
AggSalmon::AggSalmon(double startX, double startY, StudentWorld* sw)
: Bacteria(IID_SALMONELLA, startX, startY, sw, 10) {
}
void AggSalmon::doSomething() {
return;
}
Ecoli::Ecoli(double startX, double startY, StudentWorld* sw)
: Bacteria(IID_SALMONELLA, startX, startY, sw, 5) {
}
void Ecoli::doSomething() {}
//PIT================
Pit::Pit(double startX, double startY, StudentWorld* sw)
: Actor(IID_PIT, startX, startY, sw, 0, 1, 1) {
m_aggroSalmon = 3;
m_regSalmon = 5;
m_Ecoli = 2;
m_total = m_aggroSalmon + m_regSalmon + m_Ecoli;
}
void Pit::doSomething() {
}
//PROJECTILES==========
Projectile::Projectile(int imageID, double startX, double startY, StudentWorld* sw, int dir)
:Actor(imageID, startX, startY, sw, dir, 1, 1) {
m_distance = 1;
}
void Projectile::doSomething() {
if (!isAlive()) {
return;
}
if (getDistance() > 0 && getHealth() > 0) {
if (getWorld()->interact(this) == 1) {
updateHealth(-1);
}
setDistance(getDistance() - 1);
moveAngle(getDirection(), 1);
}
else if (getDistance() == 0) {
updateHealth(-1);
}
return;
}
void Projectile::setDistance(int dist) {
m_distance = dist;
}
int Projectile::getDistance() const {
return m_distance;
}
//spray
Spray::Spray(double startX, double startY, StudentWorld* sw, int dir)
:Projectile(IID_SPRAY, startX, startY, sw, dir) {
setDistance(120);
}
//flames
Flames::Flames(double startX, double startY, StudentWorld* sw, int dir)
:Projectile(IID_FLAME,startX,startY, sw, dir) {
setDistance(32);
}
//Goodie
Goodie::Goodie(int ID, double startX, double startY, StudentWorld* sw, int life, int score)
: Actor(ID, startX, startY, sw, 0, 1, life) {
m_score = score;
}
void Goodie::doSomething() {
if (!isAlive())
return;
if (getWorld()->interact(this) == 1) {
if (score() != -50) {
getWorld()->playSound(SOUND_GOT_GOODIE);
}
else if (score() == -50 && getWorld()->getScore() < 50) {
updateHealth(-getHealth());
}
else {
getWorld()->increaseScore(score());
updateHealth(-getHealth());
}
}
if (getHealth() > 0) {
updateHealth(-1);
}
}
int Goodie::score() const {
return m_score;
}
//Health goodie
HealthGoodie::HealthGoodie(double startX, double startY, StudentWorld* sw, int life)
: Goodie(IID_RESTORE_HEALTH_GOODIE, startX, startY, sw, life, 250) {
}
//Flame
FlameGoodie::FlameGoodie(double startX, double startY, StudentWorld* sw, int life)
: Goodie(IID_FLAME_THROWER_GOODIE, startX, startY, sw, life, 300) {
}
//Extra Life
ExtraLife::ExtraLife(double startX, double startY, StudentWorld* sw, int life)
: Goodie(IID_EXTRA_LIFE_GOODIE, startX, startY, sw, life, 500) {
}
//Fungus
Fungus::Fungus(double startX, double startY, StudentWorld* sw, int life)
: Goodie(IID_FUNGUS, startX, startY, sw, life, -50) {
}
//FOOD======================
Food::Food(double startX, double startY, StudentWorld* sw)
: Actor(IID_FOOD, startX, startY, sw, 0 , 1, 1) {
}
void Food::doSomething() {
return;
}
//DIRT==========================
Dirt::Dirt(double startX, double startY, StudentWorld* sw)
:Actor(IID_DIRT, startX, startY, sw, 0, 1, 1) {
}
void Dirt::doSomething() {
return;
}
//HELPER FUNCTIONS =========================
double toRadians(int degrees) {
const double PI = 4 * atan(1);
return degrees * (PI / 180);
}
int toDegrees(int radians) {
const double PI = 4 * atan(1);
return round(radians * (180.0 / PI));
}
double findAngleRadians(double x1, double y1, double x2, double y2, int add) {
double xDis = x2-x1;
double yDis = y2 - y1;
double angleFromPosXaxis = toRadians(90);
if (xDis != 0)
angleFromPosXaxis = atan(yDis / xDis);
int quad = 0;
if (xDis == 0) {
if (yDis >= 0) {
quad = 2;
}
else {
quad = 4;
}
}
if ((yDis / xDis) >= 0) {
if (xDis >= 0) {
quad = 0;
}
else {
quad = 2;
}
}
else {
if (xDis > 0) {
quad = 4;
}
else {
quad = 2;
}
}
return angleFromPosXaxis + toRadians(quad * 90.0 + add);
}