-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actor.cpp
441 lines (404 loc) · 11.8 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
#include "Actor.h"
#include "StudentWorld.h"
#include "GameConstants.h"
#include <cmath>
using namespace std;
void BlockingObjects::isBonked() {
/*
virtual function of isBonked
play bonk sound when bonked
*/
getStudentWorld()->playSound(SOUND_PLAYER_BONK);
}
void Block::isBonked() {
/*
if not holding goodie,
play bonk sound when bonked
if holding goodie, release that goodie and play a sound
*/
if (m_goodie <= 0) {
// no goodie
getStudentWorld()->playSound(SOUND_PLAYER_BONK);
}
else {
getStudentWorld()->playSound(SOUND_POWERUP_APPEARS);
switch (m_goodie) {
case 1: // in actor.h, 1 indicates startGoodie
getStudentWorld()->newGoodie(getX(), getY()+8, 1);
break;
case 2: // 2 indicates flowerGoodie
getStudentWorld()->newGoodie(getX(), getY()+8, 2);
break;
case 3: // 3 indicates mushroomGoodie
getStudentWorld()->newGoodie(getX(), getY()+8, 3);
break;
default:
break;
}
m_goodie = 0; // clear the goodie that the block is holding
}
}
void Peach::isBonked() {
if (m_starPower > 0 || m_temporaryInvincibility > 0) {
// has star power does nothing
}
else {
m_hitPoint--;
m_temporaryInvincibility = 10;
m_shootPower = false;
m_jumPower = false;
if (m_hitPoint >= 1) {
getStudentWorld()->playSound(SOUND_PLAYER_HURT);
}
if (m_hitPoint <= 0) {
setDead();
}
}
}
void Peach::isDamaged() {
isBonked(); // does the same lol
}
void Peach::setHitPoint(int point) {
m_hitPoint = point;
}
void Peach::gainPower(int type) {
/*
starGoodie = 1 --> starPower
flowerGoodie = 2 --> shootPower
mushroomGoodie = 3 --> jumpPower
*/
switch (type)
{
case 1:
m_starPower += 100;
break;
case 2:
m_shootPower = true;
break;
case 3:
m_jumPower = true;
break;
default:
break;
}
}
// Students: Add code to this file, Actor.h, StudentWorld.h, and StudentWorld.cpp
void Peach::doSomething() {
int ch;
if (!isAlive()) {
return;
}
if (m_starPower > 0) {
m_starPower--;
}
if (m_temporaryInvincibility > 0) {
m_temporaryInvincibility--;
}
if (m_timeToRecharge > 0) {
m_timeToRecharge--;
}
if (getStudentWorld()->isOverlap(getX(), getY())) {
if (getStudentWorld()->bonk(getX(), getY()) ||
getStudentWorld()->bonk(getX()+7, getY())) {
// left or right
isBonked();
}
}
if (m_rJumpDistance > 0) {
double x = getX();
double y = getY()+4;
if (getStudentWorld()->isBlockingObjectAt(x, getY()+9) || getStudentWorld()->isBlockingObjectAt(x+4, getY()+9)) {
// if something above his head
getStudentWorld()->bonk(x, getY()+12);
m_rJumpDistance = 0;
return;
}
else {
this->moveTo(x, y);
m_rJumpDistance--;
}
}
else {
if (getStudentWorld()->isBlockingObjectAt(getX(), getY()) ||
getStudentWorld()->isBlockingObjectAt(getX(), getY()-3) ||
getStudentWorld()->isBlockingObjectAt(getX()+4, getY()) ||
getStudentWorld()->isBlockingObjectAt(getX()+4, getY()-3))
{
// has something under feet, does nothing
}
else {
moveTo(getX(), getY()-4);
}
}
if (getStudentWorld()->getKey(ch)) {
double x, y;
switch (ch)
{
case KEY_PRESS_LEFT:
setDirection(180);
x = (getX()-4);
y = getY();
if (getStudentWorld()->isBlockingObjectAt(x, y) || getStudentWorld()->isBlockingObjectAt(x, y+7)){
getStudentWorld()->bonk(x,y);
}
else {
moveTo(getX()-4, y);
}
break;
case KEY_PRESS_RIGHT:
setDirection(0);
x = (getX()+8);
y = getY();
if (getStudentWorld()->isBlockingObjectAt(x, y) || getStudentWorld()->isBlockingObjectAt(x, y+7)){
getStudentWorld()->bonk(x, y);
}
else {
moveTo(getX()+4, y);
}
break;
case KEY_PRESS_UP:
if(getStudentWorld()->isBlockingObjectAt(getX(), getY()-1) || getStudentWorld()->isBlockingObjectAt(getX()+4, getY()-1)) {
if (!m_jumPower){
m_rJumpDistance = 8;
}
else {
m_rJumpDistance = 12;
}
getStudentWorld()->playSound(SOUND_PLAYER_JUMP);
}
break;
case KEY_PRESS_SPACE:
if (!m_shootPower) {
break;
}
if (m_timeToRecharge > 0) {
break;
}
getStudentWorld()->playSound(SOUND_PLAYER_FIRE);
m_timeToRecharge = 8;
x = getX();
y = getY();
getPositionInThisDirection(getDirection(), 4, x, y);
getStudentWorld()->newFireball(x, y, getDirection(), 0);
break;
default:
break;
}
}
}
void Goodie::helper() {
// Goodies have same free fall and change direcion
// so i put them in a helper function in base class of goodies
if (getStudentWorld()->isBlockingObjectAt(getX(), getY()-1) ||
getStudentWorld()->isBlockingObjectAt(getX()+4, getY()-1)) {
}
else {
moveTo(getX(), getY()-2);
}
double x = getX();
double y = getY();
getPositionInThisDirection(getDirection(), 2, x, y);
if (getStudentWorld()->isBlockingObjectAt(x, y) ||
getStudentWorld()->isBlockingObjectAt(x+6,y) ||
getStudentWorld()->isBlockingObjectAt(x,y+7) ||
getStudentWorld()->isBlockingObjectAt(x+6,y+7))
{
setDirection(180 - getDirection());
return;
}
moveForward(2);
}
void FlowerGoodie::doSomething() {
// something that are different in each goodie class
if (getStudentWorld()->isOverlapPeach(getX(), getY())) {
getStudentWorld()->increaseScore(50);
getStudentWorld()->gainPeachPower(2);
getStudentWorld()->gainPeachPower(4, 2);
setDead();
getStudentWorld()->playSound(SOUND_PLAYER_POWERUP);
return;
}
helper();
}
void MushroomGoodie::doSomething() {
// something that are different in each goodie class
if (getStudentWorld()->isOverlapPeach(getX(), getY())) {
getStudentWorld()->increaseScore(75);
getStudentWorld()->gainPeachPower(3);
getStudentWorld()->gainPeachPower(4, 2);
setDead();
getStudentWorld()->playSound(SOUND_PLAYER_POWERUP);
return;
}
helper();
}
void StarGoodie::doSomething() {
// something that are different in each goodie class
if (getStudentWorld()->isOverlapPeach(getX(), getY())) {
getStudentWorld()->increaseScore(150);
getStudentWorld()->gainPeachPower(1);
setDead();
getStudentWorld()->playSound(SOUND_PLAYER_POWERUP);
return;
}
helper();
}
void Fireball::helper() {
// fire ball class has same falling and disapper functionality
// so i put it in the base class of fireball
if (getStudentWorld()->isBlockingObjectAt(getX(), getY()-2) ||
getStudentWorld()->isBlockingObjectAt(getX()+7, getY()-2))
{
}
else {
moveTo(getX(), getY()-2);
}
double x = getX();
double y = getY();
getPositionInThisDirection(getDirection(), 2, x, y);
if (getStudentWorld()->isBlockingObjectAt(x, y) ||
getStudentWorld()->isBlockingObjectAt(x+6,y) ||
getStudentWorld()->isBlockingObjectAt(x,y+7) ||
getStudentWorld()->isBlockingObjectAt(x+6,y+7))
{
setDead();
return;
}
moveForward(2);
increaseAnimationNumber();
}
void PeachFireball::doSomething() {
// if is overlap kill the enemy
if (getStudentWorld()->isOverlap(getX(), getY()))
{
if(getStudentWorld()->damage(getX(), getY()) || getStudentWorld()->damage(getX()+7, getY())) {
setDead();
return;
}
}
helper();
}
void PiranhaFireball::doSomething() {
// if is overlap with peach, kill peach
if (getStudentWorld()->isOverlapPeach(getX(), getY())) {
getStudentWorld()->damagePeach();
setDead();
}
helper();
}
void Shell::doSomething() {
// kill the enemy it touches
if (getStudentWorld()->isOverlap(getX(), getY())) {
if(getStudentWorld()->damage(getX(), getY()) || getStudentWorld()->damage(getX()+7, getY())) {
setDead();
return;
}
}
helper();
}
void Enemies::doSomething() {
// Koopa and Goomba has same doSomething function
// so i put it in the base class
if (!isAlive()){
return;
}
if (getStudentWorld()->isOverlapPeach(getX(), getY())) {
getStudentWorld()->bonkPeach();
isBonked();
return;
}
double x = getX();
double y = getY();
getPositionInThisDirection(getDirection(), 1, x, y);
if (getStudentWorld()->isBlockingObjectAt(x, y) ||
getStudentWorld()->isBlockingObjectAt(x+7, y) ||
!getStudentWorld()->isBlockingObjectAt(x, y-1) ||
!getStudentWorld()->isBlockingObjectAt(x+7, y-1))
{
setDirection(180 - getDirection());
return;
}
moveForward(1);
increaseAnimationNumber();
}
void Enemies::isBonked() {
// Goomba and Piranha has same is bonked and is damaged functino
// so i put them in the function in base class
if (getStudentWorld()->getPeachPower(1)){
// 1 is star power
getStudentWorld()->playSound(SOUND_PLAYER_KICK);
getStudentWorld()->increaseScore(100);
setDead();
}
}
void Enemies::isDamaged() {
getStudentWorld()->increaseScore(100);
setDead();
}
void Koopa::isBonked() {
if (getStudentWorld()->getPeachPower(1)){
getStudentWorld()->playSound(SOUND_PLAYER_KICK);
getStudentWorld()->increaseScore(100);
setDead();
getStudentWorld()->newShell(getX(), getY(), getDirection());
}
}
void Koopa::isDamaged() {
// generate a new shell when killed
getStudentWorld()->increaseScore(100);
setDead();
getStudentWorld()->newShell(getX(), getY(), getDirection());
}
void Piranha::doSomething() {
// Piranha has different implementation of doSomething
// so i overwrite it.
if (!isAlive()) {
return;
}
increaseAnimationNumber();
if (getStudentWorld()->isOverlapPeach(getX(), getY())) {
getStudentWorld()->bonkPeach();
isBonked();
return;
}
if (!(abs(getStudentWorld()->getPeachY()-getY()) <= 1.5 * SPRITE_HEIGHT)) {
return;
}
if (getStudentWorld()->getPeachX() < getX()) {
setDirection(180);
}
else {
setDirection(0);
}
if (m_firingDelay > 0){
m_firingDelay--;
return;
}
double distance = abs(getStudentWorld()->getPeachX() - getX());
// use absolute value from cmath lib
if (distance < 8 * SPRITE_WIDTH) {
getStudentWorld()->newFireball(getX(), getY(), getDirection(), 1);
getStudentWorld()->playSound(SOUND_PIRANHA_FIRE);
m_firingDelay = 40;
}
}
void Flag::doSomething() {
if (!isAlive()){
return;
}
if (getStudentWorld()->isOverlapPeach(getX(), getY())){
getStudentWorld()->increaseScore(1000);
setDead();
getStudentWorld()->moveToNext(); // go to next level
}
}
void Mario::doSomething() {
if (!isAlive()){
return;
}
if (getStudentWorld()->isOverlapPeach(getX(), getY())){
getStudentWorld()->increaseScore(1000);
setDead();
getStudentWorld()->reachedMario(); // reach mario
}
}