Engine
Raylib based game framework
Loading...
Searching...
No Matches
SystemManager.h
1#pragma once
2
3#include "Types.h"
4
5#include <algorithm>
6#include <memory>
7#include <type_traits>
8#include <typeindex>
9#include <unordered_map>
10#include <utility>
11#include <vector>
12
19class System
20{
21public:
22
23 virtual ~System()
24 {
25 }
26
36 virtual void Update(const float deltaT) = 0;
37
45 virtual void Draw() = 0;
46};
47
57{
58public:
59
66 void Update(const float deltaT);
67
71 void Draw();
72
92 template <typename T, typename... Args>
93 requires std::is_base_of_v<System, T>
94 T& AddSystem(const u32 priority = 1, Args&&... args)
95 {
96 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
97 T& ref = *ptr;
98
99 m_systems.push_back(std::make_pair(priority, std::move(ptr)));
100 m_systemsMap.emplace(typeid(T), ptr.get());
101
102 std::sort(m_systems.begin(), m_systems.end(), [](const auto& a, const auto& b)
103 {
104 return a.first > b.first;
105 });
106
107 return ref;
108 }
109
110 template <typename T>
111 requires std::is_base_of_v<System, T>
112 void RemoveSystem()
113 {
114 auto it = m_systemsMap.find(typeid(T));
115 if (it == m_systemsMap.end())
116 {
117 return;
118 }
119
120 System* ptr = it->second;
121 i32 index = -1;
122
123 for (u32 i = 0; i < m_systems.size(); ++i)
124 {
125 if (m_systems[i].second.get() == ptr)
126 {
127 index = 0;
128 break;
129 }
130 }
131
132 if (index != -1)
133 {
134 m_systems.erase(m_systems.begin() + index);
135 }
136 }
137
146 template <typename T>
147 requires std::is_base_of_v<System, T>
149 {
150 auto it = m_systemsMap.find(typeid(T));
151 if (it != m_systemsMap.end())
152 {
153 return it->second;
154 }
155
156 return nullptr;
157 }
158
163 void ClearSystems();
164
165private:
166
167 std::vector<std::pair<u32, std::unique_ptr<System>>> m_systems;
168 std::unordered_map<std::type_index, System*> m_systemsMap;
169};
Manages execution and order of systems.
Definition SystemManager.h:57
void Draw()
Renders all systems in order.
Definition SystemManager.cpp:11
T * GetSystem()
Gets a system pointer.
Definition SystemManager.h:148
T & AddSystem(const u32 priority=1, Args &&... args)
Adds a system to the manager.
Definition SystemManager.h:94
void Update(const float deltaT)
Updates all systems in order.
Definition SystemManager.cpp:3
void ClearSystems()
Clears all systems.
Definition SystemManager.cpp:19
Base class for all systems.
Definition SystemManager.h:20
virtual void Update(const float deltaT)=0
Updates the system.
virtual void Draw()=0
Renders the system.