This repository was archived by the owner on Sep 29, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathparticles.cpp
More file actions
60 lines (51 loc) · 1.91 KB
/
Copy pathparticles.cpp
File metadata and controls
60 lines (51 loc) · 1.91 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
#include "particles.h"
#include "service_locator.h"
ParticlesSource::ParticlesSource(sf::Vector2f position, int level) :
Drawable(level), position(position)
{
servLoc.getRender()->addObj(this);
}
ParticlesSource::~ParticlesSource()
{
servLoc.getRender()->removeObj(this);
}
void ParticlesSource::draw(sf::RenderWindow *window)
{
sf::VertexArray points(sf::Points, particles.size());
for(unsigned int i = 0; i < particles.size(); i++)
{
points[i].position = particles[i].position;
points[i].color = particles[i].color;
}
window->draw(points);
}
void ParticlesSource::update(sf::Time dt)
{
for(unsigned int i = 0; i < particles.size(); i++)
{
particles[i].life -= dt;
if(particles[i].life <= sf::Time::Zero)
{
particles.erase(particles.begin()+i);
continue;
}
particles[i].position += particles[i].velocity;
}
}
void ParticlesSource::createParticles(unsigned count, sf::Vector2f baseVelocity, float velocityDelta,
sf::Color color, int colorDelta, sf::Time life, float lifeDelta)
{
std::uniform_real_distribution<float> velRandDist(-velocityDelta, +velocityDelta);
std::uniform_int_distribution<int> colRandDist(-colorDelta, +colorDelta);
std::uniform_real_distribution<float> lifeRandDist(-lifeDelta, +lifeDelta);
for(unsigned int i = 0; i < count; i++)
{
sf::Vector2f finalVel;
sf::Color finalColor;
sf::Time finalLifetime;
finalVel = {baseVelocity.x + velRandDist(randEngine), baseVelocity.y + velRandDist(randEngine)};
finalColor = sf::Color(color.r + colRandDist(randEngine), color.g + colRandDist(randEngine), color.b + colRandDist(randEngine));
finalLifetime = sf::Time(life + sf::seconds(lifeRandDist(randEngine)));
particles.emplace_back(this->position, finalVel, finalColor, finalLifetime);
}
}