Implemented Spawn and Remove function for particles. Also added a Particle component.
This commit is contained in:
@@ -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<Components::Transform>(entity, "Transform");
|
||||
if(!transformComponent)
|
||||
return;
|
||||
|
||||
|
||||
|
||||
|
||||
auto emitterComponent = m_World->GetComponent<Components::ParticleEmitter>(entity, "ParticleEmitter");
|
||||
if(emitterComponent)
|
||||
{
|
||||
if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency)
|
||||
{
|
||||
SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle);
|
||||
m_TimeSinceLastSpawn = 0;
|
||||
}
|
||||
|
||||
std::list<ParticleData>::iterator it;
|
||||
for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();)
|
||||
{
|
||||
it->TimeLived += dt;
|
||||
auto particleComponent = m_World->GetComponent<Components::Particle>(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<EntityID> particles;
|
||||
for(int i = 0; i < spawnCount; i++)
|
||||
{
|
||||
auto particle = m_World->CreateEntity();
|
||||
auto transform = m_World->AddComponent<Components::Transform>(particle, "Transform");
|
||||
auto ent = m_World->CreateEntity();
|
||||
auto transform = m_World->AddComponent<Components::Transform>(ent, "Transform");
|
||||
transform->Position = glm::vec3(0);
|
||||
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
||||
particle->LifeTime = 4;
|
||||
ParticleData data;
|
||||
data.ParticleID = ent;
|
||||
data.TimeLived = 0;
|
||||
m_ParticleEmitter[emitterID].push_back(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user