SystemPipeline should update systems in order depending on input update priority in AddSystem.

This commit is contained in:
William Moberg
2015-12-18 15:33:25 +01:00
parent 7dcd75fccc
commit de8461e257
2 changed files with 49 additions and 33 deletions
+40 -28
View File
@@ -14,23 +14,29 @@ public:
{ } { }
~SystemPipeline() ~SystemPipeline()
{ {
for (auto& pair : m_PureSystems) { for (UnorderedSystems& group : m_OrderedSystemGroups) {
for (auto& system : pair.second) { for (auto& pair : group.PureSystems) {
delete system; for (auto& system : pair.second) {
delete system;
}
} }
} }
} }
template <typename T, typename... Arguments> template <typename T, typename... Arguments>
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...); System* system = new T(m_EventBroker, args...);
m_Systems[typeid(T).name()] = system; group.Systems[typeid(T).name()] = system;
if (std::is_base_of<PureSystem, T>::value) { if (std::is_base_of<PureSystem, T>::value) {
PureSystem* pureSystem = static_cast<PureSystem*>(system); PureSystem* pureSystem = static_cast<PureSystem*>(system);
if (!pureSystem->m_ComponentType.empty()) { if (!pureSystem->m_ComponentType.empty()) {
m_PureSystems[pureSystem->m_ComponentType].push_back(pureSystem); group.PureSystems[pureSystem->m_ComponentType].push_back(pureSystem);
} else { } else {
LOG_ERROR("Failed to add pure system \"%s\": Missing component type!", typeid(T).name()); LOG_ERROR("Failed to add pure system \"%s\": Missing component type!", typeid(T).name());
} }
@@ -38,41 +44,47 @@ public:
if (std::is_base_of<ImpureSystem, T>::value) { if (std::is_base_of<ImpureSystem, T>::value) {
ImpureSystem* impureSystem = static_cast<ImpureSystem*>(system); ImpureSystem* impureSystem = static_cast<ImpureSystem*>(system);
m_ImpureSystems.push_back(impureSystem); group.ImpureSystems.push_back(impureSystem);
} }
} }
void Update(World* world, double dt) void Update(World* world, double dt)
{ {
// Process events for (UnorderedSystems& group : m_OrderedSystemGroups) {
for (auto& pair : m_Systems) { // Process events
m_EventBroker->Process(pair.first); for (auto& pair : group.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 (auto& component : *pool) {
for (auto& system : systems) { // Update
system->UpdateComponent(world, component, dt); 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 : group.ImpureSystems) {
for (auto& system : m_ImpureSystems) { system->Update(world, dt);
system->Update(world, dt); }
} }
} }
private: private:
EventBroker* m_EventBroker; EventBroker* m_EventBroker;
std::map<std::string, System*> m_Systems; struct UnorderedSystems
std::map<std::string, std::vector<PureSystem*>> m_PureSystems; {
std::vector<ImpureSystem*> m_ImpureSystems; std::map<std::string, System*> Systems;
std::map<std::string, std::vector<PureSystem*>> PureSystems;
std::vector<ImpureSystem*> ImpureSystems;
};
std::vector<UnorderedSystems> m_OrderedSystemGroups;
}; };
#endif #endif
+9 -5
View File
@@ -52,11 +52,15 @@ Game::Game(int argc, char* argv[])
// Create system pipeline // Create system pipeline
m_SystemPipeline = new SystemPipeline(m_EventBroker); m_SystemPipeline = new SystemPipeline(m_EventBroker);
m_SystemPipeline->AddSystem<RaptorCopterSystem>(); unsigned int updateOrderPriority = 0;
m_SystemPipeline->AddSystem<PlayerSystem>(); m_SystemPipeline->AddSystem<RaptorCopterSystem>(updateOrderPriority);
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer); m_SystemPipeline->AddSystem<PlayerSystem>(updateOrderPriority);
m_SystemPipeline->AddSystem<CollisionSystem>(); m_SystemPipeline->AddSystem<EditorSystem>(updateOrderPriority, m_Renderer);
m_SystemPipeline->AddSystem<TriggerSystem>();
//Collision and TriggerSystem should update after player.
++updateOrderPriority;
m_SystemPipeline->AddSystem<CollisionSystem>(updateOrderPriority);
m_SystemPipeline->AddSystem<TriggerSystem>(updateOrderPriority);
m_LastTime = glfwGetTime(); m_LastTime = glfwGetTime();