This code demonstrates the System Manager, which manages systems in an Entity-Component-System (ECS) architecture. The code handles the dependencies between systems and provides a sorted execution order based on those dependencies.
The System Manager was created to simplify the handling of dependencies between systems in an ECS system. By automatically sorting the execution order based on dependencies, it eliminates the need for manually adjusting system indices when adding or removing systems.
- The System Manager maintains a
Datamap to store system instances, aNeedmap to store system dependencies, andSequenceandDonequeues for managing the execution order. - When a system is created using
Create(), it is added to theDatamap along with an empty dependency list in theNeedmap. - Dependencies are set using
SetDependency(), where the dependent system's name is added to the dependency list of the main system. - To determine the execution order, the
Sort()function is called. It uses a topological sorting algorithm. - The
Sort()function initializes aDepCountmap to keep track of the number of dependencies for each system. - It then iterates through the
Needmap and increments the dependency count for each system based on its dependencies. - Systems with no dependencies (count = 0) are pushed into the
Sequencequeue and marked as processed by decrementing their dependency count. - The function continues this process until all systems are processed and added to the
Sequencequeue. - Finally, the
Frame()function executes the systems in the sortedSequencequeue, and the executed systems are moved to theDonequeue. - The
Frame()function repeats this process until all systems have been executed.
- Create instances of systems using the
Create()function in theSystemManagerclass. - Set dependencies between systems using the
SetDependency()function. - Call
Sort()to sort the systems based on their dependencies. - Execute the systems in the sorted order using the
Frame()function. - Clean up the systems using
Destroy().
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <set>
#include <map>
#include <cstdarg>
#include <windows.h>
// Define ISystem struct
// Define SystemManager class
int main()
{
// Create system instances
ISystem* Render = SystemManager::GetHandle().Create("RenderSystem");
ISystem* Move = SystemManager::GetHandle().Create("MovementSystem");
ISystem* Collision = SystemManager::GetHandle().Create("CollisionSystem");
ISystem* Light = SystemManager::GetHandle().Create("LightSystem");
ISystem* Physics = SystemManager::GetHandle().Create("PhysicsSystem");
ISystem* Camera = SystemManager::GetHandle().Create("CameraSystem");
// Set system dependencies
SystemManager::GetHandle().SetDependency(Move, Collision);
SystemManager::GetHandle().SetDependency(Render, Move);
SystemManager::GetHandle().SetDependency(Render, Light);
SystemManager::GetHandle().SetDependency(Collision, Physics);
SystemManager::GetHandle().SetDependency(Light, Physics);
// Circlic Dependency
SystemManager::GetHandle().SetDependency(Camera, Render);
SystemManager::GetHandle().SetDependency(Render, Camera);
// Sort systems based on dependencies
SystemManager::GetHandle().Sort();
// Execute systems in the sorted order
SystemManager::GetHandle().Frame();
// Clean up systems
SystemManager::GetHandle().Destroy();
return 0;
}