Particle System & Bug Fixes

This commit is contained in:
Asrın Doğan 2020-01-07 19:44:40 +03:00
parent 98d08e622b
commit d45db29f4e
4 changed files with 83 additions and 17 deletions

View File

@ -32,17 +32,25 @@
void Particle::Update(float deltaTime)
{
PhysicEntity::Update(deltaTime);
if(!isActive)
return;
remainingLifeTime -= deltaTime;
if(IsLifeTimeOver())
return;
PhysicEntity::Update(deltaTime);
ChangeColorOverLifeTime();
remainingLifeTime -= deltaTime;
}
void Particle::ChangeColorOverLifeTime()
{
if(lifeTime < 0.0)
{
vertex.color.a = 0;
return;
}
int value = 255 * (remainingLifeTime / lifeTime);
vertex.color.a = value;
}
@ -74,13 +82,23 @@
private:
std::vector<Particle> particles;
sf::Color color;
sf::Vector2f localVelo;
float rotation;
float speed;
int particleSize;
float particleLifeTime;
float spawnRate;
float spawnTimer;
public:
ParticleSystem();
void SetSize(int);
void SetSpeed(float);
void SetRotation(float);
void SetLocalVelocity(sf::Vector2f);
void SetParticleLifeTime(float);
void SetSpawnRate(int);
void Update(float);
void Play();
Particle &operator[](int);
int Size();
};
@ -91,6 +109,8 @@
color = sf::Color::White;
particleSize = 0;
particleLifeTime = 0.1;
spawnRate = 10;
spawnTimer = 0.0;
}
void ParticleSystem::Update(float deltaTime)
@ -98,19 +118,47 @@
int i;
Particle *particle;
if(spawnTimer > 0.0)
spawnTimer -= deltaTime;
for (i = 0; i < particleSize; i++)
{
particle = &particles[i];
particle -> Update(deltaTime);
}
}
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];
if(particle -> IsLifeTimeOver())
{
particle -> ResetRemainingLifeTime();
particle -> SetPosition(position.x, position.y, false);
particle -> SetVelocity(sf::Vector2f(Random(-10.0, 10.0), Random(-10.0, 10.0)));
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;
}
}
}
void ParticleSystem::SetSpawnRate(int rate)
{
spawnRate = rate;
}
void ParticleSystem::SetSize(int size)
{
while(particleSize != size)
@ -128,6 +176,22 @@
}
}
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;
}
void ParticleSystem::SetParticleLifeTime(float lifeTime)
{
if(lifeTime < 0.1)

View File

@ -1,11 +1,4 @@
// #include <SFML/Graphics.hpp>
// #include <cmath>
#include "SynGame.hpp"
// #include "Particles.hpp"
// #include "PhysicEntity.hpp"
#define DegToRad 0.0174533
#define RotationLimit 45.0
class Rocket : public PhysicEntity
{
@ -56,15 +49,18 @@ void Rocket::Thrust(float deltaTime)
float force = thrustForce * deltaTime;
velocity.x += sin(rotation * DegToRad) * force;
velocity.y += cos(rotation * DegToRad) * force;
particleSystem.SetPosition(position);
particleSystem.Update(deltaTime);
particleSystem.Play();
}
void Rocket::Update(float deltaTime)
{
PhysicEntity::Update(deltaTime);
particleSystem.SetPosition(GetLandingPoint(false));
particleSystem.SetRotation(rotation);
particleSystem.SetLocalVelocity(velocity);
particleSystem.Update(deltaTime);
sprite.setPosition(GetPosition());
}

View File

@ -1,5 +1,9 @@
#ifndef SynClasses
#define SynClasses
#define DegToRad 0.0174533
#define RotationLimit 45.0
#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>

View File

@ -8,7 +8,7 @@
#define WindowTitle "Test Window Syntriax"
#define SizeMultiplier 5
#define WinThreshold 1.5
#define WinThreshold 2.0
sf::Vertex CreateVertex(double, double);
@ -49,8 +49,10 @@ int main()
player.SetGravity(10 * SizeMultiplier);
player.SetPosition(480, -50);
player.SetActive(true);
player.particleSystem.SetParticleLifeTime(1.5);
player.particleSystem.SetSize(25);
player.particleSystem.SetParticleLifeTime(2);
player.particleSystem.SetSpawnRate(25);
player.particleSystem.SetSize(50);
player.particleSystem.SetSpeed(10 * SizeMultiplier);
text.setFont(font);
window.setVerticalSyncEnabled(true);