Implemented Spawn and Remove function for particles. Also added a Particle component.
This commit is contained in:
@@ -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 <vector>
|
||||||
|
|
||||||
|
namespace Components
|
||||||
|
{
|
||||||
|
|
||||||
|
struct Particle : Component
|
||||||
|
{
|
||||||
|
std::vector<Color> ColorSpectrum;
|
||||||
|
std::vector<float> ScaleSpectrum;
|
||||||
|
double LifeTime;
|
||||||
|
std::vector<glm::vec3> VelocitySpectrum;
|
||||||
|
std::vector<glm::vec3> AngularVelocitySpectrum;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
#endif // !Components_Particle_h__
|
||||||
@@ -1,10 +1,7 @@
|
|||||||
#ifndef Components_ParticleEmitter_h__
|
#ifndef Components_ParticleEmitter_h__
|
||||||
#define Components_ParticleEmitter_h__
|
#define Components_ParticleEmitter_h__
|
||||||
|
|
||||||
#include "System.h"
|
|
||||||
#include "Component.h"
|
#include "Component.h"
|
||||||
#include "Components/Transform.h"
|
|
||||||
#include "Components/ParticleEmitter.h"
|
|
||||||
#include "Color.h"
|
#include "Color.h"
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -13,15 +10,15 @@ namespace Components
|
|||||||
|
|
||||||
struct ParticleEmitter : Component
|
struct ParticleEmitter : Component
|
||||||
{
|
{
|
||||||
int ParticleTemplate;
|
EntityID ParticleTemplate;
|
||||||
float SpawnFrequency;
|
float SpawnFrequency;
|
||||||
int SpawnCount;
|
int SpawnCount;
|
||||||
std::vector<Color> ColorSpectrum;
|
std::vector<Color> ColorSpectrum;
|
||||||
std::vector<float> ScaleSpectrum;
|
std::vector<float> ScaleSpectrum;
|
||||||
float SpreadAngle;
|
float SpreadAngle;
|
||||||
float LifeTime;
|
double LifeTime;
|
||||||
std::vector<float[3]> VelocitySpectrum;
|
std::vector<glm::vec3> VelocitySpectrum;
|
||||||
std::vector<float[3]> AngularVelocitySpectrum;
|
std::vector<glm::vec3> AngularVelocitySpectrum;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
#include "Components/Input.h"
|
#include "Components/Input.h"
|
||||||
#include "Components/Model.h"
|
#include "Components/Model.h"
|
||||||
#include "Components/ParticleEmitter.h"
|
#include "Components/ParticleEmitter.h"
|
||||||
|
#include "Components/Particle.h"
|
||||||
#include "Components/PointLight.h"
|
#include "Components/PointLight.h"
|
||||||
#include "Components/SoundEmitter.h"
|
#include "Components/SoundEmitter.h"
|
||||||
#include "Components/Sprite.h"
|
#include "Components/Sprite.h"
|
||||||
|
|||||||
@@ -3,9 +3,10 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "World.h"
|
#include "World.h"
|
||||||
|
|
||||||
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)
|
||||||
@@ -13,26 +14,56 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
|
|||||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity, "Transform");
|
||||||
if(!transformComponent)
|
if(!transformComponent)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
auto emitterComponent = m_World->GetComponent<Components::ParticleEmitter>(entity, "ParticleEmitter");
|
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)
|
void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf)
|
||||||
{
|
{
|
||||||
cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); });
|
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 ent = m_World->CreateEntity();
|
||||||
auto transform = m_World->AddComponent<Components::Transform>(particle, "Transform");
|
auto transform = m_World->AddComponent<Components::Transform>(ent, "Transform");
|
||||||
transform->Position = glm::vec3(0);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,11 +4,17 @@
|
|||||||
#include "System.h"
|
#include "System.h"
|
||||||
#include "Components/Transform.h"
|
#include "Components/Transform.h"
|
||||||
#include "Components/ParticleEmitter.h"
|
#include "Components/ParticleEmitter.h"
|
||||||
|
#include "Components/Particle.h"
|
||||||
|
|
||||||
|
|
||||||
namespace Systems
|
namespace Systems
|
||||||
{
|
{
|
||||||
|
|
||||||
|
struct ParticleData
|
||||||
|
{
|
||||||
|
EntityID ParticleID;
|
||||||
|
double TimeLived;
|
||||||
|
};
|
||||||
|
|
||||||
class ParticleSystem : public System
|
class ParticleSystem : public System
|
||||||
{
|
{
|
||||||
@@ -21,12 +27,13 @@ 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();
|
void SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle);
|
||||||
|
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
||||||
|
double m_TimeSinceLastSpawn;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif // ParticleSystem_h__
|
#endif // !ParticleSystem_h__
|
||||||
@@ -127,6 +127,7 @@
|
|||||||
<ClInclude Include="..\..\src\Components\FreeSteering.h" />
|
<ClInclude Include="..\..\src\Components\FreeSteering.h" />
|
||||||
<ClInclude Include="..\..\src\Components\Input.h" />
|
<ClInclude Include="..\..\src\Components\Input.h" />
|
||||||
<ClInclude Include="..\..\src\Components\Model.h" />
|
<ClInclude Include="..\..\src\Components\Model.h" />
|
||||||
|
<ClInclude Include="..\..\src\Components\Particle.h" />
|
||||||
<ClInclude Include="..\..\src\Components\ParticleEmitter.h" />
|
<ClInclude Include="..\..\src\Components\ParticleEmitter.h" />
|
||||||
<ClInclude Include="..\..\src\Components\Physics.h" />
|
<ClInclude Include="..\..\src\Components\Physics.h" />
|
||||||
<ClInclude Include="..\..\src\Components\PointLight.h" />
|
<ClInclude Include="..\..\src\Components\PointLight.h" />
|
||||||
|
|||||||
@@ -220,6 +220,9 @@
|
|||||||
<ClInclude Include="..\..\src\Systems\ParticleSystem.h">
|
<ClInclude Include="..\..\src\Systems\ParticleSystem.h">
|
||||||
<Filter>Particle System\Systems</Filter>
|
<Filter>Particle System\Systems</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\src\Components\Particle.h">
|
||||||
|
<Filter>Particle System\Components</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
<None Include="..\..\src\Shaders\AABB.frag.glsl">
|
||||||
|
|||||||
Reference in New Issue
Block a user