diff --git a/Landing/Particles.hpp b/Landing/Particles.hpp index 271f09c..9af8a8e 100644 --- a/Landing/Particles.hpp +++ b/Landing/Particles.hpp @@ -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 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) diff --git a/Landing/Rocket.hpp b/Landing/Rocket.hpp index 0b7a57e..01517f6 100644 --- a/Landing/Rocket.hpp +++ b/Landing/Rocket.hpp @@ -1,11 +1,4 @@ -// #include -// #include #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()); } diff --git a/Landing/SynGame.hpp b/Landing/SynGame.hpp index bf8f8c1..7c4223d 100644 --- a/Landing/SynGame.hpp +++ b/Landing/SynGame.hpp @@ -1,5 +1,9 @@ #ifndef SynClasses #define SynClasses + + #define DegToRad 0.0174533 + #define RotationLimit 45.0 + #include #include #include diff --git a/Landing/main.cpp b/Landing/main.cpp index 6ea4688..91e8d61 100644 --- a/Landing/main.cpp +++ b/Landing/main.cpp @@ -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);