-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactor.cpp
More file actions
382 lines (328 loc) · 10.7 KB
/
actor.cpp
File metadata and controls
382 lines (328 loc) · 10.7 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
#include "actor.h"
#include "StudentWorld.h"
#include <cstdlib>
// Students: Add code to this file (if you wish), actor.h, StudentWorld.h, and StudentWorld.cpp
Actor::Actor(int imageID, int startX, int startY, StudentWorld* sw)
: GraphObject(imageID, startX, startY), m_studentWorld(sw), m_alive(true)
{setVisible(true);}
bool Actor::alive()
{
if (getY() < 0 || getY() > VIEW_HEIGHT-1 || getX() < 0 || getX() > VIEW_WIDTH-1)
kill();
return m_alive;
}
bool operator< (const Actor& a, const Actor& b)
{
return true;
}
// ***Star class implementations***
Star::Star(StudentWorld* sw)
: Actor(IID_STAR,rand()%30,VIEW_HEIGHT-1, sw) {}
void Star::doSomething()
{
if (alive())
{
if (getY() > 0)
moveTo(getX(),getY()-1);
else
kill();
}
}
// ***Ship class implementations***
Ship::Ship(int imageID, int startX, int startY, StudentWorld* sw)
: Actor(imageID, startX, startY, sw)
{ }
bool Ship::alive()
{
if (getEnergy() <= 0)
kill();
return Actor::alive();
}
void Ship::damage(int severity)
{
if (getID() != IID_PLAYER_SHIP && severity == COLLIDE_PLAYERSHIP)
{ kill(); return;}
int updateEnergy = getEnergy()-severity;
setEnergy(updateEnergy);
if (getEnergy() <= 0)
kill();
}
// ***PlayerShip class implementations***
PlayerShip::PlayerShip(StudentWorld* sw)
: Ship(IID_PLAYER_SHIP, VIEW_WIDTH/2, 1, sw), m_firedLastTime(false), m_nTorpedoes(0)
{ setEnergy(DEFAULT_PLAYER_ENERGY); }
void PlayerShip::doSomething()
{
// if colliding with any alien ships
getWorld()->collide(this, getX(), getY());
// if energy is drained
if (getEnergy() <= 0)
{ kill(); return; }
// get user's keyboard input
int key;
if (getWorld()->getKey(key)) // user hit a valid key
{
switch(key)
{
case KEY_PRESS_LEFT:
if (getX() > 0) moveTo(getX()-1, getY());
m_firedLastTime = false; break;
case KEY_PRESS_RIGHT:
if (getX() < VIEW_WIDTH-1) moveTo(getX()+1, getY());
m_firedLastTime = false; break;
case KEY_PRESS_UP:
if (getY() < VIEW_HEIGHT-1) moveTo(getX(), getY()+1);
m_firedLastTime = false; break;
case KEY_PRESS_DOWN:
if (getY() > 0) moveTo(getX(), getY()-1);
m_firedLastTime = false; break;
case KEY_PRESS_SPACE:
if (!m_firedLastTime)
{
if (getY() < VIEW_HEIGHT-1)
{
getWorld()->fire(IID_BULLET,IID_PLAYER_SHIP,getX(),getY()+1);
getWorld()->playSound(SOUND_PLAYER_FIRE);
m_firedLastTime = true;
}
}
break;
case KEY_PRESS_TAB:
if (getNumTorpedo()> 0 && !m_firedLastTime)
{
getWorld()->fire(IID_TORPEDO,IID_PLAYER_SHIP,getX(),getY()+1);
getWorld()->playSound(SOUND_PLAYER_TORPEDO);
decTorpedo();
m_firedLastTime = true;
}
break;
}
}
else m_firedLastTime = false; // user didn't hit a key or hit an invalid key
// check if colliding again after shift
getWorld()->collide(this, getX(), getY());
}
// ***Alien class implementations***
Alien::Alien(int imageID, int startX, int startY,StudentWorld* sw)
: Ship(imageID, startX, startY,sw),m_active(true) {}
Nachling::Nachling(StudentWorld* sw, int imageID)
: Alien(imageID, rand()% 30, VIEW_HEIGHT-1, sw), m_state(0)
{ setEnergy(NACHLING_STARTING_ENERGY_FACTOR * (sw->getRound())); }
WealthyNachling::WealthyNachling(StudentWorld *sw)
: Nachling(sw, IID_WEALTHY_NACHLING), m_malfunction(false)
{ setEnergy(WEALTHY_NACHLING_STARTING_ENERGY_FACTOR * (sw->getRound())); }
Smallbot::Smallbot(StudentWorld* sw)
: Alien(IID_SMALLBOT, rand()% 30, VIEW_HEIGHT-1, sw), m_gotAttacked(false)
{ setEnergy(SMALLBOT_STARTING_ENERGY_FACTOR * (sw->getRound()));}
void Nachling::doSomething()
{
if(!isActive()) // did something last tick
{ setActivity(true); return; }
setActivity(false);
if (m_state == 0)
{
if (getX() == getWorld()->getPlayerX() && getX() != 0 && getX() != VIEW_WIDTH)
{
m_state = 1;
int mdb = MDB(getX());
if (mdb > 3)
m_HMD = rand() % 3+1;
else
m_HMD = mdb;
m_hdir = rand() % 2;
m_HMR = m_HMD;
}
else if (rand() % 3 == 0)
{
if (getX() < getWorld()->getPlayerX())
{ moveTo(getX()+1, getY()-1); return; }
else if (getX() > getWorld()->getPlayerX())
{ moveTo(getX()-1, getY()-1); return; }
}
moveTo(getX(), getY()-1);
if (getY() < 0)
kill();
return;
}
if (m_state == 1)
{
if (getWorld()->getPlayerY() > getY())
{ m_state = 2; return; }
if (m_HMR == 0)
{
if(m_hdir == LEFT)
m_hdir = RIGHT;
else m_hdir = LEFT;
m_HMR = 2*m_HMD;
}
else m_HMR--;
if (m_hdir == LEFT)
moveTo(getX()-1, getY());
else moveTo(getX()+1, getY());
int chancesOfFiring = (BASE_CHANCE_OF_FIRING / (getWorld()->getRound()) )+1;
if (rand() % chancesOfFiring == 0)
{
if (getWorld()->activeEnemyProjectiles() < 2*getWorld()->getRound() )
getWorld()->fire(IID_BULLET,IID_ALIEN,getX(),getY()-1);
}
if (rand() % CHANCE_TRANS_STATE2 == 0)
m_state = 2;
return;
}
if (m_state == 2)
{
if (getY() == VIEW_HEIGHT-1)
{ m_state = 0; return; }
if (getX() == 0)
m_hdir = RIGHT;
else if(getX() == VIEW_WIDTH-1)
m_hdir = LEFT;
else
m_hdir = rand() % 2;
if (m_hdir == LEFT)
moveTo(getX()-1,getY()+1);
else moveTo(getX()+1, getY()+1);
return;
}
}
int Nachling::MDB(int x)
{
return x < VIEW_WIDTH-1-x ? x : VIEW_WIDTH-1-x;
}
void WealthyNachling::doSomething()
{
if (m_malfunction && m_malfuncstate != MALFUNC_TICKS)
{ m_malfuncstate++; return; }
else if (m_malfunction && m_malfuncstate == MALFUNC_TICKS)
{ m_malfuncstate = 0; m_malfunction = false; }
else if (rand() % 200 == 0)
{
m_malfunction = true;
m_malfuncstate = 0;
return;
}
Nachling::doSomething();
}
void Smallbot::doSomething()
{
if (!isActive())
{ setActivity(true); return; }
setActivity(false);
if(m_gotAttacked)
{
if (getX() == 0)
moveTo(1, getY()-1);
else if(getX() == VIEW_WIDTH-1 || rand() % 2 == 0)
moveTo(getX()-1, getY()-1);
else
moveTo(getX()+1, getY()-1);
}
else
moveTo(getX(), getY()-1);
m_gotAttacked = false;
if (getX() == getWorld()->getPlayerX() && getY() > getWorld()->getPlayerY())
{
int q = 100 / (getWorld()->getRound());
int missileType = IID_BULLET;
if (rand() % q == 0)
missileType = IID_TORPEDO;
int d = getWorld()->activeEnemyProjectiles();
if (d < 2 * getWorld()->getRound())
getWorld()->fire(missileType, IID_SMALLBOT, getX(), getY()-1);
}
alive();
}
void WealthyNachling::damage(int severity)
{
if (severity != COLLIDE_PLAYERSHIP)
{
Alien::damage(severity);
if (!alive() && rand() % 3 == 0)
{
if (rand() % 100 < 50)
getWorld()->dropGoodie(IID_ENERGY_GOODIE,getX(),getY());
else
getWorld()->dropGoodie(IID_TORPEDO_GOODIE,getX(),getY());
}
}
else kill(); // collide with player ship
if (alive())
getWorld()->playSound(SOUND_ENEMY_HIT);
}
void Smallbot::damage(int severity)
{
if (severity != COLLIDE_PLAYERSHIP)
{
m_gotAttacked = true;
Alien::damage(severity);
if (!alive() && rand() % 3 == 0)
getWorld()->dropGoodie(IID_FREE_SHIP_GOODIE,getX(),getY());
}
else Alien::damage(severity); // collide with player ship
if (alive())
getWorld()->playSound(SOUND_ENEMY_HIT);
}
// Implementations of helper functions of Actor class
void moveUp(Actor* who)
{
if (who->getY() < VIEW_HEIGHT-1)
who->moveTo(who->getX(),who->getY()+1);
else // moving off the grid
who->kill();
}
void moveDown(Actor* who)
{
if (who->getY() > 0)
who->moveTo(who->getX(),who->getY()-1);
else // moving off the grid
who->kill();
}
// ***Bullet class implementations***
Bullet::Bullet(int startX, int startY,StudentWorld* sw,
int firedBy, int imageID)
: Actor(imageID, startX, startY, sw), m_firedBy(firedBy) {alive();}
void Bullet::doSomething()
{
if (!getWorld()->collide(this, getX(), getY())) // didn't collide with a target
{
if (m_firedBy == IID_PLAYER_SHIP)
moveUp(this);
else
moveDown(this);
getWorld()->collide(this, getX(), getY());
}
}
// ***Torpedo class implementations***
Torpedo::Torpedo(int startX, int startY, StudentWorld* sw, int firedBy)
: Bullet(startX, startY, sw, firedBy, IID_TORPEDO) {}
// *** Goodie class implementations ***
Goodie::Goodie(int imageID, int startX, int startY, StudentWorld* sw)
: Actor(imageID,startX,startY,sw), m_ticksExisted(1) {}
int Goodie:: goodieTickLifetime()
{ return 100 / (getWorld()->getRound()) + 30; }
void Goodie::doSomething() {
if (!getWorld()->collide(this, getX(), getY()) )
{
m_ticksExisted++;
double brightness = static_cast<double>(goodieTickLifetime()-getTicksExisted()) / goodieTickLifetime() + 0.2;
setBrightness(brightness);
if (getTicksExisted() == goodieTickLifetime())
{ kill(); return; }
else if (getTicksExisted() % 3 == 0)
moveTo(getX(), getY()-1);
if (alive())
getWorld()->collide(this, getX(), getY());
}
}
FreeShipGoodie::FreeShipGoodie(int startX, int startY, StudentWorld* sw)
: Goodie(IID_FREE_SHIP_GOODIE, startX, startY, sw) {}
TorpedoGoodie::TorpedoGoodie(int startX, int startY, StudentWorld* sw)
: Goodie(IID_TORPEDO_GOODIE, startX, startY, sw) {}
EnergyGoodie::EnergyGoodie(int startX, int startY, StudentWorld* sw)
: Goodie(IID_ENERGY_GOODIE, startX, startY, sw) {}
void FreeShipGoodie::pickedUp()
{ getWorld()->incLives(); }
void EnergyGoodie::pickedUp()
{ getWorld()->refillPlayerEnergy(); }
void TorpedoGoodie::pickedUp()
{ getWorld()->givePlayerTorpedoes(); }