From 55221cca12a45b97ba1d127a21a7454dbe73a540 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 22 Apr 2014 16:52:14 +0200 Subject: [PATCH] Implemented Spawn and Remove function for particles. Also added a Particle component. --- src/Components/Particle.h | 24 ++++++++++ src/Components/ParticleEmitter.h | 11 ++--- src/GameWorld.h | 1 + src/Systems/ParticleSystem.cpp | 47 +++++++++++++++---- src/Systems/ParticleSystem.h | 13 +++-- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 ++ 7 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 src/Components/Particle.h diff --git a/src/Components/Particle.h b/src/Components/Particle.h new file mode 100644 index 0000000..71ac940 --- /dev/null +++ b/src/Components/Particle.h @@ -0,0 +1,24 @@ +#ifndef Components_Particle_h__ +#define Components_Particle_h__ + +#include "System.h" +#include "Component.h" +#include "Components/Transform.h" +#include "Components/ParticleEmitter.h" +#include "Color.h" +#include + +namespace Components +{ + + struct Particle : Component + { + std::vector ColorSpectrum; + std::vector ScaleSpectrum; + double LifeTime; + std::vector VelocitySpectrum; + std::vector AngularVelocitySpectrum; + }; + +} +#endif // !Components_Particle_h__ \ No newline at end of file diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 48248a8..9f5e20f 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -1,10 +1,7 @@ #ifndef Components_ParticleEmitter_h__ #define Components_ParticleEmitter_h__ -#include "System.h" #include "Component.h" -#include "Components/Transform.h" -#include "Components/ParticleEmitter.h" #include "Color.h" #include @@ -13,15 +10,15 @@ namespace Components struct ParticleEmitter : Component { - int ParticleTemplate; + EntityID ParticleTemplate; float SpawnFrequency; int SpawnCount; std::vector ColorSpectrum; std::vector ScaleSpectrum; float SpreadAngle; - float LifeTime; - std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; + double LifeTime; + std::vector VelocitySpectrum; + std::vector AngularVelocitySpectrum; }; } diff --git a/src/GameWorld.h b/src/GameWorld.h index 264ec4d..4f74358 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -20,6 +20,7 @@ #include "Components/Input.h" #include "Components/Model.h" #include "Components/ParticleEmitter.h" +#include "Components/Particle.h" #include "Components/PointLight.h" #include "Components/SoundEmitter.h" #include "Components/Sprite.h" diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index bc23511..d742e5d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -3,9 +3,10 @@ #include "World.h" + void Systems::ParticleSystem::Update(double dt) { - + m_TimeSinceLastSpawn += dt; } void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -13,26 +14,56 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transformComponent = m_World->GetComponent(entity, "Transform"); if(!transformComponent) return; - - - auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); + if(emitterComponent) + { + if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency) + { + SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); + m_TimeSinceLastSpawn = 0; + } + + 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) + { + m_ParticleEmitter[entity].erase(it); + break; + } + else + { + it++; + } + + } + } } void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) { cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); + cf->Register("Particle", []() { return new Components::Particle(); }); } -void Systems::ParticleSystem::SpawnParticles() +void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle) { - for(int i = 0; i < 100; i++) + std::list particles; + for(int i = 0; i < spawnCount; i++) { - auto particle = m_World->CreateEntity(); - auto transform = m_World->AddComponent(particle, "Transform"); + 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; + ParticleData data; + data.ParticleID = ent; + data.TimeLived = 0; + m_ParticleEmitter[emitterID].push_back(data); } } diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 35dfee1..8414a96 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -4,11 +4,17 @@ #include "System.h" #include "Components/Transform.h" #include "Components/ParticleEmitter.h" +#include "Components/Particle.h" namespace Systems { + struct ParticleData + { + EntityID ParticleID; + double TimeLived; + }; class ParticleSystem : public System { @@ -21,12 +27,13 @@ public: void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void Draw(double dt); private: - void SpawnParticles(); + void SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle); + std::map> m_ParticleEmitter; + double m_TimeSinceLastSpawn; }; - } -#endif // ParticleSystem_h__ \ No newline at end of file +#endif // !ParticleSystem_h__ \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 40cd9be..3cfd619 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -127,6 +127,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 185a9c4..35f6ce6 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -220,6 +220,9 @@ Particle System\Systems + + Particle System\Components +