5 Commits

Author SHA1 Message Date
Jace 6d3aa5d394 Red rocket 2014-05-20 04:34:44 +02:00
ViktorLjung 7e5cf8efaa Fuckhack 2014-05-20 03:41:10 +02:00
ViktorLjung fe400e9af8 Merge remote-tracking branch 'origin/particles'
Conflicts:
	src/GameWorld.cpp
	src/Systems/ParticleSystem.cpp
2014-05-20 03:40:44 +02:00
Stiffly 65acae410d Added Explosion function to particles 2014-05-20 00:38:30 +02:00
Stiffly 1a52b1d04c finlir 2014-05-18 20:32:10 +02:00
28 changed files with 312 additions and 229 deletions
+1 -1
Submodule assets updated: 6cc38589ed...a71e214594
+4 -3
View File
@@ -19,7 +19,7 @@ struct ParticleEmitter : Component
, SpawnCount(0)
, SpreadAngle(0)
, LifeTime(0)
, TimeSinceLastSpawn(0) { }
, TimeSinceLastSpawn(100) { } // TEMP fulhack så att partiklarna spawnar direkt
EntityID ParticleTemplate;
float SpawnFrequency;
@@ -34,10 +34,11 @@ struct ParticleEmitter : Component
std::vector<float> AngularVelocitySpectrum;
std::vector<glm::vec3> OrientationSpectrum; //Keep?
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
private:
double TimeSinceLastSpawn;
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
};
}
-1
View File
@@ -38,7 +38,6 @@ public:
m_InputManager->Update(dt);
m_World->Update(dt);
m_Renderer->Draw(dt);
m_EventBroker->Clear();
glfwPollEvents();
}
+4 -43
View File
@@ -1,6 +1,5 @@
#include "PrecompiledHeader.h"
#include "EventBroker.h"
#include "Events/BindKey.h"
BaseEventRelay::~BaseEventRelay()
{
@@ -12,18 +11,12 @@ BaseEventRelay::~BaseEventRelay()
void EventBroker::Unsubscribe(BaseEventRelay &relay) // ?
{
auto contextIt = m_ContextRelays.find(relay.m_ContextTypeName);
if (contextIt == m_ContextRelays.end())
return;
auto eventRelays = contextIt->second;
auto itpair = eventRelays.equal_range(relay.m_EventTypeName);
auto itpair = m_Subscribers.equal_range(relay.m_TypeName);
for (auto it = itpair.first; it != itpair.second; ++it)
{
if (it->second == &relay)
{
eventRelays.erase(it);
m_Subscribers.erase(it);
break;
}
}
@@ -32,37 +25,5 @@ void EventBroker::Unsubscribe(BaseEventRelay &relay) // ?
void EventBroker::Subscribe(BaseEventRelay &relay)
{
relay.m_Broker = this;
m_ContextRelays[relay.m_ContextTypeName].insert(std::make_pair(relay.m_EventTypeName, &relay));
}
int EventBroker::Process(std::string contextTypeName)
{
auto it = m_ContextRelays.find(contextTypeName);
if (it == m_ContextRelays.end())
return 0;
EventRelays_t &relays = it->second;
int eventsProcessed = 0;
for (auto &pair : *m_EventQueueRead)
{
std::string &eventTypeName = pair.first;
std::shared_ptr<Event> event = pair.second;
auto itpair = relays.equal_range(eventTypeName);
for (auto it2 = itpair.first; it2 != itpair.second; ++it2)
{
auto relay = it2->second;
relay->Receive(event);
eventsProcessed++;
}
}
return eventsProcessed;
}
void EventBroker::Clear()
{
std::swap(m_EventQueueRead, m_EventQueueWrite);
m_EventQueueWrite->clear();
}
m_Subscribers.insert(std::make_pair(relay.m_TypeName, &relay));
}
+16 -69
View File
@@ -3,7 +3,6 @@
#include <typeinfo>
#include <functional>
#include <map>
#include <unordered_map>
#include <list>
@@ -24,22 +23,19 @@ class BaseEventRelay
friend class EventBroker;
protected:
BaseEventRelay(std::string contextTypeName, std::string eventTypeName)
: m_ContextTypeName(contextTypeName)
, m_EventTypeName(eventTypeName)
, m_Broker(nullptr) { }
BaseEventRelay(std::string typeName)
: m_TypeName(typeName), m_Broker(nullptr) { }
~BaseEventRelay();
public:
virtual bool Receive(const std::shared_ptr<Event> event) = 0;
virtual bool Receive(const Event &event) = 0;
protected:
std::string m_ContextTypeName;
std::string m_EventTypeName;
std::string m_TypeName;
EventBroker* m_Broker;
};
template <typename ContextType, typename EventType>
template <typename EventType>
class EventRelay : public BaseEventRelay
{
public:
@@ -47,24 +43,24 @@ public:
EventRelay()
: m_Callback(nullptr)
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { }
, BaseEventRelay(typeid(EventType).name()) { }
EventRelay(CallbackType callback)
: m_Callback(callback)
, BaseEventRelay(typeid(ContextType).name(), typeid(EventType).name()) { }
, BaseEventRelay(typeid(EventType).name()) { }
protected:
bool Receive(const std::shared_ptr<Event> event) override;
bool Receive(const Event &event) override;
private:
CallbackType m_Callback;
};
template <typename ContextType, typename EventType>
bool EventRelay<ContextType, EventType>::Receive(const std::shared_ptr<Event> event)
template <typename EventType>
bool EventRelay<EventType>::Receive(const Event &event)
{
if (m_Callback != nullptr)
{
return m_Callback(*static_cast<const EventType*>(event.get()));
return m_Callback(static_cast<const EventType&>(event));
}
else
{
@@ -74,75 +70,26 @@ bool EventRelay<ContextType, EventType>::Receive(const std::shared_ptr<Event> ev
class EventBroker
{
template <typename ContextType, typename EventType> friend class EventRelay;
template <typename EventType> friend class EventRelay;
public:
EventBroker()
{
m_EventQueueRead = std::make_shared<EventQueue_t>();
m_EventQueueWrite = std::make_shared<EventQueue_t>();
}
void Subscribe(BaseEventRelay &relay);
template <typename EventType>
void Publish(const EventType &event);
// Process all events no matter the context.
/*void Process()
{
}*/
/*
Process all events in a given context.
Returns: Number of events processed
*/
template <typename ContextType>
int Process();
int Process(std::string contextTypeName);
void Clear();
void Subscribe(BaseEventRelay &relay);
void Unsubscribe(BaseEventRelay &relay);
template <typename ContextType>
void UnsubscribeAll();
private:
typedef std::string ContextTypeName_t; // typeid(ContextType).name()
typedef std::string EventTypeName_t; // typeid(EventType).name()
typedef std::unordered_multimap<EventTypeName_t, BaseEventRelay*> EventRelays_t;
typedef std::unordered_map<ContextTypeName_t, EventRelays_t> ContextRelays_t;
ContextRelays_t m_ContextRelays;
typedef std::list<std::pair<EventTypeName_t, std::shared_ptr<Event>>> EventQueue_t;
std::shared_ptr<EventQueue_t> m_EventQueueRead;
std::shared_ptr<EventQueue_t> m_EventQueueWrite;
std::unordered_multimap<std::string, BaseEventRelay*> m_Subscribers;
};
template <typename EventType>
void EventBroker::Publish(const EventType &event)
{
/*auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
auto itpair = m_Subscribers.equal_range(typeid(EventType).name());
for (auto it = itpair.first; it != itpair.second; ++it)
{
it->second->Receive(event);
}*/
m_EventQueueWrite->push_back(std::make_pair(typeid(EventType).name(), std::shared_ptr<EventType>(new EventType(event))));
}
template <typename ContextType>
int EventBroker::Process()
{
const std::string contextTypeName = typeid(ContextType).name();
return Process(contextTypeName);
}
template <typename ContextType>
void EventBroker::UnsubscribeAll()
{
const std::string contextTypeName = typeid(ContextType).name();
auto contextIt = m_ContextRelays.find(contextTypeName);
if (contextIt != m_ContextRelays.end())
{
m_ContextRelays.erase(contextIt);
}
}
+18
View File
@@ -0,0 +1,18 @@
#ifndef Events_Collision_h__
#define Events_Collision_h__
#include "Entity.h"
#include "EventBroker.h"
namespace Events
{
struct Collision : Event
{
EntityID Entity1;
EntityID Entity2;
};
}
#endif // Events_Collision_h__
+7 -6
View File
@@ -335,7 +335,7 @@ void GameWorld::Initialize()
auto cameraTower = CreateEntity(tower);
{
auto transform = AddComponent<Components::Transform>(cameraTower);
transform->Position.z = 11.f;
transform->Position.z = 14.f;
transform->Position.y = 4.f;
//transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(cameraTower);
@@ -551,15 +551,16 @@ void GameWorld::Initialize()
auto entity = CreateEntity(tank);
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->Orientation = glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpawnFrequency = 10;
emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5;
emitterComponent->Speed = 5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity);
@@ -635,7 +636,7 @@ void GameWorld::Initialize()
auto entity = CreateEntity(tank);
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->Orientation = glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
@@ -644,6 +645,7 @@ void GameWorld::Initialize()
emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5;
emitterComponent->Speed = 5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity);
@@ -654,7 +656,6 @@ void GameWorld::Initialize()
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
emitterComponent->ParticleTemplate = particleEntity;
CommitEntity(particleEntity);
}
@@ -753,7 +754,7 @@ void GameWorld::Initialize()
auto cameraTower = CreateEntity(tower);
{
auto transform = AddComponent<Components::Transform>(cameraTower);
transform->Position.z = 11.f;
transform->Position.z = 14.f;
transform->Position.y = 4.f;
//transform->Orientation = glm::quat(glm::vec3(glm::pi<float>() / 8.f, 0.f, 0.f));
auto cameraComp = AddComponent<Components::Camera>(cameraTower);
+2 -3
View File
@@ -7,7 +7,6 @@
#include "Events/InputCommand.h"
#include "Events/MouseMove.h"
template <typename EventContext>
class InputController
{
public:
@@ -27,8 +26,8 @@ protected:
std::shared_ptr<::EventBroker> EventBroker;
private:
EventRelay<EventContext, Events::InputCommand> m_EInputCommand;
EventRelay<EventContext, Events::MouseMove> m_EMouseMove;
EventRelay<Events::InputCommand> m_EInputCommand;
EventRelay<Events::MouseMove> m_EMouseMove;
};
#endif // InputController_h__
-2
View File
@@ -13,8 +13,6 @@ void InputManager::Initialize()
void InputManager::Update(double dt)
{
EventBroker->Process<InputManager>();
m_LastKeyState = m_CurrentKeyState;
m_LastMouseState = m_CurrentMouseState;
m_LastMouseX = m_CurrentMouseX;
+2 -2
View File
@@ -39,9 +39,9 @@ private:
GLFWwindow* m_GLFWWindow;
std::shared_ptr<::EventBroker> EventBroker;
EventRelay<InputManager, Events::LockMouse> m_ELockMouse;
EventRelay<Events::LockMouse> m_ELockMouse;
bool OnLockMouse(const Events::LockMouse &event);
EventRelay<InputManager, Events::UnlockMouse> m_EUnlockMouse;
EventRelay<Events::UnlockMouse> m_EUnlockMouse;
bool OnUnlockMouse(const Events::UnlockMouse &event);
std::array<int, GLFW_KEY_LAST+1> m_CurrentKeyState;
+15 -14
View File
@@ -357,18 +357,7 @@ void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat
{
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
glm::vec3 camToParticle = glm::normalize(m_Camera->Position() - position);
glm::vec3 up = glm::vec3(0,1,0);
glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle));
glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec));
glm::mat4 billboardMatrix;
billboardMatrix[0] = glm::vec4(rightVec, 0);
billboardMatrix[1] = glm::vec4(up2, 0);
billboardMatrix[2] = glm::vec4(camToParticle, 0);
//billboardMatrix[3] = glm::vec4(position, 0);
TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, billboardMatrix));
TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, position));
}
void Renderer::AddPointLightToDraw(
@@ -770,10 +759,22 @@ void Renderer::DrawFBOScene(Viewport &viewport)
{
Texture* texture;
glm::mat4 modelMatrix;
glm::mat4 billboardMatrix;
std::tie(texture, modelMatrix, billboardMatrix) = tuple;
glm::vec3 position;
std::tie(texture, modelMatrix, position) = tuple;
//MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix );
glm::vec3 camToParticle = glm::normalize(viewport.Camera->Position() - position);
glm::vec3 up = glm::vec3(0,1,0);
glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle));
glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec));
glm::mat4 billboardMatrix;
billboardMatrix[0] = glm::vec4(rightVec, 0);
billboardMatrix[1] = glm::vec4(up2, 0);
billboardMatrix[2] = glm::vec4(camToParticle, 0);
//billboardMatrix[3] = glm::vec4(position, 0);
MVP = cameraMatrix * modelMatrix * billboardMatrix;
depthMVP = depthCameraMatrix * modelMatrix;
+1 -1
View File
@@ -26,7 +26,7 @@ public:
int Height() const { return m_Height; }
std::list<std::tuple<Model*, glm::mat4, bool, bool>> ModelsToRender;
std::list<std::tuple<Texture*, glm::mat4, glm::mat4>> TexturesToRender;
std::list<std::tuple<Texture*, glm::mat4, glm::vec3>> TexturesToRender;
std::list<std::tuple<glm::mat4, bool>> AABBsToRender;
Renderer();
+1 -1
View File
@@ -40,7 +40,7 @@ void main()
{
// Diffuse Texture
frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord) * Shadow(Input.ShadowCoord);
frag_Diffuse = texture(DiffuseTexture, Input.TextureCoord); // * Shadow(Input.ShadowCoord);
// G-buffer Position
frag_Position = vec4(Input.Position.xyz, 1.0);
+1
View File
@@ -33,6 +33,7 @@ public:
virtual void OnComponentRemoved(std::string type, Component* component) { }
// Called when components are committed to an entity
virtual void OnEntityCommit(EntityID entity) { }
virtual void OnEntityRemoved(EntityID entity) { }
protected:
World* m_World;
+1 -1
View File
@@ -19,7 +19,7 @@ public:
void Update(double dt) override;
EventRelay<DebugSystem, Events::KeyDown> m_EKeyDown;
EventRelay<Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
//void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
+1 -1
View File
@@ -27,7 +27,7 @@ private:
std::unique_ptr<FreeSteeringInputController> m_InputController;
};
class FreeSteeringSystem::FreeSteeringInputController : InputController<FreeSteeringSystem>
class FreeSteeringSystem::FreeSteeringInputController : InputController
{
public:
FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
+1 -1
View File
@@ -27,7 +27,7 @@ private:
std::map<EntityID, double> m_TimeSinceLastShot;
};
class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController<HelicopterSteeringSystem>
class HelicopterSteeringSystem::HelicopterSteeringInputController : InputController
{
public:
HelicopterSteeringInputController(std::shared_ptr<::EventBroker> eventBroker)
+11 -11
View File
@@ -45,28 +45,28 @@ private:
std::unordered_map<Gamepad::Button, std::tuple<std::string, float>> m_GamepadButtonBindings; // Gamepad::Button -> command string
// Input events
EventRelay<InputSystem, Events::KeyDown> m_EKeyDown;
EventRelay<Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const Events::KeyDown &event);
EventRelay<InputSystem, Events::KeyUp> m_EKeyUp;
EventRelay<Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp &event);
EventRelay<InputSystem, Events::MousePress> m_EMousePress;
EventRelay<Events::MousePress> m_EMousePress;
bool OnMousePress(const Events::MousePress &event);
EventRelay<InputSystem, Events::MouseRelease> m_EMouseRelease;
EventRelay<Events::MouseRelease> m_EMouseRelease;
bool OnMouseRelease(const Events::MouseRelease &event);
EventRelay<InputSystem, Events::GamepadAxis> m_EGamepadAxis;
EventRelay<Events::GamepadAxis> m_EGamepadAxis;
bool OnGamepadAxis(const Events::GamepadAxis &event);
EventRelay<InputSystem, Events::GamepadButtonDown> m_EGamepadButtonDown;
EventRelay<Events::GamepadButtonDown> m_EGamepadButtonDown;
bool OnGamepadButtonDown(const Events::GamepadButtonDown &event);
EventRelay<InputSystem, Events::GamepadButtonUp> m_EGamepadButtonUp;
EventRelay<Events::GamepadButtonUp> m_EGamepadButtonUp;
bool OnGamepadButtonUp(const Events::GamepadButtonUp &event);
// Input binding events
EventRelay<InputSystem, Events::BindKey> m_EBindKey;
EventRelay<Events::BindKey> m_EBindKey;
bool OnBindKey(const Events::BindKey &event);
EventRelay<InputSystem, Events::BindMouseButton> m_EBindMouseButton;
EventRelay<Events::BindMouseButton> m_EBindMouseButton;
bool OnBindMouseButton(const Events::BindMouseButton &event);
EventRelay<InputSystem, Events::BindGamepadAxis> m_EBindGamepadAxis;
EventRelay<Events::BindGamepadAxis> m_EBindGamepadAxis;
bool OnBindGamepadAxis(const Events::BindGamepadAxis &event);
EventRelay<InputSystem, Events::BindGamepadButton> m_EBindGamepadButton;
EventRelay<Events::BindGamepadButton> m_EBindGamepadButton;
bool OnBindGamepadButton(const Events::BindGamepadButton &event);
float GetCommandTotalValue(std::string command);
+106 -37
View File
@@ -7,11 +7,32 @@
void Systems::ParticleSystem::Initialize()
{
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
tempSpawnedExplosions = false;
EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::ParticleSystem::OnKeyUp);
}
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)
@@ -101,43 +122,41 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf)
void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
{
auto emitterComponent = m_World->GetComponent<Components::ParticleEmitter>(emitterID);
auto emitterTransform = m_World->GetComponent<Components::Transform>(emitterID);
glm::vec3 emitterPos = m_TransformSystem->AbsolutePosition(emitterID);
glm::quat emitterOrientation = emitterTransform->Orientation;
auto eComponent = m_World->GetComponent<Components::ParticleEmitter>(emitterID);
auto eTransform = m_World->GetComponent<Components::Transform>(emitterID);
glm::vec3 ePosition = m_TransformSystem->AbsolutePosition(emitterID);
glm::quat eOrientation = eTransform->Orientation;
glm::vec3 paticleSpeed = glm::vec3(eComponent->Speed);
float tempSpeed = 4;
glm::vec3 speed = glm::vec3(tempSpeed);
for(int i = 0; i < emitterComponent->SpawnCount; i++)
for(int i = 0; i < eComponent->SpawnCount; i++)
{
auto ent = m_World->CloneEntity(emitterComponent->ParticleTemplate);
auto particleEntity = m_World->CloneEntity(eComponent->ParticleTemplate);
auto particleTransform = m_World->GetComponent<Components::Transform>(ent);
particleTransform->Position = emitterPos;
particleTransform->Orientation = emitterOrientation;
auto particleTransform = m_World->GetComponent<Components::Transform>(particleEntity);
particleTransform->Position = ePosition;
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.
float spreadAngle = emitterComponent->SpreadAngle;
particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed *
float spreadAngle = eComponent->SpreadAngle;
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(0, 1, 0))) *
glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1)));
auto particle = m_World->AddComponent<Components::Particle>(ent);
particle->LifeTime = emitterComponent->LifeTime;
particle->ScaleSpectrum = emitterComponent->ScaleSpectrum;
particle->VelocitySpectrum.push_back(particleTransform->Velocity);
auto particleComponent = m_World->AddComponent<Components::Particle>(particleEntity);
particleComponent->LifeTime = eComponent->LifeTime - 0.5;
particleComponent->ScaleSpectrum = eComponent->ScaleSpectrum;
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
{
particleTransform->Scale = emitterComponent->ScaleSpectrum[0];
particleTransform->Scale = eComponent->ScaleSpectrum[0];
}
}
else
@@ -145,23 +164,22 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
particleTransform->Scale = glm::vec3(1, 1, 1);
}
if(emitterComponent->UseGoalVelocity)
particle->VelocitySpectrum.push_back(emitterComponent->GoalVelocity);
particle->OrientationSpectrum = emitterComponent->OrientationSpectrum;
if(particle->OrientationSpectrum.size() != 0)
particleTransform->Orientation = glm::angleAxis(0.f, particle->OrientationSpectrum[0]);
particle->AngularVelocitySpectrum = emitterComponent->AngularVelocitySpectrum;
if(eComponent->UseGoalVelocity)
particleComponent->VelocitySpectrum.push_back(eComponent->GoalVelocity);
particleComponent->OrientationSpectrum = eComponent->OrientationSpectrum;
if(particleComponent->OrientationSpectrum.size() != 0)
particleTransform->Orientation = glm::angleAxis(0.f, particleComponent->OrientationSpectrum[0]);
particleComponent->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum;
ParticleData data;
data.ParticleID = ent;
data.ParticleID = particleEntity;
data.SpawnTime = glfwGetTime();
if (particle->AngularVelocitySpectrum.size() != 0)
data.AngularVelocity = particle->AngularVelocitySpectrum[0];
if (particle->OrientationSpectrum.size() != 0)
data.Orientation = particle->OrientationSpectrum[0];
else data.Orientation = emitterOrientation * glm::vec3(0,0,-1);
if (particleComponent->AngularVelocitySpectrum.size() != 0)
data.AngularVelocity = particleComponent->AngularVelocitySpectrum[0];
if (particleComponent->OrientationSpectrum.size() != 0)
data.Orientation = particleComponent->OrientationSpectrum[0];
else data.Orientation = eOrientation * glm::vec3(0,0,-1);
m_ParticleEmitter[emitterID].push_back(data);
}
}
@@ -205,4 +223,55 @@ void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vect
if(spectrum[0] > spectrum[1])
dAlpha *= -1;
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;
}
+13 -2
View File
@@ -6,8 +6,9 @@
#include "Components/Transform.h"
#include "Components/ParticleEmitter.h"
#include "Components/Particle.h"
#include "Components/Model.h"
#include "Components/PointLight.h"
#include "Components/Sprite.h"
#include "EventBroker.h"
#include "Events/KeyUp.h"
#include "Color.h"
#include <GLFW/glfw3.h>
@@ -33,6 +34,11 @@ public:
void Update(double dt) override;
void UpdateEntity(double dt, EntityID entity, EntityID parent) 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:
void SpawnParticles(EntityID emitterID);
float RandomizeAngle(float spreadAngle);
@@ -43,9 +49,14 @@ private:
void Billboard();
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
std::map<EntityID, double> m_TimeSinceLastSpawn;
std::map<EntityID, double> m_ExplosionEmitters;
std::shared_ptr<Systems::TransformSystem> m_TransformSystem;
bool tempSpawnedExplosions;
EventRelay<Events::KeyUp> m_EKeyUp;
bool OnKeyUp(const Events::KeyUp &e);
};
}
+19 -5
View File
@@ -78,7 +78,7 @@ void Systems::PhysicsSystem::Initialize()
worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_DO_NOTHING;
// You must specify the size of the broad phase - objects should not be simulated outside this region
worldInfo.setBroadPhaseWorldSize(1000.0f);
worldInfo.setBroadPhaseWorldSize(1500.0f);
m_PhysicsWorld = new hkpWorld(worldInfo);
// When the simulation type is SIMULATION_TYPE_MULTITHREADED, in the debug build, the sdk performs checks
@@ -107,7 +107,7 @@ void Systems::PhysicsSystem::Initialize()
m_PhysicsWorld->unmarkForWrite();
m_collisionResolution = new MyCollisionResolution;
m_collisionResolution = new MyCollisionResolution(this);
}
}
@@ -326,7 +326,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
rigidBody->addContactListener( m_collisionResolution );
m_Vehicles[entity]->addToWorld(m_PhysicsWorld);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_RigidBodyEntities[rigidBody] = entity;
// The vehicle is an action
@@ -344,7 +344,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
rigidBody->addContactListener( m_collisionResolution );
m_PhysicsWorld->addEntity(rigidBody);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_RigidBodyEntities[rigidBody] = entity;
m_PhysicsWorld->unmarkForWrite();
shape->removeReference();
@@ -397,7 +397,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
m_PhysicsWorld->markForWrite();
m_PhysicsWorld->addEntity(rigidBody);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_RigidBodyEntities[rigidBody] = entity;
m_PhysicsWorld->unmarkForWrite();
shape->removeReference();
@@ -577,3 +577,17 @@ bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpuls
m_PhysicsWorld->unmarkForWrite();
return true;
}
void Systems::PhysicsSystem::OnEntityRemoved( EntityID entity )
{
// TODO:
/*auto rigidBodyIt = m_RigidBodies.find(entity);
if (rigidBodyIt == m_RigidBodies.end())
return;
auto rigidBody = rigidBodyIt->second;
m_RigidBodies.erase(entity);
m_RigidBodyEntities.erase(rigidBody);
rigidBody->removeReference();*/
}
+42 -21
View File
@@ -27,6 +27,7 @@
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "Events/ApplyPointImpulse.h"
#include "Events/Collision.h"
#include "OBJ.h"
// Math and base include
@@ -75,27 +76,45 @@
#include <Physics2012/Dynamics/Collide/ContactListener/hkpContactListener.h>
class MyCollisionResolution: public hkReferencedObject, public hkpContactListener
{
public:
std::unordered_map<hkpRigidBody*, EntityID> m_RigidBodies;
virtual void contactPointCallback( const hkpContactPointEvent& event )
{
EntityID entity1 = m_RigidBodies[event.getBody(0)];
EntityID entity2 = m_RigidBodies[event.getBody(1)];
//LOG_INFO("Entities colliding: %i, %i ", entity1, entity2);
}
};
#include "Components/Model.h"
namespace Systems
{
class PhysicsSystem : public System
{
public:
class MyCollisionResolution: public hkReferencedObject, public hkpContactListener
{
public:
MyCollisionResolution(Systems::PhysicsSystem* physicsSystem)
: m_PhysicsSystem(physicsSystem) { }
virtual void contactPointCallback( const hkpContactPointEvent& event )
{
EntityID entity1 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(0)];
EntityID entity2 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(1)];
Events::Collision e;
e.Entity1 = entity1;
e.Entity2 = entity2;
m_PhysicsSystem->EventBroker->Publish(e);
auto modelComponent = m_PhysicsSystem->m_World->GetComponent<Components::Model>(entity1);
if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj" && entity2 == 6)
{
m_PhysicsSystem->m_PhysicsWorld->markForWrite();
m_PhysicsSystem->m_RigidBodies[entity1]->setPosition(hkVector4(2000, -2000, 2000));
m_PhysicsSystem->m_PhysicsWorld->unmarkForWrite();
}
}
private:
Systems::PhysicsSystem* m_PhysicsSystem;
};
friend class MyCollisionResolution;
PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
@@ -107,19 +126,20 @@ public:
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void OnEntityCommit(EntityID entity) override;
void OnEntityRemoved(EntityID entity) override;
private:
double m_Accumulator;
hkpWorld* m_PhysicsWorld;
// Events
EventRelay<PhysicsSystem, Events::TankSteer> m_ETankSteer;
EventRelay<Events::TankSteer> m_ETankSteer;
bool OnTankSteer(const Events::TankSteer &event);
EventRelay<PhysicsSystem, Events::SetVelocity> m_ESetVelocity;
EventRelay<Events::SetVelocity> m_ESetVelocity;
bool OnSetVelocity(const Events::SetVelocity &event);
EventRelay<PhysicsSystem, Events::ApplyForce> m_EApplyForce;
EventRelay<Events::ApplyForce> m_EApplyForce;
bool OnApplyForce(const Events::ApplyForce &event);
EventRelay<PhysicsSystem, Events::ApplyPointImpulse> m_EApplyPointImpulse;
EventRelay<Events::ApplyPointImpulse> m_EApplyPointImpulse;
bool OnApplyPointImpulse(const Events::ApplyPointImpulse &event);
void SetUpPhysicsState(EntityID entity, EntityID parent);
@@ -132,6 +152,7 @@ private:
void SetupPhysics(hkpWorld* physicsWorld);
std::unordered_map<EntityID, hkpRigidBody*> m_RigidBodies;
std::unordered_map<hkpRigidBody*, EntityID> m_RigidBodyEntities;
hkJobThreadPool* m_ThreadPool;
hkJobQueue* m_JobQueue;
+1 -1
View File
@@ -44,7 +44,7 @@ private:
//unsigned long dataSize;
// Events
EventRelay<SoundSystem, Events::PlaySound> m_EPlaySound;
EventRelay<Events::PlaySound> m_EPlaySound;
bool OnPlaySound(const Events::PlaySound &event);
std::map<Component*, ALuint> m_Sources;
+28
View File
@@ -11,6 +11,8 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
void Systems::TankSteeringSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_ECollision, &Systems::TankSteeringSystem::OnCollision);
for (int i = 0; i < 4; i++)
{
m_TankInputControllers[i] = std::shared_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker, i + 1));
@@ -93,6 +95,32 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit
}
}
bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
{
// TODO:
auto modelComponent = m_World->GetComponent<Components::Model>(e.Entity1);
if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj")
{
auto transform = m_World->GetComponent<Components::Transform>(e.Entity1);
m_World->GetSystem<Systems::ParticleSystem>()->CreateExplosion(
transform->Position,
1,
60,
"Textures/Sprites/NewtonTreeDeleteASAPPlease.png",
glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0)),
40,
glm::pi<float>(),
0.5f
);
//m_World->RemoveEntity(e.Entity1);
}
//LOG_INFO("Ent1 %i Ent2 %i", e.Entity1, e.Entity2);
return true;
}
void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt )
{
PositionX = m_Horizontal;
+7 -1
View File
@@ -5,6 +5,7 @@
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "Events/ApplyPointImpulse.h"
#include "Events/Collision.h"
#include "Components/Transform.h"
#include "Components/TankSteering.h"
#include "Components/TowerSteering.h"
@@ -12,7 +13,9 @@
#include "Components/Physics.h"
#include "Components/Vehicle.h"
#include "Components/Player.h"
#include "Components/Model.h"
#include "Systems/TransformSystem.h"
#include "Systems/ParticleSystem.h"
#include "InputController.h"
namespace Systems
@@ -31,13 +34,16 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
EventRelay<Events::Collision> m_ECollision;
bool OnCollision(const Events::Collision &e);
class TankSteeringInputController;
std::array<std::shared_ptr<TankSteeringInputController>, 4> m_TankInputControllers;
std::map<EntityID, double> m_TimeSinceLastShot;
};
class TankSteeringSystem::TankSteeringInputController : InputController<TankSteeringSystem>
class TankSteeringSystem::TankSteeringInputController : InputController
{
public:
TankSteeringInputController(std::shared_ptr<::EventBroker> eventBroker, int playerID)
+6 -2
View File
@@ -36,9 +36,7 @@ void World::Update(double dt)
{
for (auto pair : m_Systems)
{
const std::string &type = pair.first;
auto system = pair.second;
m_EventBroker->Process(type);
system->Update(dt);
RecursiveUpdate(system, dt, 0);
}
@@ -85,6 +83,12 @@ void World::RemoveEntity(EntityID entity)
m_EntitiesToRemove.push_back(pair.first);
}
}
// Trigger events
for (auto pair : m_Systems)
{
auto system = pair.second;
system->OnEntityRemoved(entity);
}
}
void World::ProcessEntityRemovals()
+1
View File
@@ -163,6 +163,7 @@
<ClInclude Include="..\..\src\Events\BindGamepadButton.h" />
<ClInclude Include="..\..\src\Events\BindKey.h" />
<ClInclude Include="..\..\src\Events\BindMouseButton.h" />
<ClInclude Include="..\..\src\Events\Collision.h" />
<ClInclude Include="..\..\src\Events\GamepadAxis.h" />
<ClInclude Include="..\..\src\Events\GamepadButton.h" />
<ClInclude Include="..\..\src\Events\InputCommand.h" />
@@ -397,6 +397,9 @@
<ClInclude Include="..\..\src\Components\Player.h">
<Filter>Gameplay\Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Events\Collision.h">
<Filter>Physics\Events</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\..\src\Shaders\Fragment2.glsl">