Merge remote-tracking branch 'origin/particles'
Conflicts: src/GameWorld.cpp src/Systems/ParticleSystem.cpp
This commit is contained in:
@@ -19,7 +19,7 @@ struct ParticleEmitter : Component
|
|||||||
, SpawnCount(0)
|
, SpawnCount(0)
|
||||||
, SpreadAngle(0)
|
, SpreadAngle(0)
|
||||||
, LifeTime(0)
|
, LifeTime(0)
|
||||||
, TimeSinceLastSpawn(0) { }
|
, TimeSinceLastSpawn(100) { } // TEMP fulhack så att partiklarna spawnar direkt
|
||||||
|
|
||||||
EntityID ParticleTemplate;
|
EntityID ParticleTemplate;
|
||||||
float SpawnFrequency;
|
float SpawnFrequency;
|
||||||
@@ -34,10 +34,11 @@ struct ParticleEmitter : Component
|
|||||||
std::vector<float> AngularVelocitySpectrum;
|
std::vector<float> AngularVelocitySpectrum;
|
||||||
std::vector<glm::vec3> OrientationSpectrum; //Keep?
|
std::vector<glm::vec3> OrientationSpectrum; //Keep?
|
||||||
|
|
||||||
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
double TimeSinceLastSpawn;
|
double TimeSinceLastSpawn;
|
||||||
|
|
||||||
|
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-4
@@ -551,15 +551,16 @@ void GameWorld::Initialize()
|
|||||||
|
|
||||||
auto entity = CreateEntity(tank);
|
auto entity = CreateEntity(tank);
|
||||||
auto transformComponent = AddComponent<Components::Transform>(entity);
|
auto transformComponent = AddComponent<Components::Transform>(entity);
|
||||||
transformComponent->Position = glm::vec3(2,-1.7,2.0);
|
transformComponent->Position = glm::vec3(-2,-1.7,2.0);
|
||||||
transformComponent->Scale = glm::vec3(3,3,3);
|
transformComponent->Scale = glm::vec3(3,3,3);
|
||||||
transformComponent->Orientation = glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0));
|
transformComponent->Orientation = glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0));
|
||||||
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
|
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
|
||||||
emitterComponent->SpawnCount = 2;
|
emitterComponent->SpawnCount = 2;
|
||||||
emitterComponent->SpawnFrequency = 0.005;
|
emitterComponent->SpawnFrequency = 10;
|
||||||
emitterComponent->SpreadAngle = glm::pi<float>();
|
emitterComponent->SpreadAngle = glm::pi<float>();
|
||||||
emitterComponent->UseGoalVelocity = false;
|
emitterComponent->UseGoalVelocity = false;
|
||||||
emitterComponent->LifeTime = 0.5;
|
emitterComponent->LifeTime = 0.5;
|
||||||
|
emitterComponent->Speed = 5;
|
||||||
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
|
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
|
||||||
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
|
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
|
||||||
CommitEntity(entity);
|
CommitEntity(entity);
|
||||||
@@ -635,7 +636,7 @@ void GameWorld::Initialize()
|
|||||||
|
|
||||||
auto entity = CreateEntity(tank);
|
auto entity = CreateEntity(tank);
|
||||||
auto transformComponent = AddComponent<Components::Transform>(entity);
|
auto transformComponent = AddComponent<Components::Transform>(entity);
|
||||||
transformComponent->Position = glm::vec3(-2,-1.7,2.0);
|
transformComponent->Position = glm::vec3(2,-1.7,2.0);
|
||||||
transformComponent->Scale = glm::vec3(3,3,3);
|
transformComponent->Scale = glm::vec3(3,3,3);
|
||||||
transformComponent->Orientation = glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0));
|
transformComponent->Orientation = glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0));
|
||||||
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
|
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
|
||||||
@@ -644,6 +645,7 @@ void GameWorld::Initialize()
|
|||||||
emitterComponent->SpreadAngle = glm::pi<float>();
|
emitterComponent->SpreadAngle = glm::pi<float>();
|
||||||
emitterComponent->UseGoalVelocity = false;
|
emitterComponent->UseGoalVelocity = false;
|
||||||
emitterComponent->LifeTime = 0.5;
|
emitterComponent->LifeTime = 0.5;
|
||||||
|
emitterComponent->Speed = 5;
|
||||||
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
|
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
|
||||||
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
|
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
|
||||||
CommitEntity(entity);
|
CommitEntity(entity);
|
||||||
@@ -654,7 +656,6 @@ void GameWorld::Initialize()
|
|||||||
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
|
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
|
||||||
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
|
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
|
||||||
emitterComponent->ParticleTemplate = particleEntity;
|
emitterComponent->ParticleTemplate = particleEntity;
|
||||||
|
|
||||||
CommitEntity(particleEntity);
|
CommitEntity(particleEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+104
-35
@@ -7,11 +7,32 @@
|
|||||||
void Systems::ParticleSystem::Initialize()
|
void Systems::ParticleSystem::Initialize()
|
||||||
{
|
{
|
||||||
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
|
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
|
||||||
|
tempSpawnedExplosions = false;
|
||||||
|
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::ParticleSystem::OnKeyUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::ParticleSystem::Update(double dt)
|
void Systems::ParticleSystem::Update(double dt)
|
||||||
{
|
{
|
||||||
|
std::map<EntityID, double>::iterator it;
|
||||||
|
for(it = m_ExplosionEmitters.begin(); it != m_ExplosionEmitters.end();)
|
||||||
|
{
|
||||||
|
EntityID explosionID = it->first;
|
||||||
|
double spawnTime = it->second;
|
||||||
|
|
||||||
|
double timeLived = glfwGetTime() - spawnTime;
|
||||||
|
auto eComp = m_World->GetComponent<Components::ParticleEmitter>(explosionID);
|
||||||
|
|
||||||
|
if(timeLived > eComp->LifeTime)
|
||||||
|
{
|
||||||
|
m_World->RemoveEntity(explosionID);
|
||||||
|
it = m_ExplosionEmitters.erase(it);
|
||||||
|
//LOG_INFO("Deleted explosion emitter successfully");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||||
@@ -101,43 +122,41 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf)
|
|||||||
|
|
||||||
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
|
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
|
||||||
{
|
{
|
||||||
auto emitterComponent = m_World->GetComponent<Components::ParticleEmitter>(emitterID);
|
auto eComponent = m_World->GetComponent<Components::ParticleEmitter>(emitterID);
|
||||||
auto emitterTransform = m_World->GetComponent<Components::Transform>(emitterID);
|
auto eTransform = m_World->GetComponent<Components::Transform>(emitterID);
|
||||||
glm::vec3 emitterPos = m_TransformSystem->AbsolutePosition(emitterID);
|
glm::vec3 ePosition = m_TransformSystem->AbsolutePosition(emitterID);
|
||||||
glm::quat emitterOrientation = emitterTransform->Orientation;
|
glm::quat eOrientation = eTransform->Orientation;
|
||||||
|
glm::vec3 paticleSpeed = glm::vec3(eComponent->Speed);
|
||||||
|
|
||||||
float tempSpeed = 4;
|
for(int i = 0; i < eComponent->SpawnCount; i++)
|
||||||
glm::vec3 speed = glm::vec3(tempSpeed);
|
|
||||||
|
|
||||||
for(int i = 0; i < emitterComponent->SpawnCount; i++)
|
|
||||||
{
|
{
|
||||||
auto ent = m_World->CloneEntity(emitterComponent->ParticleTemplate);
|
auto particleEntity = m_World->CloneEntity(eComponent->ParticleTemplate);
|
||||||
|
|
||||||
auto particleTransform = m_World->GetComponent<Components::Transform>(ent);
|
auto particleTransform = m_World->GetComponent<Components::Transform>(particleEntity);
|
||||||
particleTransform->Position = emitterPos;
|
particleTransform->Position = ePosition;
|
||||||
|
|
||||||
particleTransform->Orientation = emitterOrientation;
|
particleTransform->Orientation = eOrientation;
|
||||||
//The emitter's orientation as "start value" times the default direction for emitter. Times the speed, and then rotate on x and y axis with the randomized spread angle.
|
//The emitter's orientation as "start value" times the default direction for emitter. Times the speed, and then rotate on x and y axis with the randomized spread angle.
|
||||||
float spreadAngle = emitterComponent->SpreadAngle;
|
float spreadAngle = eComponent->SpreadAngle;
|
||||||
particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed *
|
particleTransform->Velocity = eOrientation * glm::vec3(0, 0, -1) * paticleSpeed *
|
||||||
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) *
|
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) *
|
||||||
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) *
|
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) *
|
||||||
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1)));
|
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1)));
|
||||||
|
|
||||||
auto particle = m_World->AddComponent<Components::Particle>(ent);
|
auto particleComponent = m_World->AddComponent<Components::Particle>(particleEntity);
|
||||||
particle->LifeTime = emitterComponent->LifeTime;
|
particleComponent->LifeTime = eComponent->LifeTime - 0.5;
|
||||||
particle->ScaleSpectrum = emitterComponent->ScaleSpectrum;
|
particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum;
|
||||||
particle->VelocitySpectrum.push_back(particleTransform->Velocity);
|
particleComponent->VelocitySpectrum.push_back(particleTransform->Velocity);
|
||||||
|
|
||||||
if (emitterComponent->ScaleSpectrum.size() > 0)
|
if (eComponent->ScaleSpectrum.size() > 0)
|
||||||
{
|
{
|
||||||
if (emitterComponent->ScaleSpectrum.size() > 1)
|
if (eComponent->ScaleSpectrum.size() > 1)
|
||||||
{
|
{
|
||||||
particle->ScaleSpectrum = emitterComponent->ScaleSpectrum;
|
particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
particleTransform->Scale = emitterComponent->ScaleSpectrum[0];
|
particleTransform->Scale = eComponent->ScaleSpectrum[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -145,23 +164,22 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
|
|||||||
particleTransform->Scale = glm::vec3(1, 1, 1);
|
particleTransform->Scale = glm::vec3(1, 1, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(emitterComponent->UseGoalVelocity)
|
if(eComponent->UseGoalVelocity)
|
||||||
particle->VelocitySpectrum.push_back(emitterComponent->GoalVelocity);
|
particleComponent->VelocitySpectrum.push_back(eComponent->GoalVelocity);
|
||||||
particle->OrientationSpectrum = emitterComponent->OrientationSpectrum;
|
particleComponent->OrientationSpectrum = eComponent->OrientationSpectrum;
|
||||||
if(particle->OrientationSpectrum.size() != 0)
|
if(particleComponent->OrientationSpectrum.size() != 0)
|
||||||
particleTransform->Orientation = glm::angleAxis(0.f, particle->OrientationSpectrum[0]);
|
particleTransform->Orientation = glm::angleAxis(0.f, particleComponent->OrientationSpectrum[0]);
|
||||||
particle->AngularVelocitySpectrum = emitterComponent->AngularVelocitySpectrum;
|
particleComponent->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum;
|
||||||
|
|
||||||
|
|
||||||
ParticleData data;
|
ParticleData data;
|
||||||
data.ParticleID = ent;
|
data.ParticleID = particleEntity;
|
||||||
data.SpawnTime = glfwGetTime();
|
data.SpawnTime = glfwGetTime();
|
||||||
if (particle->AngularVelocitySpectrum.size() != 0)
|
if (particleComponent->AngularVelocitySpectrum.size() != 0)
|
||||||
data.AngularVelocity = particle->AngularVelocitySpectrum[0];
|
data.AngularVelocity = particleComponent->AngularVelocitySpectrum[0];
|
||||||
if (particle->OrientationSpectrum.size() != 0)
|
if (particleComponent->OrientationSpectrum.size() != 0)
|
||||||
data.Orientation = particle->OrientationSpectrum[0];
|
data.Orientation = particleComponent->OrientationSpectrum[0];
|
||||||
else data.Orientation = emitterOrientation * glm::vec3(0,0,-1);
|
else data.Orientation = eOrientation * glm::vec3(0,0,-1);
|
||||||
|
|
||||||
m_ParticleEmitter[emitterID].push_back(data);
|
m_ParticleEmitter[emitterID].push_back(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,3 +224,54 @@ void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vect
|
|||||||
dAlpha *= -1;
|
dAlpha *= -1;
|
||||||
alpha = spectrum[0] + dAlpha * timeProgress;
|
alpha = spectrum[0] + dAlpha * timeProgress;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Systems::ParticleSystem::CreateExplosion(glm::vec3 _pos, double _lifeTime, int _particlesToSpawn, std::string _spritePath, glm::quat _relativeUpOri, float _speed, float _spreadAngle, float _particleScale)
|
||||||
|
{
|
||||||
|
auto explosion = m_World->CreateEntity();
|
||||||
|
auto emitter = m_World->AddComponent<Components::ParticleEmitter>(explosion);
|
||||||
|
emitter->LifeTime = _lifeTime;
|
||||||
|
emitter->SpawnCount = _particlesToSpawn;
|
||||||
|
emitter->Speed = _speed;
|
||||||
|
emitter->SpreadAngle = _spreadAngle;
|
||||||
|
emitter->SpawnFrequency = _lifeTime + 20; //temp
|
||||||
|
// emitter->UseGoalVelocity = true;
|
||||||
|
// emitter->GoalVelocity = glm::vec3(0,-_speed, 0);
|
||||||
|
m_World->CommitEntity(explosion);
|
||||||
|
|
||||||
|
auto particleEnt = m_World->CreateEntity();
|
||||||
|
auto TEMP = m_World->AddComponent<Components::Transform>(particleEnt);
|
||||||
|
TEMP->Scale = glm::vec3(0);
|
||||||
|
auto spriteComponent = m_World->AddComponent<Components::Sprite>(particleEnt);
|
||||||
|
spriteComponent->SpriteFile = _spritePath;
|
||||||
|
m_World->CommitEntity(particleEnt);
|
||||||
|
emitter->ParticleTemplate = particleEnt;
|
||||||
|
|
||||||
|
auto transform = m_World->AddComponent<Components::Transform>(explosion);
|
||||||
|
transform->Position = _pos;
|
||||||
|
transform->Orientation = _relativeUpOri;
|
||||||
|
|
||||||
|
SpawnParticles(explosion);
|
||||||
|
m_ExplosionEmitters[explosion] = glfwGetTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Systems::ParticleSystem::OnKeyUp(const Events::KeyUp &e)
|
||||||
|
{
|
||||||
|
if(!tempSpawnedExplosions)
|
||||||
|
{
|
||||||
|
if (e.KeyCode == GLFW_KEY_B)
|
||||||
|
{
|
||||||
|
tempSpawnedExplosions = true;
|
||||||
|
CreateExplosion(
|
||||||
|
glm::vec3(0, 10, 0),
|
||||||
|
0.5,
|
||||||
|
60,
|
||||||
|
"Textures/Sprites/NewtonTreeDeleteASAPPlease.png",
|
||||||
|
glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0)),
|
||||||
|
40,
|
||||||
|
glm::pi<float>(),
|
||||||
|
0.5f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
@@ -6,8 +6,9 @@
|
|||||||
#include "Components/Transform.h"
|
#include "Components/Transform.h"
|
||||||
#include "Components/ParticleEmitter.h"
|
#include "Components/ParticleEmitter.h"
|
||||||
#include "Components/Particle.h"
|
#include "Components/Particle.h"
|
||||||
#include "Components/Model.h"
|
#include "Components/Sprite.h"
|
||||||
#include "Components/PointLight.h"
|
#include "EventBroker.h"
|
||||||
|
#include "Events/KeyUp.h"
|
||||||
#include "Color.h"
|
#include "Color.h"
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
@@ -33,6 +34,11 @@ public:
|
|||||||
void Update(double dt) override;
|
void Update(double dt) override;
|
||||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||||
void Initialize() override;
|
void Initialize() override;
|
||||||
|
|
||||||
|
void CreateExplosion(glm::vec3 _pos, double _lifeTime, int _particlesToSpawn, std::string _spritePath, glm::quat _relativeUpOri, float _speed, float _spreadAngle, float _particleScale);
|
||||||
|
|
||||||
|
virtual bool OnCommand(const Events::KeyUp &event) { return false; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void SpawnParticles(EntityID emitterID);
|
void SpawnParticles(EntityID emitterID);
|
||||||
float RandomizeAngle(float spreadAngle);
|
float RandomizeAngle(float spreadAngle);
|
||||||
@@ -43,8 +49,13 @@ private:
|
|||||||
void Billboard();
|
void Billboard();
|
||||||
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
|
||||||
std::map<EntityID, double> m_TimeSinceLastSpawn;
|
std::map<EntityID, double> m_TimeSinceLastSpawn;
|
||||||
|
std::map<EntityID, double> m_ExplosionEmitters;
|
||||||
std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
|
std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
|
||||||
|
|
||||||
|
bool tempSpawnedExplosions;
|
||||||
|
|
||||||
|
EventRelay<Events::KeyUp> m_EKeyUp;
|
||||||
|
bool OnKeyUp(const Events::KeyUp &e);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user