-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathActor.cpp
More file actions
492 lines (395 loc) · 14 KB
/
Actor.cpp
File metadata and controls
492 lines (395 loc) · 14 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
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
466
467
468
#include "Actor.h"
#include "StudentWorld.h"
#include <cstdlib>
// Students: Add code to this file (if you wish), Actor.h, StudentWorld.h, and StudentWorld.cpp
void Player::doSomething()
{
if(!isAlive())
return;
int key = 0;
if(getWorld()->getKey(key))
{
int x = getX();
int y = getY();
Direction dir = none;
switch (key) //Switch statement to record direction and adjacent x and y coordinates according to the direction
{
case KEY_PRESS_DOWN:
{
dir= down;
x = getWorld()->getXcoordOfNext(x, dir);
y = getWorld()->getYcoordOfNext(y, dir);
}
break;
case KEY_PRESS_LEFT:
{
dir = left;
x= getWorld()->getXcoordOfNext(x, dir);
y= getWorld()->getYcoordOfNext(y, dir);
}
break;
case KEY_PRESS_RIGHT:
{
dir = right;
x=getWorld()->getXcoordOfNext(x, dir);
y=getWorld()->getYcoordOfNext(y, dir);
}
break;
case KEY_PRESS_UP:
{
dir=up;
x=getWorld()->getXcoordOfNext(x, dir);
y=getWorld()->getYcoordOfNext(y, dir);
}
break;
case KEY_PRESS_ESCAPE:
{
setDead();
return;
}
break;
case KEY_PRESS_SPACE:
{
if(m_ammo <= 0) //return if player is out of ammo
return;
m_ammo--; //decrement ammo after firing
x = getWorld()->getXcoordOfNext(x, getDirection());
y = getWorld()->getYcoordOfNext(y, getDirection());
getWorld()->fireBullet(this, x, y, getDirection()); //fire a bullet in front of the player
return;
}
break;
default:
break;
}
if(getDirection() != dir) //change direction if player has been given new direction
setDirection(dir);
if(getWorld()->canMove(this,x, y, getDirection())) //move the player in the rquired direction if it is able to move
moveTo(x, y);
}
}
void Bullet::doSomething()
{
if(!isAlive())
return;
int x = getWorld()->getXcoordOfNext(getX(), getDirection());
int y = getWorld()->getYcoordOfNext(getY(), getDirection());
Actor* q = getWorld()->returnActor(getX(), getY()); //return actor at bullet's current position
Actor* p = getWorld()->returnActor(x, y); //return actor at bullet's next position
if(q != nullptr && !getWorld()->BulletCanPass(q)) //checks bullet's current position
{
setDead(); //kill bullet if it hits something it can't pass
setVisible(false);
if(getWorld()->canBeHit(getX(), getY())) //if actor can be hit, then hit it
getWorld()->HitIt(getX(), getY());
return;
}
moveTo(x, y); //moves bullet to adjacent position
if(p != nullptr && !getWorld()->BulletCanPass(p)) //checks bullet's next position
{
setDead();
setVisible(false);
if(getWorld()->canBeHit(getX(), getY()))
getWorld()->HitIt(getX(), getY());
return;
}
}
bool Boulder::push(Direction dir)
{
int a = getWorld()->getXcoordOfNext(getX(), dir);
int b = getWorld()->getYcoordOfNext(getY(), dir);
Actor* p = getWorld()->returnActor(a, b); //return actor at Boulder's adjacent position
if(p==nullptr)
{
moveTo(a, b); //move boulder if adjacent position is empty
return true;
}
else if(dynamic_cast<Hole*>(p) != nullptr) //else if there is a hole, destroy both the hole and the boulder
{
p->setDead();
moveTo(a, b);
setDead();
return true;
}
else
return false;
}
void Goodie::doSomething()
{
getWorld()->increaseScore(m_increase); //increase score after getting goodie
setDead(); //destroy goodie after taking it
getWorld()->playSound(SOUND_GOT_GOODIE); //play sound
}
void ExtraLife::doSomething()
{
if(getWorld()->returnPlayer()->getX() == getX() && getWorld()->returnPlayer()->getY() == getY())
{
Goodie::doSomething();
getWorld()->incLives(); //if player is on the goodie, apply the goodie's bonus
}
}
void RestoreHealth::doSomething()
{
if(!isAlive())
return;
if(getWorld()->returnPlayer()->getX() == getX() && getWorld()->returnPlayer()->getY() == getY())
{
Goodie::doSomething();
getWorld()->returnPlayer()->restoreHealth(); //restore health of the player
}
}
void Ammo::doSomething()
{
if(!isAlive())
return;
if(getWorld()->returnPlayer()->getX() == getX() && getWorld()->returnPlayer()->getY() == getY())
{
Goodie::doSomething();
getWorld()->returnPlayer()->increaseAmmo(); //increase ammo of the player
}
}
void Jewel::doSomething()
{
if(!isAlive())
return;
if(getWorld()->returnPlayer()->getX() == getX() && getWorld()->returnPlayer()->getY() == getY())
{
Goodie::doSomething();
getWorld()->decreaseJewels(); //decrease the number of jewels in the world
}
}
void Exit::doSomething()
{
if(getWorld()->returnPlayer()->getX() == getX() && getWorld()->returnPlayer()->getY() == getY() && isVisible())
{
getWorld()->playSound(SOUND_FINISHED_LEVEL); //if player is on the exit and it's visible, complete level
getWorld()->increaseScore(2000 + getWorld()->getBonus());
levelComplete = true;
return;
}
if(getWorld()->numJewels() == 0) //if jewels are finished, make the level visible and play the sound once
{
if(checker == 1)
return;
getWorld()->playSound(SOUND_REVEAL_EXIT);
makeVisible();
checker++;
}
}
void Robot::isAttacked() //calls the base class' isAttacked function and plays the appropriate sound
{
Mortal::isAttacked();
getWorld()->playSound(SOUND_ROBOT_IMPACT);
if(!isAlive())
getWorld()->playSound(SOUND_ROBOT_DIE);
}
int Robot::setTicks() //function to set the ticks
{
int x = (28 - getWorld()->getLevel())/4;
return x > 3 ? x : 3;
}
bool Robot::fire(int x, int y, Direction dir) //fires a bullet if the robot can fire in that direction
{
if(getWorld()->RobotCanFire(x, y, dir))
{
int a = getWorld()->getXcoordOfNext(x, dir);
int b = getWorld()->getYcoordOfNext(y, dir);
getWorld()->fireBullet(this, a, b, dir);
return true;
}
return false;
}
void Snarlbot::doSomething()
{
if(!isAlive())
return;
if(getTicks() != getTempTicks()) //turn to rest
{
increaseTick();
return;
}
if(fire(getX(), getY(), getDirection())) //function to check if it can fire and then fire the bullet
{
setTicksToZero();
return;
}
int x = getWorld()->getXcoordOfNext(getX(), getDirection());
int y = getWorld()->getYcoordOfNext(getY(), getDirection());
if(getWorld()->canMove(this,x, y,getDirection())) //else move in the adjacent direction if possible
moveTo(x, y);
else
setDirection(changeDirection()); //else change the direction
setTicksToZero(); //set ticks to 0 after moving
}
bool Kleptobot::setProperDirection()
{
int arr[] = {0,0,0,0};
while(arr[0] == 0 || arr[1] == 0 || arr[2] == 0 || arr[3] == 0) //checks if kleptobot can move in any direction and change its direction if it can
{
Direction dir = getRandomDirection(); //get a random direction and adjacent position coordinates
int x = getWorld()->getXcoordOfNext(getX(), dir);
int y = getWorld()->getYcoordOfNext(getY(), dir);
switch (dir)
{
case up:
if(arr[0] == 0) //set array value to 1 if robot cannot move in that direction
{
if(getWorld()->canMove(this, x, y, dir))
{
setDirection(dir);
return true;}
else
arr[0] = 1;
}
break;
case down:
if(arr[1] == 0)
{
if(getWorld()->canMove(this, x, y, dir))
{
setDirection(dir);
return true;}
else
arr[1] = 1;
}
break;
case left:
if(arr[2] == 0)
{
if(getWorld()->canMove(this, x, y, dir))
{
setDirection(dir);
return true;}
else
arr[2] = 1;
}
break;
case right:
if(arr[3] == 0)
{
if(getWorld()->canMove(this, x, y, dir))
{
setDirection(dir);
return true;}
else
arr[3] = 1;
}
break;
default:
break;
}
}
return false;
}
void Kleptobot::doSomething()
{
if(!isAlive())
return;
if(getTicks() != getTempTicks())
{
increaseTick();
return;
}
int x = getWorld()->getXcoordOfNext(getX(), getDirection());
int y = getWorld()->getYcoordOfNext(getY(), getDirection());
if(getTemp() < getDistance()) //if it hasn't moved DistanceBeforeTurning steps
{
Actor* p = getWorld()->checkIfGoodie(getX(), getY()); //check if robot is standing on goodie
if(p != nullptr)
{
if(doesItPickUpGoodie(p)) //if it is, then check if it actually picks it up(1/10 chance)
{
p->setDead();
getWorld()->playSound(SOUND_ROBOT_MUNCH); //if it does, destory the goodie and play sound
pickedUpGoodie();
setTicksToZero();
}
else
{
if(getWorld()->canMove(this, x, y, getDirection())) //else if it doesn't pick up the goodie, move one step in current direction
{
Move(x, y);
}
else
{
if(setProperDirection()) //else change direction and then move in the new direction
{
int a = getWorld()->getXcoordOfNext(getX(), getDirection());
int b = getWorld()->getYcoordOfNext(getY(), getDirection());
Move(a, b);
reset();
}
}
}
}
else //else if robot is not on a goodie
{
if(getWorld()->canMove(this, x, y, getDirection())) //move in current direction, if it can
{
Move(x, y);
}
else
{
if(setProperDirection()) //else move in a new direction
{
int a = getWorld()->getXcoordOfNext(getX(), getDirection());
int b = getWorld()->getYcoordOfNext(getY(), getDirection());
Move(a, b);
reset();
}
}
}
}
else //else if robot has already moved distanceBeforeTurning steps, change direction and move in new direction
{
if(setProperDirection())
{
int a = getWorld()->getXcoordOfNext(getX(), getDirection());
int b = getWorld()->getYcoordOfNext(getY(), getDirection());
Move(a, b);
reset();
}
}
}
bool KleptobotFactory::createRobot() //function to check if robot has been created by factory
{
if(getWorld()->createNewRobot(this,getX(),getY()))
return true;
else
return false;
}
void KleptobotFactory::doSomething() //if factory can create robot and creates it, play sound
{
if(getWorld()->canCreateRobot(getX(), getY()))
if(createRobot())
getWorld()->playSound(SOUND_ROBOT_BORN);
}
Kleptobot::~Kleptobot() //if kleptobot had a goodie, create new goodie when it dies
{
if(hasGoodie())
getWorld()->createNewGoodie(storeId, getX(), getY());
}
bool AngryKleptobotFactory::createRobot() //same as kleptobot factory, but pointer being passed in the function will be different
{
if(getWorld()->createNewRobot(this,getX(),getY()))
return true;
else
return false;
}
void AngryKleptobot::doSomething() //if angry bot can fire, then it fires else it does what a normal kleptobot will do
{
if(!isAlive())
return;
if(getTicks() != getTempTicks())
{
increaseTick();
return;
}
if(fire(getX(), getY(), getDirection()))
{
setTicksToZero();
return;
}
else
Kleptobot::doSomething();
}