10#include "../Logger/Logger.hpp"
29 const std::string &afterSystemName) {
31 LOG_ERROR(
"SystemScheduler::runBefore - System '", systemName,
"' not registered");
35 _systemInfos[systemName].runBefore.push_back(afterSystemName);
42 const std::string &beforeSystemName) {
44 LOG_ERROR(
"SystemScheduler::runAfter - System '", systemName,
"' not registered");
48 _systemInfos[systemName].runAfter.push_back(beforeSystemName);
57 it->second.enabled =
true;
64 it->second.enabled =
false;
71 return it->second.enabled;
90 [
this](
const std::string &a,
const std::string &b) {
91 return _systemInfos[a].priority > _systemInfos[b].priority;
102 std::unordered_map<std::string, std::vector<std::string>> graph;
103 std::unordered_map<std::string, int> inDegree;
114 for (
const auto &target : info.runBefore) {
116 graph[name].push_back(target);
122 for (
const auto &dependency : info.runAfter) {
124 graph[dependency].push_back(name);
131 std::vector<std::string> sorted;
132 std::vector<std::string> queue;
135 for (
const auto &[name, degree] : inDegree) {
137 queue.push_back(name);
142 std::sort(queue.begin(), queue.end(), [
this](
const std::string &a,
const std::string &b) {
143 return _systemInfos[a].priority > _systemInfos[b].priority;
146 while (!queue.empty()) {
147 std::string current = queue.back();
149 sorted.push_back(current);
152 for (
const auto &neighbor : graph[current]) {
153 inDegree[neighbor]--;
154 if (inDegree[neighbor] == 0) {
155 queue.push_back(neighbor);
160 std::sort(queue.begin(), queue.end(), [
this](
const std::string &a,
const std::string &b) {
161 return _systemInfos[a].priority > _systemInfos[b].priority;
167 LOG_WARNING(
"Circular dependencies detected. Some systems may not execute in optimal order.");
191 std::cout <<
"=== System Execution Order ===" << std::endl;
196 std::cout << (i + 1) <<
". " << name <<
" (priority: " << it->second.priority
197 <<
", enabled: " << (it->second.enabled ?
"yes" :
"no") <<
")" << std::endl;
200 std::cout <<
"==============================" << std::endl;
High-level ECS manager providing clean server-side API.
bool updateSystem(const std::string &name, float deltaTime)
Update a specific system by name.
Advanced scheduler for controlling system execution order and dependencies.
const std::vector< std::string > & getExecutionOrder() const
Get the computed execution order.
void disable(const std::string &name)
Disable a system.
bool isEnabled(const std::string &name) const
Check if a system is enabled.
void printExecutionOrder() const
Print the execution order for debugging.
SystemScheduler & registerSystem(const std::string &name, int priority=0)
Register a system with the scheduler.
void computeExecutionOrder()
std::unordered_map< std::string, SystemInfo > _systemInfos
SystemScheduler & runBefore(const std::string &systemName, const std::string &afterSystemName)
Specify that a system must run before another.
std::vector< std::string > _executionOrder
SystemScheduler & runAfter(const std::string &systemName, const std::string &beforeSystemName)
Specify that a system must run after another.
void update(float deltaTime)
Update all enabled systems in the scheduled order.
void enable(const std::string &name)
Enable a system.
SystemScheduler(ECSWorld *world)
Construct a SystemScheduler.
Information about a scheduled system.
int priority
Higher priority systems run first.