-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleParticles.cpp
287 lines (240 loc) · 7.65 KB
/
ModuleParticles.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
#include "ModuleParticles.h"
#include "Application.h"
#include "ModuleTextureManager.h"
#include "ModuleRenderer.h"
#include "ModuleCollisions.h"
#include "ModuleLevel1.h"
#include "SDL_timer.h"
ModuleParticles::ModuleParticles(bool startEnabled) : Module(startEnabled)
{
name = "particles";
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
particles[i] = nullptr;
}
ModuleParticles::~ModuleParticles()
{
}
bool ModuleParticles::Start()
{
LOG("Loading particles");
particlesTexture = App->textures->Load("Assets/sprites/explosions/explosions.png");
++totalTextures;
LOG("Loading weapons particles");
weaponsParticlesTexture = App->textures->Load("Assets/sprites/weapons/weapons.png");
++totalTextures;
// Bullets Animaton
bullet1.anim.PushBack({ 456, 8, 13, 3 });
bullet2.anim.PushBack({ 470, 6, 12, 7 });
bullet3.anim.PushBack({ 483, 3, 13, 12 });
bullet4.anim.PushBack({ 498, 0, 13, 18 });
bullet5.anim.PushBack({ 499, 18, 13, 12 });
bullet1.anim.loop = bullet2.anim.loop = bullet3.anim.loop = bullet4.anim.loop = bullet5.anim.loop = true;
bullet1.speed.x = bullet2.speed.x = bullet3.speed.x = bullet4.speed.x = bullet5.speed.x = 7.0f;
bullet1.lifetime = bullet2.lifetime = bullet3.lifetime = bullet4.lifetime = bullet5.lifetime = 180;
// Enemy Bullet Animaton
enemyBullet.anim.PushBack({ 44, 370, 5 , 7 });
enemyBullet.anim.PushBack({ 78, 372, 5, 5 });
enemyBullet.anim.PushBack({ 110, 371, 15, 6 });
enemyBullet.anim.loop = true;
enemyBullet.anim.speed = 0.075f;
enemyBullet.lifetime = 180;
// Turret's missiles
turretMissile.anim.PushBack({ 307, 57, 6, 16 });
turretMissile.anim.PushBack({ 322, 58, 6, 16 });
turretMissile.anim.loop = true;
turretMissile.anim.speed = 0.075f;
turretMissile.lifetime = 180;
// Explosion Animation
explosion.anim.PushBack({ 27, 256, 34, 39 });
explosion.anim.PushBack({ 98, 261, 44, 36 });
explosion.anim.PushBack({ 164, 263, 33, 27 });
explosion.anim.PushBack({ 223, 262, 31, 29 });
explosion.anim.loop = true;
explosion.anim.speed = 0.075f;
// Explosion of the enemies
enemyExplosion.anim.PushBack({ 27, 313, 24, 25 });
enemyExplosion.anim.PushBack({ 97, 310, 31, 29 });
enemyExplosion.anim.PushBack({ 159, 310, 31, 28 });
enemyExplosion.anim.PushBack({ 217, 315, 35, 27 });
enemyExplosion.anim.speed = 0.075f;
// Stealth bomber bombs explosion
sbBombExplosion.anim.PushBack({ 27, 313, 24, 25 });
sbBombExplosion.anim.PushBack({ 97, 310, 31, 29 });
sbBombExplosion.anim.PushBack({ 159, 310, 31, 28 });
sbBombExplosion.anim.PushBack({ 217, 315, 35, 27 });
sbBombExplosion.anim.speed = 0.01f;
sbBombExplosion.speed.x = 1.0f;
sbBombExplosion.speed.y = 0;
sbBombExplosion.lifetime = 15;
// Stealth bomber firecannon
sbFirecannon.anim.PushBack({ 0,193,47,12 });
sbFirecannon.anim.loop = false;
sbFirecannon.anim.speed = 0;
sbFirecannon.speed.x = -2;
sbFirecannon.isWeapon = true;
//Stealth bomber explosion when shooting
sbSparks.anim.PushBack({ 48,191,13,15 });
sbSparks.anim.loop = false;
sbSparks.anim.speed = 0;
sbSparks.speed.x = 1.0;
sbSparks.lifetime = 20;
sbSparks.isWeapon = true;
// Falcon explosion animation
falconExplosion.anim.PushBack({ 154, 0, 23, 26 });
falconExplosion.anim.PushBack({ 178, 0, 24, 26 });
falconExplosion.anim.PushBack({ 203, 0, 26, 28 });
falconExplosion.anim.PushBack({ 230, 0, 26, 28 });
falconExplosion.anim.PushBack({ 284, 0, 26, 28 });
falconExplosion.anim.speed = 0.075f;
bombExplosion.anim.loop = false;
falconExplosion.isWeapon = true;
// Bomb explosions animation
bombExplosion.anim.PushBack({ 28, 57, 28, 33 });
bombExplosion.anim.PushBack({ 57, 57, 24, 33 });
bombExplosion.anim.PushBack({ 82, 57, 28, 33 });
bombExplosion.anim.PushBack({ 111, 57, 26, 33 });
bombExplosion.anim.PushBack({ 138, 57, 28, 33 });
bombExplosion.anim.PushBack({ 167, 57, 28, 33 });
bombExplosion.anim.speed = 0.075f;
bombExplosion.anim.loop = false;
bombExplosion.isWeapon = true;
// Ceiling explosions animation
ceilingExplosion.anim.PushBack({ 210, 151, 8, 5 });
ceilingExplosion.anim.PushBack({ 190, 148, 19, 8 });
ceilingExplosion.anim.PushBack({ 0, 149, 16, 7 });
ceilingExplosion.anim.PushBack({ 222, 142, 14, 14 });
ceilingExplosion.anim.speed = 0.075f;
ceilingExplosion.speed.x = 1;
ceilingExplosion.anim.loop = false;
ceilingExplosion.isWeapon = true;
return true;
}
bool ModuleParticles::CleanUp()
{
LOG("Unloading particles");
// Delete all remaining active particles on application exit
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
if (particles[i] != nullptr)
{
delete particles[i];
particles[i] = nullptr;
--particlesCount;
}
}
App->textures->Unload(particlesTexture);
--totalTextures;
App->textures->Unload(weaponsParticlesTexture);
--totalTextures;
return true;
}
void ModuleParticles::OnCollision(Collider* c1, Collider* c2)
{
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
// Always destroy particles that collide
if (particles[i] != nullptr && particles[i]->collider == c1)
{
delete particles[i];
particles[i] = nullptr;
--particlesCount;
break;
}
}
}
update_status ModuleParticles::Update()
{
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
Particle* particle = particles[i];
if (particle == nullptr) continue;
// Call particle Update. If it has reached its lifetime, destroy it
if (particle->Update() == false)
{
delete particle;
particles[i] = nullptr;
--particlesCount;
}
}
return update_status::UPDATE_CONTINUE;
}
update_status ModuleParticles::PostUpdate()
{
//Iterating all particle array and drawing any active particles
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
Particle* particle = particles[i];
if (particle != nullptr && particle->isAlive)
{
if (particle->isWeapon) {
App->render->Blit(weaponsParticlesTexture, particle->position.x, particle->position.y, &(particle->anim.GetCurrentFrame()));
}
else {
App->render->Blit(particlesTexture, particle->position.x, particle->position.y, &(particle->anim.GetCurrentFrame()));
}
}
}
return update_status::UPDATE_CONTINUE;
}
void ModuleParticles::AddParticle(const Particle& particle, int x, int y, Collider::Type colliderType, uint delay)
{
for (uint i = 0; i < MAX_ACTIVE_PARTICLES; ++i)
{
//Finding an empty slot for a new particle
if (particles[i] == nullptr)
{
Particle* p = new Particle(particle);
p->frameCount = -(int)delay; // We start the frameCount as the negative delay
p->position.x = x; // so when frameCount reaches 0 the particle will be activated
p->position.y = y;
//Adding the particle's collider
if (colliderType != Collider::Type::NONE) {
p->collider = App->collisions->AddCollider(p->anim.GetCurrentFrame(), colliderType, this);
}
particles[i] = p;
++particlesCount;
break;
}
}
}
Particle::Particle()
{
position.SetToZero();
speed.SetToZero();
}
Particle::Particle(const Particle& p) : anim(p.anim), position(p.position), speed(p.speed),
frameCount(p.frameCount), lifetime(p.lifetime),isWeapon(p.isWeapon)
{
}
Particle::~Particle()
{
if (collider != nullptr)
collider->pendingToDelete = true;
}
bool Particle::Update()
{
bool ret = true;
frameCount++;
// The particle is set to 'alive' when the spawnTime is reached
if (!isAlive && frameCount >= 0)
isAlive = true;
if (isAlive)
{
anim.Update();
// If the particle has a specific lifetime, check when it has to be destroyed
if (lifetime > 0)
{
if (frameCount >= lifetime)
ret = false;
}
// Otherwise the particle is destroyed when the animation is finished
else if (anim.Finished())
ret = false;
// Update the position in the screen
position.x += speed.x;
position.y += speed.y;
if (collider != nullptr)
collider->SetPos(position.x, position.y);
}
return ret;
}