From de8461e2579e567ed328234cb29f95be7bf049af Mon Sep 17 00:00:00 2001 From: William Moberg Date: Fri, 18 Dec 2015 15:33:25 +0100 Subject: [PATCH] SystemPipeline should update systems in order depending on input update priority in AddSystem. --- include/Engine/Core/SystemPipeline.h | 68 ++++++++++++++++------------ src/Game/Game.cpp | 14 ++++-- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h index 78ebc966..fdae834f 100644 --- a/include/Engine/Core/SystemPipeline.h +++ b/include/Engine/Core/SystemPipeline.h @@ -14,23 +14,29 @@ public: { } ~SystemPipeline() { - for (auto& pair : m_PureSystems) { - for (auto& system : pair.second) { - delete system; + for (UnorderedSystems& group : m_OrderedSystemGroups) { + for (auto& pair : group.PureSystems) { + for (auto& system : pair.second) { + delete system; + } } } } template - void AddSystem(Arguments... args) + void AddSystem(int updateOrderPriority, Arguments... args) { + if (updateOrderPriority + 1 > m_OrderedSystemGroups.size()) { + m_OrderedSystemGroups.resize(updateOrderPriority + 1); + } + UnorderedSystems& group = m_OrderedSystemGroups[updateOrderPriority]; System* system = new T(m_EventBroker, args...); - m_Systems[typeid(T).name()] = system; + group.Systems[typeid(T).name()] = system; if (std::is_base_of::value) { PureSystem* pureSystem = static_cast(system); if (!pureSystem->m_ComponentType.empty()) { - m_PureSystems[pureSystem->m_ComponentType].push_back(pureSystem); + group.PureSystems[pureSystem->m_ComponentType].push_back(pureSystem); } else { LOG_ERROR("Failed to add pure system \"%s\": Missing component type!", typeid(T).name()); } @@ -38,41 +44,47 @@ public: if (std::is_base_of::value) { ImpureSystem* impureSystem = static_cast(system); - m_ImpureSystems.push_back(impureSystem); + group.ImpureSystems.push_back(impureSystem); } } void Update(World* world, double dt) { - // Process events - for (auto& pair : m_Systems) { - m_EventBroker->Process(pair.first); - } - - // Update - for (auto& pair : m_PureSystems) { - const std::string& componentName = pair.first; - auto& systems = pair.second; - const ComponentPool* pool = world->GetComponents(componentName); - if (pool == nullptr) { - continue; + for (UnorderedSystems& group : m_OrderedSystemGroups) { + // Process events + for (auto& pair : group.Systems) { + m_EventBroker->Process(pair.first); } - for (auto& component : *pool) { - for (auto& system : systems) { - system->UpdateComponent(world, component, dt); + + // Update + for (auto& pair : group.PureSystems) { + 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->UpdateComponent(world, component, dt); + } } } - } - for (auto& system : m_ImpureSystems) { - system->Update(world, dt); + for (auto& system : group.ImpureSystems) { + system->Update(world, dt); + } } } private: EventBroker* m_EventBroker; - std::map m_Systems; - std::map> m_PureSystems; - std::vector m_ImpureSystems; + struct UnorderedSystems + { + std::map Systems; + std::map> PureSystems; + std::vector ImpureSystems; + }; + std::vector m_OrderedSystemGroups; }; #endif \ No newline at end of file diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index bf990e23..b0628bd8 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -52,11 +52,15 @@ Game::Game(int argc, char* argv[]) // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); - m_SystemPipeline->AddSystem(); - m_SystemPipeline->AddSystem(); - m_SystemPipeline->AddSystem(m_Renderer); - m_SystemPipeline->AddSystem(); - m_SystemPipeline->AddSystem(); + unsigned int updateOrderPriority = 0; + m_SystemPipeline->AddSystem(updateOrderPriority); + m_SystemPipeline->AddSystem(updateOrderPriority); + m_SystemPipeline->AddSystem(updateOrderPriority, m_Renderer); + + //Collision and TriggerSystem should update after player. + ++updateOrderPriority; + m_SystemPipeline->AddSystem(updateOrderPriority); + m_SystemPipeline->AddSystem(updateOrderPriority); m_LastTime = glfwGetTime();