From 352074da04bf27c7e72020166c17c9f1c692fcb7 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 10 Dec 2015 10:17:08 +0100 Subject: [PATCH] Created base for systems --- include/Engine/Core/System.h | 25 ++++++++++++ include/Engine/Core/SystemPipeline.h | 58 ++++++++++++++++++++++++++++ include/Game/Game.h | 2 + src/Game/Game.cpp | 5 +++ 4 files changed, 90 insertions(+) create mode 100644 include/Engine/Core/System.h create mode 100644 include/Engine/Core/SystemPipeline.h diff --git a/include/Engine/Core/System.h b/include/Engine/Core/System.h new file mode 100644 index 00000000..8e8c71b9 --- /dev/null +++ b/include/Engine/Core/System.h @@ -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 \ No newline at end of file diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h new file mode 100644 index 00000000..24f98121 --- /dev/null +++ b/include/Engine/Core/SystemPipeline.h @@ -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 + 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> m_Systems; +}; + +#endif \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index b561119a..d52bc8cb 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -11,6 +11,7 @@ #include "Rendering/RenderQueueFactory.h" #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" +#include "Core/SystemPipeline.h" class Game { @@ -29,6 +30,7 @@ private: InputManager* m_InputManager; GUI::Frame* m_FrameStack; World* m_World; + SystemPipeline* m_SystemPipeline; RenderQueueFactory* m_RenderQueueFactory; EventRelay m_EKeyUp; diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index eb62587e..efa8c4b7 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -41,6 +41,9 @@ Game::Game(int argc, char* argv[]) if (!mapToLoad.empty()) { ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); } + + // Create system pipeline + m_SystemPipeline = new SystemPipeline(m_EventBroker); m_LastTime = glfwGetTime(); @@ -64,6 +67,8 @@ void Game::Tick() m_Renderer->Update(dt); m_EventBroker->Swap(); + // Iterate through systems and update world! + m_SystemPipeline->Update(m_World, dt); testTick(dt); m_RenderQueueFactory->Update(m_World);