CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
System.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "Action.hpp"
13#include "ActionHandler.hpp"
14#include "Component.hpp"
15#include "Event.hpp"
16#include "EventListener.hpp"
17
19
20#include <type_traits>
21
22namespace CeresEngine {
23
26 using SystemID = unsigned int;
27
32
36 friend class SystemManager;
37
38 public:
41
42 private:
44 EntityManager* mEntityManager = nullptr;
45
47 SystemManager* mSystemManager = nullptr;
48
49 public:
51 virtual ~AbstractSystem() = default;
52
53 public:
56 virtual void update(double time) = 0;
57
60
66 virtual void attached(EntityManager& entityManager, SystemManager& systemManager);
67
72 virtual void detached(EntityManager& entityManager, SystemManager& systemManager);
73
77
80
83
86
89
92
93 private:
95
97 static inline SystemID nextSystemID = 0;
98 };
99
111 virtual void didAdd(const EO& entity) noexcept = 0;
112
118 virtual void didRemove(const EO& entity) noexcept = 0;
119
124 virtual void didChange(const EO& entity, const EntityChangeSet& changeSet) noexcept {}
125
126 private:
128 void componentsAdded(const Entity& entity) noexcept final { didAdd(entity.as<EO>()); }
129
131 void componentsRemoved(const Entity& entity) noexcept final { didRemove(entity.as<EO>()); }
132
134 void entityDirtied(const Entity& entity, const EntityChangeSet& changeSet) noexcept final { didChange(entity.as<EO>(), changeSet); }
135 };
136
168 template<typename T, typename EntityObjects = EntityObjectSet<>, typename Components = ComponentSet<>, typename Actions = EntityActionSet<>> class System;
169
171 template<typename T, typename... EOs, typename... Cs, typename... As>
173 : public AbstractSystem, private SystemComponentListener<EOs>..., private EntityActionHandler<As>... {
174 public:
177
178 public:
180 void attached(EntityManager& entityManager, SystemManager& systemManager) override;
181
183 void detached(EntityManager& entityManager, SystemManager& systemManager) override;
184
185 public:
187 static inline const SystemID systemID = []() noexcept {
188 static SystemID systemID = nextSystemID++;
189 return systemID;
190 }();
191
197 };
198
201 template<typename S> constexpr bool isSystem = std::is_base_of<AbstractSystem, S>::value;
202
205 template<typename... Ss> constexpr bool areSystems = (isSystem<Ss> && ...);
206
212 template<typename T, typename S> using ifSystem = typename std::enable_if<isSystem<S>, T>::type;
213
216 template<typename S> inline constexpr void checkSystem() { static_assert(isSystem<S>, "S must be a System type!"); }
217
218 template<typename T>
220
221 template<typename T>
222 concept CSystemImplementation = CSystem<T> && !std::is_abstract_v<T>;
223
224} // namespace CeresEngine
225
226#include "ActionManager.hpp"
227#include "EventManager.hpp"
228
229namespace CeresEngine {
230
231 template<typename T, typename... EOs, typename... Cs, typename... As>
232 void System<T, EntityObjectSet<EOs...>, ComponentSet<Cs...>, EntityActionSet<As...>>::attached(EntityManager& entityManager, SystemManager& systemManager) {
233 AbstractSystem::attached(entityManager, systemManager);
234 ((void)entityManager.actions().add(static_cast<EntityActionHandler<As>&>(*this)), ...);
235 ((void)entityManager.addListener(static_cast<SystemComponentListener<EOs>&>(*this)), ...);
236 }
237
238 template<typename T, typename... EOs, typename... Cs, typename... As>
239 void System<T, EntityObjectSet<EOs...>, ComponentSet<Cs...>, EntityActionSet<As...>>::detached(EntityManager& entityManager, SystemManager& systemManager) {
240 ((void)entityManager.removeListener(static_cast<SystemComponentListener<EOs>&>(*this)), ...);
241 ((void)entityManager.actions().remove(static_cast<EntityActionHandler<As>&>(*this)), ...);
242 AbstractSystem::detached(entityManager, systemManager);
243 }
244
245} // namespace CeresEngine
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
A abstract class that provides a trait that allows checking for system implementations.
Definition System.hpp:35
static const SystemOrder defaultOrder
The default execution updating order for systems.
Definition System.hpp:40
virtual ~AbstractSystem()=default
Defaulted virtual destructor.
virtual void update(double time)=0
Updates the system.
virtual SystemOrder getOrder() const noexcept
virtual void detached(EntityManager &entityManager, SystemManager &systemManager)
A method called by the system manager once the system has been detached from the manager.
virtual void attached(EntityManager &entityManager, SystemManager &systemManager)
A method called by the system manager once the system has been attached to the manager.
Definition ActionHandler.hpp:23
void add(EntityActionHandler< A > &actionHandler)
Adds a new action handler.
Definition ActionManager.hpp:79
void remove(EntityActionHandler< A > &actionHandler)
Removes an existing action handler.
Definition ActionManager.hpp:90
Definition EventManager.hpp:24
The base entity class.
Definition Entity.hpp:41
Definition EntityManager.hpp:49
void addListener(AbstractComponentListener &componentListener)
Add a new ComponentListener to the entity manager.
EntityActionManager & actions() noexcept
void removeListener(AbstractComponentListener &componentListener)
Removes an attached ComponentListener from the entity manager.
A system is a special kind of service that is managed internally by the SystemManager.
Definition System.hpp:168
The system manager is responsible for keeping track and dispatching updates to all attached systems.
Definition SystemManager.hpp:22
Definition System.hpp:219
Definition System.hpp:222
Definition Application.hpp:19
constexpr void checkSystem()
A trait that checks if the type S is a valid system type.
Definition System.hpp:216
constexpr bool isSystem
A trait that checks if the type S is a system.
Definition System.hpp:201
unsigned int SystemID
A numeric type that represents a system.
Definition Forward.hpp:78
int SystemOrder
A signed numeric type that represents the order in which a system should be updated by the SystemMana...
Definition System.hpp:31
typename std::enable_if< isSystem< S >, T >::type ifSystem
If the type S is a system (as defined by isSystem<S>), this type is aliased to T.
Definition System.hpp:212
std::bitset< 128 > ComponentMask
A bitset that represents a components mask (i.e. a set of components)
Definition Component.hpp:35
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
constexpr bool areSystems
A trait that checks if the types Ss are all systems.
Definition System.hpp:205
The ComponentListener is a EntityManager listener type that gets notified whenever an entity matching...
Definition Component.hpp:436
Definition Component.hpp:346
static ComponentMask getComponentMask() noexcept
A trait that contains the minimum component mask for that includes all components in the set.
Definition Component.hpp:357
Definition Action.hpp:81
Definition Entity.hpp:452
Definition Entity.hpp:756
A special type of ComponentListener that listens when an entity becomes a given EntityObject type giv...
Definition System.hpp:105
void componentsAdded(const Entity &entity) noexcept final
A notification dispatched by the SystemManager to a system.
Definition System.hpp:128
virtual void didAdd(const EO &entity) noexcept=0
A notification dispatched by the SystemManager to a system.
void entityDirtied(const Entity &entity, const EntityChangeSet &changeSet) noexcept final
A notification dispatched by the SystemManager to a system.
Definition System.hpp:134
void componentsRemoved(const Entity &entity) noexcept final
A notification dispatched by the SystemManager to a system.
Definition System.hpp:131
virtual void didChange(const EO &entity, const EntityChangeSet &changeSet) noexcept
A notification dispatched by the SystemManager to a system.
Definition System.hpp:124
virtual void didRemove(const EO &entity) noexcept=0
A notification dispatched by the SystemManager to a system.