-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvader.cpp
More file actions
270 lines (225 loc) · 7.27 KB
/
Invader.cpp
File metadata and controls
270 lines (225 loc) · 7.27 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
#include "Invader.h"
#include "Game.h"
Invader::Invader()
{
inUse = false;
firingFlash = false;
mWeapon = BASIC_ENEMY;
mpFlashProjectile = NULL;
mpBunkerBusterProjectile = NULL;
}
Invader::~Invader()
{
delete mpWeaponAnimation;
if (firingFlash)
{
delete mpFlashProjectile;
mpFlashProjectile = NULL;
}
delete mpBunkerBusterProjectile;
mpBunkerBusterProjectile = NULL;
}
void Invader::drawWeapon(GraphicsSystem *graphics)
{
graphics->drawAnimation(mpWeaponAnimation, Vector2(mPosition.x, mPosition.y));
// Also draw my Flash
if (firingFlash)
{
mpFlashProjectile->draw(graphics);
}
// Also draw my Bunker Buster
if (mpBunkerBusterProjectile != NULL)
{
mpBunkerBusterProjectile->draw(graphics);
}
}
void Invader::restoreOriginalPosition()
{
mPosition = mOriginalPosition;
timeLastFired = Game::getInstance()->getCurrentTime();
}
bool Invader::update(const float frameTime)
{
// Update bounding box
mBoundingBox = Rect(Vector2((float)mPosition.x + 12, (float)mPosition.y + 16), Vector2(24, 18));
// Destroy Barriers
std::vector<Barrier*, std::allocator<Barrier*>> barriers = Game::getInstance()->getPlayer()->getBarriers();
if (mPosition.y > Game::getInstance()->getPlayer()->BARRIER_Y - 48)
{
for (unsigned int i = 0; i < barriers.size(); ++i)
{
if (mBoundingBox.checkCollision(barriers[i]->getBoundingBox()))
{
barriers[i]->destroy();
}
}
}
// Fire event that a wall has been reached
if (mPosition.x <= 0 || mPosition.x >= Game::getInstance()->getGraphicsSystem()->getWidth() - 48)
{
gpEventSystem->fireEvent(INVADER_HIT_WALL);
}
// Fire event that player has been hit
if (mBoundingBox.checkCollision(Game::getInstance()->getPlayer()->getBoundingBox()))
{
inUse = false;
gpEventSystem->fireEvent(INVADER_HIT_PLAYER);
return true;
}
// Update animation
mpWeaponAnimation->update(frameTime);
// Weapon specific, Fire a projectile
switch (mWeapon)
{
case BASIC_ENEMY:
if (timeLastFired + 1.0f <= Game::getInstance()->getCurrentTime() && (rand() % (300 - (Game::getInstance()->getDifficulty() * 40) - (Game::getInstance()->getLevel() * 5)) == 0))
{
mpWeaponAnimation->setCurrentSprite(0);
timeLastFired = Game::getInstance()->getCurrentTime();
// No sound here is better
Game::getInstance()->getEnemyManager()->fireProjectile(BASIC_ENEMY, Vector2(mPosition.x + 20, mPosition.y + 40), Vector2(0.0f, 0.0f));
}
break;
case TRIPLE_THREAT:
if (timeLastFired + 1.0f <= Game::getInstance()->getCurrentTime() && (rand() % ((400 - (Game::getInstance()->getDifficulty() * 50) - (Game::getInstance()->getLevel() * 5))) == 0))
{
mpWeaponAnimation->setCurrentSprite(0);
timeLastFired = Game::getInstance()->getCurrentTime();
Audio *audio = AudioLocator::locate();
audio->playSound(MULTI);
Game::getInstance()->getEnemyManager()->fireProjectile(TRIPLE_THREAT, Vector2(mPosition.x + 20, mPosition.y + 40), Vector2(-1.0f, 0.0f));
Game::getInstance()->getEnemyManager()->fireProjectile(TRIPLE_THREAT, Vector2(mPosition.x + 20, mPosition.y + 40), Vector2(0.0f, 0.0f));
Game::getInstance()->getEnemyManager()->fireProjectile(TRIPLE_THREAT, Vector2(mPosition.x + 20, mPosition.y + 40), Vector2(1.0f, 0.0f));
}
break;
case FLASH:
// Set my Flash Projectile to correct position
if (firingFlash)
{
if (mpFlashProjectile != NULL)
{
// Update my Flash
mpFlashProjectile->setPosition(Vector2(mPosition.x + 20, mPosition.y + 42));
float timeSinceFired = Game::getInstance()->getCurrentTime() - timeLastFired;
if (timeSinceFired >= 0.75f && mpWeaponAnimation->getCurrentSprite() == 3)
{
if (timeSinceFired < 1.2f)
{
// Animate Weapon
mpWeaponAnimation->setCurrentSprite(0);
}
else
{
// Finish firing
firingFlash = false;
delete mpFlashProjectile;
mpFlashProjectile = NULL;
}
}
}
else
{
firingFlash = false;
}
}
else if (timeLastFired + 7.0f <= Game::getInstance()->getCurrentTime() && (rand() % (550 - (Game::getInstance()->getDifficulty() * 50) - (Game::getInstance()->getLevel() * 5)) == 0))
{
firingFlash = true;
timeLastFired = Game::getInstance()->getCurrentTime();
mpFlashProjectile = new Projectile(FLASH, Vector2(mPosition.x + 20, mPosition.y + 40), Vector2(0.0f, 0.0f));
}
break;
case BUNKER_BUSTER:
if (mpBunkerBusterProjectile != NULL)
{
// Set it to the correct position
mpBunkerBusterProjectile->setPosition(Vector2(mPosition.x + 18.0f, mPosition.y + 33.0f));
// Drop Bunker Buster
if (timeLastFired + 3.0f <= Game::getInstance()->getCurrentTime() && rand() % (950 - (Game::getInstance()->getDifficulty() * 100)) == 0)
{
// Play sound
Audio *audio = AudioLocator::locate();
audio->playSound(FIRE_EXPLOSIVE);
// Switch sprite
mpWeaponAnimation->setCurrentSprite(1);
// Add new bunker buster to Enemy Manager
Game::getInstance()->getEnemyManager()->fireProjectile(BUNKER_BUSTER, Vector2(mPosition.x + 18.0f, mPosition.y + 33.0f), Vector2(0.0f, 0.05f));
// Delete my personal one
delete mpBunkerBusterProjectile;
mpBunkerBusterProjectile = NULL;
}
else if (!mpBunkerBusterProjectile->inUse)
{
delete mpBunkerBusterProjectile;
mpBunkerBusterProjectile = NULL;
}
}
break;
}
return false;
}
Invader* Invader::init(float x_, float y_)
{
inUse = true;
mPosition.x = x_;
mPosition.y = y_;
mOriginalPosition.x = x_;
mOriginalPosition.y = y_;
timeLastFired = Game::getInstance()->getCurrentTime();
mBoundingBox = Rect(Vector2((float)mPosition.x + 12, (float)mPosition.y + 16), Vector2(24, 18));
if (firingFlash)
{
// Should be redundant, but you never know.
delete mpFlashProjectile;
}
mpFlashProjectile = NULL;
firingFlash = false;
delete mpBunkerBusterProjectile;
mpBunkerBusterProjectile = NULL;
// For resetting
delete mpWeaponAnimation;
// Chance to have special weapon
int random = rand() % 100;
if (random < 15)
{
mWeapon = TRIPLE_THREAT;
mpWeaponAnimation = new Animation(0.1f, false);
for (int i = 0; i < 4; ++i)
{
mpWeaponAnimation->addSprite(new Sprite(Game::getInstance()->getGraphicsBufferManager()->getGraphicsBuffer("Weapon_Triple_Threat"), 48, 48, Vector2((float)i * 48, 0.0f)));
}
}
else if (random < 28)
{
mWeapon = FLASH;
mpWeaponAnimation = new Animation(0.3f, false);
for (int i = 0; i < 4; ++i)
{
mpWeaponAnimation->addSprite(new Sprite(Game::getInstance()->getGraphicsBufferManager()->getGraphicsBuffer("Weapon_Flash"), 48, 48, Vector2((float)i * 48, 0.0f)));
}
}
else if (random < 40)
{
mWeapon = BUNKER_BUSTER;
mpWeaponAnimation = new Animation(0.0f, false);
for (float i = 0; i < 2; ++i)
{
mpWeaponAnimation->addSprite(new Sprite(Game::getInstance()->getGraphicsBufferManager()->getGraphicsBuffer("Weapon_Bunker_Buster"), 48, 48, Vector2(i * 48.0f, 0.0f)));
}
mpBunkerBusterProjectile = new Projectile(BUNKER_BUSTER, Vector2(mPosition.x + 19.0f, mPosition.y + 33.0f), Vector2());
}
else
{
mWeapon = BASIC_ENEMY;
mpWeaponAnimation = new Animation(0.2f, false);
for (int i = 0; i < 4; ++i)
{
mpWeaponAnimation->addSprite(new Sprite(Game::getInstance()->getGraphicsBufferManager()->getGraphicsBuffer("Weapon_Basic_Enemy"), 48, 48, Vector2((float)i * 48, 0.0f)));
}
}
if (mWeapon != BUNKER_BUSTER)
{
mpWeaponAnimation->setCurrentSprite(3);
}
return this;
}