-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleSystem.cpp
More file actions
146 lines (123 loc) · 3.09 KB
/
ParticleSystem.cpp
File metadata and controls
146 lines (123 loc) · 3.09 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
/*
* ParticleSystem.cpp
*
* Created on: 27/06/2020
* Author: vinicius
*/
#include "ParticleSystem.h"
ParticleSystem::ParticleSystem(const SpawnAreaSpecs& saSpecs, const ParticleSystemSpecs& psSpecs)
: m_pos(saSpecs.position), m_size(saSpecs.size), m_fps(psSpecs.fps), m_amount(psSpecs.amount), m_particleSize(psSpecs.particleSize), m_color(psSpecs.color), m_lifetime(psSpecs.lifetime), m_gravity(psSpecs.gravity), m_speed(psSpecs.speed), m_gravityEffect(psSpecs.gravityEffect), m_visible(psSpecs.visible), m_gap(psSpecs.lifetime / psSpecs.amount)
{
Random::init();
for (unsigned int i = 0; i < m_amount; i++)
{
ParticleSpecs specs = {sf::Vector2f(0.0f, 0.0f), m_particleSize, m_color, m_lifetime, m_gravity, m_speed, m_gravityEffect};
m_particles.emplace_back(specs);
}
restart();
}
ParticleSystem::~ParticleSystem() {}
float ParticleSystem::m_frame2sec(const int& frame) const
{
return frame / m_fps;
}
unsigned int ParticleSystem::m_sec2frame(const float& sec) const
{
return (int)(sec * m_fps);
}
void ParticleSystem::restart()
{
for (unsigned int i = 0; i < m_amount; i++)
{
sf::Vector2f pos(Random::randf() * m_size.x + m_pos.x, Random::randf() * m_size.y + m_pos.y);
m_particles[i].restart(pos);
}
}
void ParticleSystem::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (m_visible)
{
for (unsigned int i = 0; i < m_amount; i++)
{
if (m_particles[i].getVisible())
{
target.draw(m_particles[i], states);
}
}
}
}
void ParticleSystem::updatePhysics(const float& delta)
{
for (unsigned int i = 0; i < m_amount; i++)
{
if (m_particles[i].getFrame() >= m_sec2frame(m_particles[i].getLifetime()))
{
sf::Vector2f pos(Random::randf() * m_size.x + m_pos.x, Random::randf() * m_size.y + m_pos.y);
m_particles[i].restart(pos);
}
m_particles[i].updatePhysics(delta);
}
if (m_frame >= m_sec2frame(m_gap) * (m_currentParticle + 1))
{
m_particles[m_currentParticle].start();
if (m_currentParticle == m_amount - 1)
{
m_currentParticle = 0;
}
else
{
m_currentParticle++;
}
}
m_frame++;
}
void ParticleSystem::setPosition(const sf::Vector2i& pos)
{
m_pos = sf::Vector2f((float)pos.x, (float)pos.y);
updateVariables();
}
void ParticleSystem::setPosition(const sf::Vector2f& pos)
{
m_pos = pos;
updateVariables();
}
void ParticleSystem::setColor(const sf::Color& color)
{
m_color = color;
updateVariables();
}
sf::Color ParticleSystem::getColor() const
{
return m_color;
}
void ParticleSystem::setGravity(const sf::Vector2f& gravity)
{
m_gravity = gravity;
updateVariables();
}
sf::Vector2f ParticleSystem::getGravity() const
{
return m_gravity;
}
void ParticleSystem::hide()
{
m_visible = false;
}
void ParticleSystem::show()
{
m_visible = true;
}
void ParticleSystem::updateVariables()
{
for (unsigned int i = 0; i < m_amount; i++)
{
if (!m_particles[i].getVisible())
{
sf::Vector2f pos(Random::randf() * m_size.x + m_pos.x, Random::randf() * m_size.y + m_pos.y);
m_particles[i].setPosition(pos);
m_particles[i].setFillColor(m_color);
m_particles[i].setGravity(m_gravity);
m_particles[i].setSpeed(m_speed);
}
}
}