Particle System & Bug Fixes
This commit is contained in:
parent
98d08e622b
commit
d45db29f4e
|
@ -32,17 +32,25 @@
|
||||||
|
|
||||||
void Particle::Update(float deltaTime)
|
void Particle::Update(float deltaTime)
|
||||||
{
|
{
|
||||||
PhysicEntity::Update(deltaTime);
|
|
||||||
|
|
||||||
if(!isActive)
|
if(!isActive)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
remainingLifeTime -= deltaTime;
|
if(IsLifeTimeOver())
|
||||||
|
return;
|
||||||
|
|
||||||
|
PhysicEntity::Update(deltaTime);
|
||||||
|
|
||||||
ChangeColorOverLifeTime();
|
ChangeColorOverLifeTime();
|
||||||
|
remainingLifeTime -= deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Particle::ChangeColorOverLifeTime()
|
void Particle::ChangeColorOverLifeTime()
|
||||||
{
|
{
|
||||||
|
if(lifeTime < 0.0)
|
||||||
|
{
|
||||||
|
vertex.color.a = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
int value = 255 * (remainingLifeTime / lifeTime);
|
int value = 255 * (remainingLifeTime / lifeTime);
|
||||||
vertex.color.a = value;
|
vertex.color.a = value;
|
||||||
}
|
}
|
||||||
|
@ -74,13 +82,23 @@
|
||||||
private:
|
private:
|
||||||
std::vector<Particle> particles;
|
std::vector<Particle> particles;
|
||||||
sf::Color color;
|
sf::Color color;
|
||||||
|
sf::Vector2f localVelo;
|
||||||
|
float rotation;
|
||||||
|
float speed;
|
||||||
int particleSize;
|
int particleSize;
|
||||||
float particleLifeTime;
|
float particleLifeTime;
|
||||||
|
float spawnRate;
|
||||||
|
float spawnTimer;
|
||||||
public:
|
public:
|
||||||
ParticleSystem();
|
ParticleSystem();
|
||||||
void SetSize(int);
|
void SetSize(int);
|
||||||
|
void SetSpeed(float);
|
||||||
|
void SetRotation(float);
|
||||||
|
void SetLocalVelocity(sf::Vector2f);
|
||||||
void SetParticleLifeTime(float);
|
void SetParticleLifeTime(float);
|
||||||
|
void SetSpawnRate(int);
|
||||||
void Update(float);
|
void Update(float);
|
||||||
|
void Play();
|
||||||
Particle &operator[](int);
|
Particle &operator[](int);
|
||||||
int Size();
|
int Size();
|
||||||
};
|
};
|
||||||
|
@ -91,6 +109,8 @@
|
||||||
color = sf::Color::White;
|
color = sf::Color::White;
|
||||||
particleSize = 0;
|
particleSize = 0;
|
||||||
particleLifeTime = 0.1;
|
particleLifeTime = 0.1;
|
||||||
|
spawnRate = 10;
|
||||||
|
spawnTimer = 0.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParticleSystem::Update(float deltaTime)
|
void ParticleSystem::Update(float deltaTime)
|
||||||
|
@ -98,19 +118,47 @@
|
||||||
int i;
|
int i;
|
||||||
Particle *particle;
|
Particle *particle;
|
||||||
|
|
||||||
|
if(spawnTimer > 0.0)
|
||||||
|
spawnTimer -= deltaTime;
|
||||||
|
|
||||||
for (i = 0; i < particleSize; i++)
|
for (i = 0; i < particleSize; i++)
|
||||||
{
|
{
|
||||||
particle = &particles[i];
|
particle = &particles[i];
|
||||||
particle -> Update(deltaTime);
|
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())
|
if(particle -> IsLifeTimeOver())
|
||||||
{
|
{
|
||||||
particle -> ResetRemainingLifeTime();
|
particle -> ResetRemainingLifeTime();
|
||||||
particle -> SetPosition(position.x, position.y, false);
|
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)
|
void ParticleSystem::SetSize(int size)
|
||||||
{
|
{
|
||||||
while(particleSize != 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)
|
void ParticleSystem::SetParticleLifeTime(float lifeTime)
|
||||||
{
|
{
|
||||||
if(lifeTime < 0.1)
|
if(lifeTime < 0.1)
|
||||||
|
|
|
@ -1,11 +1,4 @@
|
||||||
// #include <SFML/Graphics.hpp>
|
|
||||||
// #include <cmath>
|
|
||||||
#include "SynGame.hpp"
|
#include "SynGame.hpp"
|
||||||
// #include "Particles.hpp"
|
|
||||||
// #include "PhysicEntity.hpp"
|
|
||||||
|
|
||||||
#define DegToRad 0.0174533
|
|
||||||
#define RotationLimit 45.0
|
|
||||||
|
|
||||||
class Rocket : public PhysicEntity
|
class Rocket : public PhysicEntity
|
||||||
{
|
{
|
||||||
|
@ -56,15 +49,18 @@ void Rocket::Thrust(float deltaTime)
|
||||||
float force = thrustForce * deltaTime;
|
float force = thrustForce * deltaTime;
|
||||||
velocity.x += sin(rotation * DegToRad) * force;
|
velocity.x += sin(rotation * DegToRad) * force;
|
||||||
velocity.y += cos(rotation * DegToRad) * force;
|
velocity.y += cos(rotation * DegToRad) * force;
|
||||||
|
particleSystem.Play();
|
||||||
particleSystem.SetPosition(position);
|
|
||||||
particleSystem.Update(deltaTime);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Rocket::Update(float deltaTime)
|
void Rocket::Update(float deltaTime)
|
||||||
{
|
{
|
||||||
PhysicEntity::Update(deltaTime);
|
PhysicEntity::Update(deltaTime);
|
||||||
|
|
||||||
|
particleSystem.SetPosition(GetLandingPoint(false));
|
||||||
|
particleSystem.SetRotation(rotation);
|
||||||
|
particleSystem.SetLocalVelocity(velocity);
|
||||||
particleSystem.Update(deltaTime);
|
particleSystem.Update(deltaTime);
|
||||||
|
|
||||||
sprite.setPosition(GetPosition());
|
sprite.setPosition(GetPosition());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
#ifndef SynClasses
|
#ifndef SynClasses
|
||||||
#define SynClasses
|
#define SynClasses
|
||||||
|
|
||||||
|
#define DegToRad 0.0174533
|
||||||
|
#define RotationLimit 45.0
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
#define WindowTitle "Test Window Syntriax"
|
#define WindowTitle "Test Window Syntriax"
|
||||||
|
|
||||||
#define SizeMultiplier 5
|
#define SizeMultiplier 5
|
||||||
#define WinThreshold 1.5
|
#define WinThreshold 2.0
|
||||||
|
|
||||||
sf::Vertex CreateVertex(double, double);
|
sf::Vertex CreateVertex(double, double);
|
||||||
|
|
||||||
|
@ -49,8 +49,10 @@ int main()
|
||||||
player.SetGravity(10 * SizeMultiplier);
|
player.SetGravity(10 * SizeMultiplier);
|
||||||
player.SetPosition(480, -50);
|
player.SetPosition(480, -50);
|
||||||
player.SetActive(true);
|
player.SetActive(true);
|
||||||
player.particleSystem.SetParticleLifeTime(1.5);
|
player.particleSystem.SetParticleLifeTime(2);
|
||||||
player.particleSystem.SetSize(25);
|
player.particleSystem.SetSpawnRate(25);
|
||||||
|
player.particleSystem.SetSize(50);
|
||||||
|
player.particleSystem.SetSpeed(10 * SizeMultiplier);
|
||||||
|
|
||||||
text.setFont(font);
|
text.setFont(font);
|
||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
|
|
Loading…
Reference in New Issue