/
ParticleManager.cpp
101 lines (73 loc) · 1.92 KB
/
ParticleManager.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
#include "ParticleManager.hpp"
ImageParticle::ImageParticle() {
}
ImageParticle::ImageParticle(uint16_t sprite_index, float x, float y, float x_vel, float y_vel, float x_grav, float y_grav, float angle, float spin, float scale, float alpha, float fade) {
this->sprite_index = sprite_index;
this->x = x;
this->y = y;
this->x_vel = x_vel;
this->y_vel = y_vel;
this->x_grav = x_grav;
this->y_grav = y_grav;
this->angle = angle;
this->spin = spin;
this->scale = scale;
this->alpha = alpha;
this->fade = fade;
}
void ImageParticle::update(float dt) {
x_vel += x_grav * dt;
y_vel += y_grav * dt;
x += x_vel * dt;
y += y_vel * dt;
angle += spin * dt;
alpha += fade * dt;
if (alpha > 255.0f) {
alpha = 255.0f;
}
else if (alpha < 0.0f) {
alpha = 0.0f;
}
}
void ImageParticle::render(Spritesheet& spritesheet) {
uint8_t old_alpha = spritesheet.get_alpha();
spritesheet.set_alpha(alpha);
spritesheet.sprite(sprite_index, x, y, scale, angle, NULL, SDL_FLIP_NONE);
spritesheet.set_alpha(old_alpha);
}
float ImageParticle::get_x() {
return x;
}
float ImageParticle::get_y() {
return y;
}
float ImageParticle::get_scale() {
return scale;
}
float ImageParticle::get_alpha() {
return alpha;
}
ParticleHandler::ParticleHandler() {
}
void ParticleHandler::update(float dt) {
for (ImageParticle& particle : image_particles) {
particle.update(dt);
}
}
void ParticleHandler::render(Spritesheet& spritesheet) {
for (ImageParticle& particle : image_particles) {
particle.render(spritesheet);
}
}
void ParticleHandler::add(ImageParticle particle) {
image_particles.push_back(particle);
}
void ParticleHandler::remove_if(bool (*condition) (ImageParticle&)) {
image_particles.erase(std::remove_if(image_particles.begin(), image_particles.end(), condition), image_particles.end());
}
void ParticleHandler::clear() {
image_particles.clear();
}
uint16_t ParticleHandler::count_particles() {
return image_particles.size();
}