-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleParticles.cpp
More file actions
152 lines (126 loc) · 3.25 KB
/
ModuleParticles.cpp
File metadata and controls
152 lines (126 loc) · 3.25 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
#include <math.h>
#include "Globals.h"
#include "Application.h"
#include "ModuleParticles.h"
ModuleParticles::ModuleParticles(Application* app, bool start_enabled) : Module(app, start_enabled), graphics(NULL)
{}
ModuleParticles::~ModuleParticles()
{}
// Load assets
bool ModuleParticles::Start()
{
LOG("Loading particles");
graphics = App->textures->Load("rtype/particles.png");
// Explosion particle
explosion.fx = App->audio->LoadFx("rtype/explosion.wav");
explosion.anim.frames.PushBack({274, 296, 33, 30});
explosion.anim.frames.PushBack({313, 296, 33, 30});
explosion.anim.frames.PushBack({346, 296, 33, 30});
explosion.anim.frames.PushBack({382, 296, 33, 30});
explosion.anim.frames.PushBack({419, 296, 33, 30});
explosion.anim.frames.PushBack({457, 296, 33, 30});
explosion.anim.loop = false;
explosion.anim.speed = 0.3f;
// Laser particle
laser.fx = App->audio->LoadFx("rtype/slimeball.wav");
laser.anim.frames.PushBack({200, 120, 32, 12});
laser.anim.frames.PushBack({230, 120, 32, 12});
laser.speed.x = 7;
laser.life = 1000;
laser.anim.speed = 0.05f;
return true;
}
// Unload assets
bool ModuleParticles::CleanUp()
{
LOG("Unloading particles");
App->textures->Unload(graphics);
return true;
}
// Update: draw background
update_status ModuleParticles::Update()
{
p2List_item<Particle*>* tmp = active.getFirst();
p2List_item<Particle*>* tmp_next = active.getFirst();
while(tmp != NULL)
{
Particle* p = tmp->data;
tmp_next = tmp->next;
if(p->Update() == false)
{
active.del(tmp);
delete p;
}
else if(SDL_GetTicks() >= p->born)
{
App->renderer->Blit(graphics, p->position.x, p->position.y, &(p->anim.GetCurrentFrame()));
if(p->fx_played == false)
{
p->fx_played = true;
App->audio->PlayFx(p->fx);
}
}
tmp = tmp_next;
}
return UPDATE_CONTINUE;
}
// Always destroy particles that collide
void ModuleParticles::OnCollision(Collider* c1, Collider* c2)
{
p2List_item<Particle*>* tmp = active.getFirst();
while(tmp != NULL)
{
if(tmp->data->collider == c1 )
{
delete tmp->data;
active.del(tmp);
break;
}
tmp = tmp->next;
}
}
void ModuleParticles::AddParticle(const Particle& particle, int x, int y, COLLIDER_TYPE collider_type, Uint32 delay)
{
Particle* p = new Particle(particle);
p->born = SDL_GetTicks() + delay;
p->position.x = x;
p->position.y = y;
if(collider_type != COLLIDER_NONE)
{
p->collider = App->collision->AddCollider({p->position.x, p->position.y, 0, 0}, collider_type, this);
}
active.add(p);
}
// -------------------------------------------------------------
// -------------------------------------------------------------
Particle::Particle() : fx(0), born(0), life(0), fx_played(false), collider(NULL)
{
position.SetToZero();
speed.SetToZero();
}
Particle::Particle(const Particle& p) : anim(p.anim), position(p.position), speed(p.speed), fx_played(false), collider(p.collider)
{
fx = p.fx;
born = p.born;
life = p.life;
}
bool Particle::Update()
{
bool ret = true;
if(life > 0)
{
if((SDL_GetTicks() - born) > life)
ret = false;
}
else
if(anim.Finished())
ret = false;
position.x += speed.x;
position.y += speed.y;
if(collider != NULL)
{
SDL_Rect r = anim.PeekCurrentFrame();
collider->rect = {position.x, position.y, r.w, r.h};
}
return ret;
}