2020-01-05 02:04:06 +03:00
|
|
|
#include "SynGame.hpp"
|
|
|
|
|
|
|
|
#pragma region Particle
|
|
|
|
class Particle : public PhysicEntity
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
sf::Vertex vertex;
|
|
|
|
float lifeTime;
|
|
|
|
float remainingLifeTime;
|
|
|
|
void ChangeColorOverLifeTime();
|
|
|
|
public:
|
|
|
|
Particle();
|
|
|
|
Particle(sf::Color);
|
|
|
|
void Update(float);
|
|
|
|
void ResetRemainingLifeTime();
|
|
|
|
void SetLifeTime(float);
|
|
|
|
bool IsLifeTimeOver();
|
|
|
|
sf::Vertex &GetVertex();
|
|
|
|
};
|
|
|
|
|
|
|
|
Particle::Particle() : PhysicEntity()
|
|
|
|
{
|
|
|
|
vertex.color = sf::Color::White;
|
|
|
|
lifeTime = remainingLifeTime = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Particle::Particle(sf::Color color) : PhysicEntity()
|
|
|
|
{
|
|
|
|
vertex.color = color;
|
|
|
|
lifeTime = remainingLifeTime = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::Update(float deltaTime)
|
|
|
|
{
|
|
|
|
if(!isActive)
|
|
|
|
return;
|
|
|
|
|
2020-01-07 19:44:40 +03:00
|
|
|
if(IsLifeTimeOver())
|
|
|
|
return;
|
|
|
|
|
|
|
|
PhysicEntity::Update(deltaTime);
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
ChangeColorOverLifeTime();
|
2020-01-07 19:44:40 +03:00
|
|
|
remainingLifeTime -= deltaTime;
|
2020-01-05 02:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::ChangeColorOverLifeTime()
|
|
|
|
{
|
2020-01-07 19:44:40 +03:00
|
|
|
if(lifeTime < 0.0)
|
|
|
|
{
|
|
|
|
vertex.color.a = 0;
|
|
|
|
return;
|
|
|
|
}
|
2020-01-05 02:04:06 +03:00
|
|
|
int value = 255 * (remainingLifeTime / lifeTime);
|
|
|
|
vertex.color.a = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::ResetRemainingLifeTime()
|
|
|
|
{
|
|
|
|
remainingLifeTime = lifeTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::SetLifeTime(float time)
|
|
|
|
{
|
|
|
|
remainingLifeTime = lifeTime = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Particle::IsLifeTimeOver()
|
|
|
|
{
|
|
|
|
return remainingLifeTime <= 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sf::Vertex &Particle::GetVertex()
|
|
|
|
{
|
|
|
|
vertex.position = position;
|
|
|
|
return vertex;
|
|
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
#pragma region Particle System
|
|
|
|
class ParticleSystem : public Entity
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::vector<Particle> particles;
|
|
|
|
sf::Color color;
|
2020-01-07 19:44:40 +03:00
|
|
|
sf::Vector2f localVelo;
|
|
|
|
float rotation;
|
|
|
|
float speed;
|
2020-01-05 02:04:06 +03:00
|
|
|
int particleSize;
|
|
|
|
float particleLifeTime;
|
2020-01-07 19:44:40 +03:00
|
|
|
float spawnRate;
|
|
|
|
float spawnTimer;
|
2020-01-05 02:04:06 +03:00
|
|
|
public:
|
|
|
|
ParticleSystem();
|
|
|
|
void SetSize(int);
|
2020-01-07 19:44:40 +03:00
|
|
|
void SetSpeed(float);
|
|
|
|
void SetRotation(float);
|
|
|
|
void SetLocalVelocity(sf::Vector2f);
|
2020-01-05 02:04:06 +03:00
|
|
|
void SetParticleLifeTime(float);
|
2020-01-07 19:44:40 +03:00
|
|
|
void SetSpawnRate(int);
|
2020-01-05 02:04:06 +03:00
|
|
|
void Update(float);
|
2020-01-07 19:44:40 +03:00
|
|
|
void Play();
|
2020-01-05 02:04:06 +03:00
|
|
|
Particle &operator[](int);
|
|
|
|
int Size();
|
|
|
|
};
|
|
|
|
|
|
|
|
ParticleSystem::ParticleSystem()
|
|
|
|
{
|
|
|
|
particles.clear();
|
|
|
|
color = sf::Color::White;
|
|
|
|
particleSize = 0;
|
|
|
|
particleLifeTime = 0.1;
|
2020-01-07 19:44:40 +03:00
|
|
|
spawnRate = 10;
|
|
|
|
spawnTimer = 0.0;
|
2020-01-05 02:04:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::Update(float deltaTime)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Particle *particle;
|
|
|
|
|
2020-01-07 19:44:40 +03:00
|
|
|
if(spawnTimer > 0.0)
|
|
|
|
spawnTimer -= deltaTime;
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
for (i = 0; i < particleSize; i++)
|
|
|
|
{
|
|
|
|
particle = &particles[i];
|
2020-01-05 11:34:06 +03:00
|
|
|
particle -> Update(deltaTime);
|
2020-01-07 19:44:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::Play()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
float spawnRotation;
|
|
|
|
sf::Vector2f spawnVelocity;
|
|
|
|
Particle *particle;
|
|
|
|
|
|
|
|
if(spawnTimer > 0.0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < particleSize; i++)
|
|
|
|
{
|
|
|
|
particle = &particles[i];
|
2020-01-05 02:04:06 +03:00
|
|
|
if(particle -> IsLifeTimeOver())
|
|
|
|
{
|
|
|
|
particle -> ResetRemainingLifeTime();
|
|
|
|
particle -> SetPosition(position.x, position.y, false);
|
2020-01-07 19:44:40 +03:00
|
|
|
spawnRotation = Random(-0.785, 0.785) - DegToRad * rotation; // 0.785 3.14(pi)/4
|
|
|
|
spawnVelocity = sf::Vector2f(sin(spawnRotation), cos(spawnRotation)) * speed;
|
|
|
|
particle -> SetVelocity(spawnVelocity + localVelo);
|
|
|
|
spawnTimer = 1 / spawnRate;
|
|
|
|
break;
|
2020-01-05 02:04:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 19:44:40 +03:00
|
|
|
void ParticleSystem::SetSpawnRate(int rate)
|
|
|
|
{
|
|
|
|
spawnRate = rate;
|
|
|
|
}
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
void ParticleSystem::SetSize(int size)
|
|
|
|
{
|
|
|
|
while(particleSize != size)
|
|
|
|
if(particleSize > size)
|
|
|
|
{
|
|
|
|
delete &particles[--particleSize];
|
|
|
|
particles.pop_back();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Particle newParticle = Particle(color);
|
|
|
|
newParticle.SetLifeTime(particleLifeTime);
|
|
|
|
particles.push_back(newParticle);
|
|
|
|
particleSize++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 19:44:40 +03:00
|
|
|
void ParticleSystem::SetSpeed(float speed)
|
|
|
|
{
|
|
|
|
this -> speed = speed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::SetRotation(float rotation)
|
|
|
|
{
|
|
|
|
this -> rotation = rotation;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::SetLocalVelocity(sf::Vector2f velo)
|
|
|
|
{
|
|
|
|
velo.y *= -1;
|
|
|
|
localVelo = velo;
|
|
|
|
}
|
|
|
|
|
2020-01-05 02:04:06 +03:00
|
|
|
void ParticleSystem::SetParticleLifeTime(float lifeTime)
|
|
|
|
{
|
|
|
|
if(lifeTime < 0.1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
particleLifeTime = lifeTime;
|
|
|
|
for (i = 0; i < particleSize; i++)
|
|
|
|
particles[i].SetLifeTime(lifeTime);
|
|
|
|
}
|
|
|
|
|
|
|
|
Particle &ParticleSystem::operator[](int index)
|
|
|
|
{
|
|
|
|
return particles[index];
|
|
|
|
}
|
|
|
|
|
|
|
|
int ParticleSystem::Size()
|
|
|
|
{
|
|
|
|
return particles.size();
|
|
|
|
}
|
|
|
|
#pragma endregion
|