Acheron
Loading...
Searching...
No Matches
system.hpp
1#pragma once
2
3#include "types.hpp"
4
5#include <cassert>
6#include <memory>
7#include <string>
8#include <vector>
9#include <unordered_map>
10
11namespace acheron::ecs {
12 class World;
13
17
18
22 struct SystemStage {
23 SystemStageID id;
24 std::string name;
25
26 bool operator==(const SystemStage& other) const noexcept {
27 return id == other.id;
28 }
29 };
30
34 class System {
35 public:
39 std::unordered_set<Entity> entities;
40
47 virtual void Update(World& world, double dt) {}
48 };
49
54 public:
55
66 template<typename T>
67 std::shared_ptr<T> RegisterSystem(std::string stage) {
68 auto typeName = typeid(T).name();
69 static_assert(std::is_base_of<System, T>::value, "Trying to register system that doesnt inherit System");
70
71 assert(systems.find(typeName) == systems.end() && "Duplicate system registration");
72
73 auto system = std::make_shared<T>();
74 systems[typeName] = system;
75 stageSystems[nameToID[stage]].push_back(system);
76
77 return system;
78 }
79
86 template<typename T>
87 void SetSignature(Signature signature) {
88 auto typeName = typeid(T).name();
89
90 assert(systems.find(typeName) != systems.end() && "System used before registration");
91
92 signatures[typeName] = signature;
93 }
94
102 void EntityDespawned(Entity entity);
103
111 SystemStageID GetOrCreateStage(const std::string& name);
112
121 SystemStageID GetStageOrFail(const std::string& name);
122
129 void StageBefore(std::string name, std::string after);
130
137 void StageAfter(std::string name, std::string before);
138
142 void PopulateStages();
143
150 void EntitySignatureChanged(Entity entity, Signature signature);
151
152 std::unordered_map<std::string, SystemStageID> nameToID;
153 std::unordered_map<SystemStageID, std::vector<SystemStageID>> edges;
154 std::vector<SystemStage> stages;
155 std::vector<SystemStageID> orderedStages;
156
157 std::unordered_map<SystemStageID, std::vector<std::shared_ptr<System>>> stageSystems;
158 std::unordered_map<std::string, std::shared_ptr<System>> systems;
159 std::unordered_map<std::string, Signature> signatures;
160
161 SystemStageID counter;
162
163 bool per_entity = true;
164 };
165}
Manages systems and the stages associated with them.
Definition system.hpp:53
void StageAfter(std::string name, std::string before)
Creates a stage before another.
Definition system.cpp:64
std::unordered_map< std::string, SystemStageID > nameToID
Maps a stage name to its respective ID.
Definition system.hpp:152
SystemStageID GetStageOrFail(const std::string &name)
Gets a StageID by name.
Definition system.cpp:36
std::vector< SystemStage > stages
A list of registered stages.
Definition system.hpp:154
std::vector< SystemStageID > orderedStages
Stages put in order by PopulateStages.
Definition system.hpp:155
SystemStageID GetOrCreateStage(const std::string &name)
Gets a StageID by name, or creates one if it doesnt exist.
Definition system.cpp:45
std::unordered_map< SystemStageID, std::vector< SystemStageID > > edges
How a stage is associated with one another.
Definition system.hpp:153
std::unordered_map< std::string, Signature > signatures
A map of systems name and the signature associated with them.
Definition system.hpp:159
std::shared_ptr< T > RegisterSystem(std::string stage)
Registers a system.
Definition system.hpp:67
void PopulateStages()
Clears and populates ordereredStages.
Definition system.cpp:70
std::unordered_map< std::string, std::shared_ptr< System > > systems
A map of systems and their name.
Definition system.hpp:158
void EntityDespawned(Entity entity)
Called when an entity is despawned.
Definition system.cpp:10
void StageBefore(std::string name, std::string after)
Creates a stage before another.
Definition system.cpp:57
void SetSignature(Signature signature)
Sets a systems signature.
Definition system.hpp:87
void EntitySignatureChanged(Entity entity, Signature signature)
Updates the system signature based on the entities new signature.
Definition system.cpp:16
Systems called every frame based on entities.
Definition system.hpp:34
virtual void Update(World &world, double dt)
This function is overriden with the functionality of the System.
Definition system.hpp:47
std::unordered_set< Entity > entities
List of entities a system is matched with.
Definition system.hpp:39
Central context for the ECS.
Definition world.hpp:20
Size for the stage ID.
Definition system.hpp:22