Added support for multiple emitters. (ParticleEmitter component now friend class to ParticleSystem)
This commit is contained in:
@@ -5,11 +5,15 @@
|
|||||||
#include "Color.h"
|
#include "Color.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
namespace Systems { class ParticleSystem; }
|
||||||
|
|
||||||
namespace Components
|
namespace Components
|
||||||
{
|
{
|
||||||
|
|
||||||
struct ParticleEmitter : Component
|
struct ParticleEmitter : Component
|
||||||
{
|
{
|
||||||
|
friend class Systems::ParticleSystem;
|
||||||
|
|
||||||
EntityID ParticleTemplate;
|
EntityID ParticleTemplate;
|
||||||
float SpawnFrequency;
|
float SpawnFrequency;
|
||||||
int SpawnCount;
|
int SpawnCount;
|
||||||
@@ -19,6 +23,9 @@ struct ParticleEmitter : Component
|
|||||||
double LifeTime;
|
double LifeTime;
|
||||||
std::vector<glm::vec3> VelocitySpectrum;
|
std::vector<glm::vec3> VelocitySpectrum;
|
||||||
std::vector<glm::vec3> AngularVelocitySpectrum;
|
std::vector<glm::vec3> AngularVelocitySpectrum;
|
||||||
|
|
||||||
|
private:
|
||||||
|
double TimeSinceLastSpawn;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-11
@@ -77,7 +77,7 @@ void GameWorld::Initialize()
|
|||||||
auto model = AddComponent<Components::Model>(ball, "Model");
|
auto model = AddComponent<Components::Model>(ball, "Model");
|
||||||
model->ModelFile = "Models/Placeholders/PhysicsTest/Sphere.obj";
|
model->ModelFile = "Models/Placeholders/PhysicsTest/Sphere.obj";
|
||||||
auto sphere = AddComponent<Components::Sphere>(ball, "Sphere");
|
auto sphere = AddComponent<Components::Sphere>(ball, "Sphere");
|
||||||
sphere->Radius = 0.5;
|
sphere->Radius = 0.05;
|
||||||
auto physics = AddComponent<Components::Physics>(ball, "Physics");
|
auto physics = AddComponent<Components::Physics>(ball, "Physics");
|
||||||
physics->Mass = 1;
|
physics->Mass = 1;
|
||||||
}
|
}
|
||||||
@@ -92,16 +92,21 @@ void GameWorld::Initialize()
|
|||||||
}*/
|
}*/
|
||||||
|
|
||||||
{
|
{
|
||||||
// Particle emitter
|
for(int i = 0; i < 4 ; i++)
|
||||||
auto ent = CreateEntity();
|
{
|
||||||
auto transform = AddComponent<Components::Transform>(ent, "Transform");
|
// Particle emitter
|
||||||
transform->Position = glm::vec3(0);
|
auto ent = CreateEntity();
|
||||||
auto emitter = AddComponent<Components::ParticleEmitter>(ent, "ParticleEmitter");
|
auto transform = AddComponent<Components::Transform>(ent, "Transform");
|
||||||
emitter->LifeTime = 4;
|
transform->Position = glm::vec3(i * 10, 20, 0);
|
||||||
emitter->SpawnCount = 1;
|
auto emitter = AddComponent<Components::ParticleEmitter>(ent, "ParticleEmitter");
|
||||||
emitter->SpreadAngle = 35;
|
emitter->LifeTime = 2;
|
||||||
emitter->SpawnFrequency = 0.05;
|
emitter->SpawnCount = 1;
|
||||||
//emitter->
|
emitter->SpreadAngle = 35;
|
||||||
|
emitter->SpawnFrequency = 2;
|
||||||
|
auto model = AddComponent<Components::Model>(ent, "Model");
|
||||||
|
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
|
||||||
|
//emitter->
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,12 +6,12 @@
|
|||||||
|
|
||||||
Systems::ParticleSystem::ParticleSystem(World *m_World) : System(m_World)
|
Systems::ParticleSystem::ParticleSystem(World *m_World) : System(m_World)
|
||||||
{
|
{
|
||||||
m_TimeSinceLastSpawn = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::ParticleSystem::Update(double dt)
|
void Systems::ParticleSystem::Update(double dt)
|
||||||
{
|
{
|
||||||
m_TimeSinceLastSpawn += dt;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||||
@@ -23,14 +23,13 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
auto emitterComponent = m_World->GetComponent<Components::ParticleEmitter>(entity, "ParticleEmitter");
|
auto emitterComponent = m_World->GetComponent<Components::ParticleEmitter>(entity, "ParticleEmitter");
|
||||||
if(emitterComponent)
|
if(emitterComponent)
|
||||||
{
|
{
|
||||||
if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency)
|
emitterComponent->TimeSinceLastSpawn += dt;
|
||||||
|
|
||||||
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
|
||||||
|
if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency)
|
||||||
{
|
{
|
||||||
//if(m_ParticleEmitter[entity].size() < 100) // TEMP
|
SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle);
|
||||||
{
|
emitterComponent->TimeSinceLastSpawn = 0;
|
||||||
SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle);
|
|
||||||
m_TimeSinceLastSpawn = 0;
|
|
||||||
}
|
|
||||||
//std::cout<<"Particle count: "<<m_ParticleEmitter[entity].size()<<std::endl; // TEMP
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::list<ParticleData>::iterator it;
|
std::list<ParticleData>::iterator it;
|
||||||
@@ -54,7 +53,7 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID, "Transform");
|
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID, "Transform");
|
||||||
float speed = 10 * dt;
|
float speed = 20 * dt;
|
||||||
transformComponent->Position.x += it->Direction.x * speed;
|
transformComponent->Position.x += it->Direction.x * speed;
|
||||||
transformComponent->Position.y += it->Direction.y * speed;
|
transformComponent->Position.y += it->Direction.y * speed;
|
||||||
transformComponent->Position.z += it->Direction.z * speed;
|
transformComponent->Position.z += it->Direction.z * speed;
|
||||||
@@ -92,14 +91,16 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf)
|
|||||||
cf->Register("Particle", []() { return new Components::Particle(); });
|
cf->Register("Particle", []() { return new Components::Particle(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle)
|
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle)
|
||||||
{
|
{
|
||||||
for(int i = 0; i < spawnCount; i++)
|
for(int i = 0; i < spawnCount; i++)
|
||||||
{
|
{
|
||||||
auto ent = m_World->CreateEntity();
|
auto ent = m_World->CreateEntity();
|
||||||
|
|
||||||
auto transform = m_World->AddComponent<Components::Transform>(ent, "Transform");
|
auto transform = m_World->AddComponent<Components::Transform>(ent, "Transform");
|
||||||
transform->Position = glm::vec3(0, 20, 0);
|
transform->Position.x = pos.x;
|
||||||
|
transform->Position.y = pos.y;
|
||||||
|
transform->Position.z = pos.z;
|
||||||
transform->Scale = glm::vec3(1, 1, 0);
|
transform->Scale = glm::vec3(1, 1, 0);
|
||||||
|
|
||||||
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
auto particle = m_World->AddComponent<Components::Particle>(ent, "Particle");
|
||||||
@@ -134,7 +135,6 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun
|
|||||||
|
|
||||||
m_ParticleEmitter[emitterID].push_back(data);
|
m_ParticleEmitter[emitterID].push_back(data);
|
||||||
}
|
}
|
||||||
//std::cout<<"Added "<<spawnCount<<" particles..."<<std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -34,9 +34,9 @@ public:
|
|||||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||||
void Draw(double dt);
|
void Draw(double dt);
|
||||||
private:
|
private:
|
||||||
void SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle);
|
void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle);
|
||||||
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
||||||
double m_TimeSinceLastSpawn;
|
std::map<EntityID, double> m_TimeSinceLastSpawn;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user