diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index d742e5d..8488d3d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -27,9 +27,10 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID std::list::iterator it; for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();) { - it->TimeLived += dt; auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); - if(it->TimeLived > particleComponent->LifeTime) + + int timeLived = glfwGetTime() - it->SpawnTime; + if(timeLived > particleComponent->LifeTime) { m_ParticleEmitter[entity].erase(it); break; @@ -39,7 +40,20 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID it++; } + // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. + // How big fraction the color is multiplied with + float timeProgress = timeLived / particleComponent->LifeTime; + // The difference between the start and end value + float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); + it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; + deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); + it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; + deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); + it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress; + //Interpolates the scale for the particle + float deltaScale = glm::abs(particleComponent->ScaleSpectrum[0] - particleComponent->ScaleSpectrum[1]); + it->Scale = particleComponent->ScaleSpectrum[0] + deltaScale * timeProgress; } } } @@ -56,13 +70,25 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); + auto transform = m_World->AddComponent(ent, "Transform"); transform->Position = glm::vec3(0); + auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = 4; + Color startColor = {.4f, .45f, .2f}; + particle->ColorSpectrum.push_back(startColor); + Color endColor = {0.f, 45.f, 23.f}; + particle->ColorSpectrum.push_back(endColor); + particle->ScaleSpectrum.push_back(1); + particle->ScaleSpectrum.push_back(30); + ParticleData data; data.ParticleID = ent; - data.TimeLived = 0; + data.SpawnTime = glfwGetTime(); + data.color = particle->ColorSpectrum[0]; + data.Scale = particle->ScaleSpectrum[0]; + m_ParticleEmitter[emitterID].push_back(data); } } diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 8414a96..84674e6 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -5,6 +5,8 @@ #include "Components/Transform.h" #include "Components/ParticleEmitter.h" #include "Components/Particle.h" +#include "Color.h" +#include namespace Systems @@ -13,7 +15,9 @@ namespace Systems struct ParticleData { EntityID ParticleID; - double TimeLived; + double SpawnTime; + float Scale; + Color color; }; class ParticleSystem : public System