Created base for systems
This commit is contained in:
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef System_h__
|
||||||
|
#define System_h__
|
||||||
|
|
||||||
|
#include "EventBroker.h"
|
||||||
|
#include "World.h"
|
||||||
|
#include "ComponentWrapper.h"
|
||||||
|
|
||||||
|
class System
|
||||||
|
{
|
||||||
|
friend class SystemPipeline;
|
||||||
|
|
||||||
|
public:
|
||||||
|
System(const EventBroker* eventBroker, std::string componentType)
|
||||||
|
: m_EventBroker(eventBroker)
|
||||||
|
, m_ComponentType(componentType)
|
||||||
|
{ }
|
||||||
|
|
||||||
|
virtual void Update(World* world, ComponentWrapper& component, double dt) = 0;
|
||||||
|
|
||||||
|
private:
|
||||||
|
const EventBroker* m_EventBroker;
|
||||||
|
std::string m_ComponentType;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
#ifndef SystemPipeline_h__
|
||||||
|
#define SystemPipeline_h__
|
||||||
|
|
||||||
|
#include "../Common.h"
|
||||||
|
#include "EventBroker.h"
|
||||||
|
#include "System.h"
|
||||||
|
#include "World.h"
|
||||||
|
|
||||||
|
class SystemPipeline
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
SystemPipeline(const EventBroker* eventBroker)
|
||||||
|
: m_EventBroker(eventBroker)
|
||||||
|
{ }
|
||||||
|
~SystemPipeline()
|
||||||
|
{
|
||||||
|
for (auto& pair : m_Systems) {
|
||||||
|
for (auto& system : pair.second) {
|
||||||
|
delete system;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T, typename... Arguments>
|
||||||
|
void AddSystem(Arguments... args)
|
||||||
|
{
|
||||||
|
System* system = new T(m_EventBroker, args...);
|
||||||
|
if (!system->m_ComponentType.empty()) {
|
||||||
|
m_Systems[system->m_ComponentType].push_back(system);
|
||||||
|
} else {
|
||||||
|
LOG_ERROR("Failed to add system \"%s\": Missing component type!", typeid(T).name());
|
||||||
|
delete system;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Update(World* world, double dt)
|
||||||
|
{
|
||||||
|
for (auto& pair : m_Systems) {
|
||||||
|
const std::string& componentName = pair.first;
|
||||||
|
auto& systems = pair.second;
|
||||||
|
const ComponentPool* pool = world->GetComponents(componentName);
|
||||||
|
if (pool == nullptr) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (auto& component : *pool) {
|
||||||
|
for (auto& system : systems) {
|
||||||
|
system->Update(world, component, dt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
const EventBroker* m_EventBroker;
|
||||||
|
std::unordered_map<std::string, std::vector<System*>> m_Systems;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -11,6 +11,7 @@
|
|||||||
#include "Rendering/RenderQueueFactory.h"
|
#include "Rendering/RenderQueueFactory.h"
|
||||||
#include "Core/EKeyDown.h"
|
#include "Core/EKeyDown.h"
|
||||||
#include "Core/EntityXMLFile.h"
|
#include "Core/EntityXMLFile.h"
|
||||||
|
#include "Core/SystemPipeline.h"
|
||||||
|
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
@@ -29,6 +30,7 @@ private:
|
|||||||
InputManager* m_InputManager;
|
InputManager* m_InputManager;
|
||||||
GUI::Frame* m_FrameStack;
|
GUI::Frame* m_FrameStack;
|
||||||
World* m_World;
|
World* m_World;
|
||||||
|
SystemPipeline* m_SystemPipeline;
|
||||||
RenderQueueFactory* m_RenderQueueFactory;
|
RenderQueueFactory* m_RenderQueueFactory;
|
||||||
|
|
||||||
EventRelay<Game, Events::KeyUp> m_EKeyUp;
|
EventRelay<Game, Events::KeyUp> m_EKeyUp;
|
||||||
|
|||||||
@@ -42,6 +42,9 @@ Game::Game(int argc, char* argv[])
|
|||||||
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Create system pipeline
|
||||||
|
m_SystemPipeline = new SystemPipeline(m_EventBroker);
|
||||||
|
|
||||||
m_LastTime = glfwGetTime();
|
m_LastTime = glfwGetTime();
|
||||||
|
|
||||||
testIntialize();
|
testIntialize();
|
||||||
@@ -64,6 +67,8 @@ void Game::Tick()
|
|||||||
m_Renderer->Update(dt);
|
m_Renderer->Update(dt);
|
||||||
m_EventBroker->Swap();
|
m_EventBroker->Swap();
|
||||||
|
|
||||||
|
// Iterate through systems and update world!
|
||||||
|
m_SystemPipeline->Update(m_World, dt);
|
||||||
testTick(dt);
|
testTick(dt);
|
||||||
|
|
||||||
m_RenderQueueFactory->Update(m_World);
|
m_RenderQueueFactory->Update(m_World);
|
||||||
|
|||||||
Reference in New Issue
Block a user