14#ifndef FOOLSENGINE_ECSWORLD_H
15#define FOOLSENGINE_ECSWORLD_H
25#include <unordered_map>
29#include <nlohmann/json.hpp>
254 virtual std::shared_ptr<BaseComponentArray>
instantiate(std::weak_ptr<ECSWorld> world)
const = 0;
288 template <
typename TComponent,
typename Enable=
void>
296 static TComponent
get(
const nlohmann::json& jsonComponent){
299 TComponent component = jsonComponent;
314 template <
typename TComponent,
typename Enable>
316 static std::shared_ptr<TComponent>
get(
const nlohmann::json& jsonComponent) {
319 std::shared_ptr<TComponent> component {
new TComponent{} = jsonComponent };
347 T
operator() (
const T& previousState,
const T& nextState,
float simulationProgress=1.f)
const;
365 template<
typename TComponent>
383 std::shared_ptr<BaseComponentArray>
instantiate(std::weak_ptr<ECSWorld> world)
const override;
534 template<
typename TComponent>
542 template <
typename TComponent>
550 return TComponent::getComponentTypeName();
560 template <
typename TComponent>
563 return TComponent::getComponentTypeName();
573 template<
typename TComponent>
575 const std::size_t componentHash {
typeid(TComponent).hash_code() };
577 return std::dynamic_pointer_cast<ComponentArray<TComponent>>(
mHashToComponentArray.at(componentHash));
586 std::shared_ptr<BaseComponentArray>
getComponentArray(
const std::string& componentTypeName)
const {
597 template<
typename TComponent>
632 template<
typename TComponent>
649 template<
typename TComponent>
670 template<
typename TComponent>
693 template<
typename TComponent>
705 template<
typename TComponent>
725 template<
typename TComponent>
806 friend class ECSWorld;
814 class BaseSystem :
public std::enable_shared_from_this<BaseSystem> {
854 template <
typename TComponent,
typename TSystem>
865 template <
typename TComponent,
typename TSystem>
892 virtual std::shared_ptr<BaseSystem>
instantiate(std::weak_ptr<ECSWorld> world) = 0;
952 virtual void onEntityUpdated(
EntityID entityID) { (void)entityID; assert(
false &&
"The base class version of onEntityUpdated should never be called"); }
1005 virtual void onVariableStep(
float simulationProgress, uint32_t variableStepMillis) {(void)simulationProgress; (void)variableStepMillis;}
1061 template <
typename TSystemDerived,
typename TListenedForComponentsTuple,
typename TRequiredComponentsTuple>
1062 class System{
static_assert(
false &&
"Non specialized system cannot be declared"); };
1074 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1075 class System<TSystemDerived,
std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>:
public BaseSystem {
1102 template<
typename TComponent>
1104 assert(!
isSingleton() &&
"Singletons cannot retrieve components by EntityID alone");
1115 template<
typename TComponent>
1117 assert(!
isSingleton() &&
"Singletons cannot retrieve components by EntityID alone");
1127 std::shared_ptr<BaseSystem>
instantiate(std::weak_ptr<ECSWorld> world)
override;
1135 Registrator<
System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>::getRegistrator()
1138 friend class Registrator<
System<TSystemDerived,
std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>;
1163 SystemManager instantiate(std::weak_ptr<ECSWorld> world)
const;
1174 template<
typename TSystem>
1175 void registerSystem(
const Signature& signature,
const Signature& listenedForComponents);
1181 void unregisterAll();
1189 template<
typename TSystem>
1190 std::shared_ptr<TSystem> getSystem();
1198 template<
typename TSystem>
1199 void enableEntity(
EntityID entityID);
1218 template<
typename TSystem>
1219 void disableEntity(EntityID entityID);
1227 void disableEntity(EntityID entityID, Signature entitySignature);
1237 template<
typename TSystem>
1238 SystemType getSystemType()
const;
1248 template<
typename TSystem>
1249 bool isEnabled(EntityID entityID);
1259 template <
typename TSystem>
1260 bool isRegistered(EntityID entityID);
1268 void handleEntitySignatureChanged(EntityID entityID, Signature signature);
1275 void handleEntityDestroyed(EntityID entityID);
1284 void handleEntityUpdated(EntityID entityID, Signature signature, ComponentType updatedComponent);
1295 template<
typename TSystem>
1296 void handleEntityUpdatedBySystem(EntityID entityID, Signature signature, ComponentType updatedComponent);
1303 void handleInitialize();
1310 void handleSimulationActivated();
1319 void handleSimulationPreStep(uint32_t simStepMillis);
1328 void handleSimulationStep(uint32_t simStepMillis);
1337 void handleSimulationPostStep(uint32_t simStepMillis);
1346 void handlePostTransformUpdate(uint32_t timeStepMillis);
1357 void handleVariableStep(
float simulationProgress, uint32_t variableStepMillis);
1366 void handlePreRenderStep(
float simulationProgress);
1373 void handlePostRenderStep(
float simulationProgress);
1379 void handleSimulationDeactivated();
1416 friend class ECSWorld;
1417 friend class BaseSystem;
1439 class ECSWorld:
public std::enable_shared_from_this<ECSWorld> {
1518 template<
typename ...TComponent>
1528 template <
typename TSystemDerived,
typename TListenedForComponents,
typename TRequiredComponents>
1541 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1590 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1599 template<
typename TSystem>
1610 template<
typename TSystem>
1623 template <
typename TSingletonSystem>
1636 template <
typename TSystem>
1647 template <
typename TSystem>
1658 template <
typename TSystem>
1670 template<
typename ...TComponents>
1684 template <
typename ...TComponents>
1757 void variableStep(
float simulationProgress, uint32_t variableStepMillis);
1799 static std::shared_ptr<ECSWorld> createWorld();
1806 static std::weak_ptr<ECSWorld> getInstance();
1845 template<
typename ...TComponents>
1865 template<
typename TSystem>
1886 template<
typename TSystem>
1887 void disableEntity(
EntityID entityID);
1895 void disableEntity(
EntityID entityID);
1906 template<
typename TComponent>
1907 void addComponent(
EntityID entityID,
const TComponent& component);
1917 void addComponent(
EntityID entityID,
const nlohmann::json& jsonComponent);
1930 template<
typename TComponent>
1931 TComponent getComponent(
EntityID entityID,
float simulationProgress=1.f)
const;
1943 template<
typename TComponent,
typename TSystem>
1944 TComponent getComponent(
EntityID entityID,
float simulationProgress=1.f)
const;
1956 template <
typename TComponent>
1957 bool hasComponent(
EntityID entityID)
const;
1970 bool hasComponent(
EntityID entityID,
const std::string& typeName)
const;
1981 template<
typename TComponent>
1982 void updateComponent(
EntityID entityID,
const TComponent& newValue);
1992 void updateComponent(
EntityID entityID,
const nlohmann::json& newValue);
2004 template<
typename TComponent,
typename TSystem>
2005 void updateComponent(
EntityID entityID,
const TComponent& newValue);
2016 template<
typename TSystem>
2017 void updateComponent(
EntityID entityID,
const nlohmann::json& newValue);
2027 template<
typename TComponent>
2028 void removeComponent(
EntityID entityID);
2036 void removeComponent(
EntityID entityID,
const std::string& typeName);
2043 void removeComponentsAll(
EntityID entityID);
2083 friend class Entity;
2084 friend class BaseSystem;
2146 void copy(
const Entity& other);
2156 template<
typename TComponent>
2157 void addComponent(
const TComponent& component);
2166 void addComponent(
const nlohmann::json& jsonComponent);
2175 template<
typename TComponent>
2176 void removeComponent();
2185 void removeComponent(
const std::string& typeName);
2197 template<
typename TComponent>
2198 bool hasComponent()
const;
2209 bool hasComponent(
const std::string& typeName)
const;
2220 template<
typename TComponent>
2221 TComponent getComponent(
float simulationProgress=1.f)
const;
2231 template<
typename TComponent>
2232 void updateComponent(
const TComponent& newValue);
2241 void updateComponent(
const nlohmann::json& jsonValue);
2252 template<
typename TSystem>
2253 bool isEnabled()
const;
2264 template <
typename TSystem>
2265 bool isRegistered()
const;
2274 template<
typename TSystem>
2275 void enableSystem();
2284 template<
typename TSystem>
2285 void disableSystem();
2292 void disableSystems();
2299 void enableSystems(
Signature systemMask);
2335 friend class ECSWorld;
2345 template<
typename T>
2347 if(simulationProgress < .5f)
return previousState;
2351 template<
typename TComponent>
2363 template <
typename TComponent>
2368 template <
typename TComponent>
2373 template<
typename TComponent>
2378 template<
typename TComponent>
2402 template <
typename TComponent>
2410 template <
typename TComponent>
2417 template <
typename TComponent>
2424 template<
typename TComponent>
2426 std::copy<typename std::vector<TComponent>::iterator,
typename std::vector<TComponent>::iterator>(
2432 template <
typename TComponent>
2437 template <
typename TComponent>
2439 assert(to <
kMaxEntities &&
"Cannot copy to an entity with an invalid entity ID");
2441 if(downcastOther.mEntityToComponentIndex.find(from) == downcastOther.mEntityToComponentIndex.end())
return;
2443 const TComponent& componentValueNext { downcastOther.mComponentsNext[downcastOther.mEntityToComponentIndex[from]] };
2444 const TComponent& componentValuePrevious { downcastOther.mComponentsPrevious[downcastOther.mEntityToComponentIndex[from]] };
2454 template<
typename TComponent>
2456 const std::size_t componentHash {
typeid(TComponent).hash_code() };
2465 has already been registered");
2474 template<
typename TComponent>
2476 const std::size_t componentHash {
typeid(TComponent).hash_code() };
2481 template<
typename TComponent>
2487 template <
typename TComponent>
2492 template<
typename TComponent>
2498 template<
typename TComponent>
2503 template<
typename TComponent>
2508 template <
typename TComponent>
2515 template<
typename TSystem>
2517 const std::string systemTypeName{ TSystem::getSystemTypeName() };
2522 template <
typename TSystem>
2526 template <
typename TSystem>
2531 template <
typename TComponent>
2537 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2538 void System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>
::registerSelf() {
2545 template<
typename TSystem>
2547 const std::string systemTypeName { TSystem::getSystemTypeName() };
2557 template<
typename TSystem>
2559 std::string systemTypeName { TSystem::getSystemTypeName() };
2561 return std::dynamic_pointer_cast<TSystem>(
mNameToSystem[systemTypeName]);
2564 template<
typename TSystem>
2566 std::string systemTypeName { TSystem::getSystemTypeName() };
2570 template<
typename TSystem>
2572 std::string systemTypeName { TSystem::getSystemTypeName() };
2576 template <
typename TComponent>
2578 mWorld.lock()->addComponent<TComponent>(
mID, component);
2581 template <
typename TComponent>
2583 return mWorld.lock()->hasComponent<TComponent>(
mID);
2586 template<
typename TComponent>
2588 return mWorld.lock()->getComponent<TComponent>(
mID, simulationProgress);
2591 template<
typename TComponent>
2593 mWorld.lock()->updateComponent<TComponent>(
mID, newValue);
2596 template <
typename TComponent>
2598 mWorld.lock()->removeComponent<TComponent>(
mID);
2601 template <
typename TSystem>
2603 mWorld.lock()->enableEntity<TSystem>(
mID);
2606 template <
typename TSystem>
2608 return mWorld.lock()->isEnabled<TSystem>(
mID);
2611 template <
typename TSystem>
2613 return mWorld.lock()->isRegistered<TSystem>(
mID);
2616 template <
typename TSystem>
2618 mWorld.lock()->disableEntity<TSystem>(
mID);
2621 template <
typename TSystem>
2623 const std::string systemTypeName { TSystem::getSystemTypeName() };
2627 template <
typename TSystem>
2629 const std::string systemTypeName { TSystem::getSystemTypeName() };
2630 return mNameToSystem[systemTypeName]->isRegistered(entityID);
2633 template<
typename ...TComponents>
2645 Entity entity { nextID, shared_from_this()};
2651 template<
typename TSystem>
2656 template<
typename TComponent>
2658 assert(entityID <
kMaxEntities &&
"Cannot add a component to an entity that does not exist");
2661 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2664 template<
typename TComponent>
2668 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2671 template<
typename TSystem>
2676 template<
typename TSystem>
2681 template<
typename TComponent>
2686 template<
typename TComponent,
typename TSystem>
2690 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2693 &&
"This system cannot access this kind of component"
2699 template<
typename TComponent>
2709 template<
typename TComponent,
typename TSystem>
2713 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2716 &&
"This system cannot access this kind of component"
2726 template <
typename TSystem>
2730 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2733 &&
"This system cannot access this kind of component"
2743 template<
typename ...TComponents>
2745 ((
getInstance().lock()->mComponentManager->registerComponentArray<TComponents>()),...);
2748 template<
typename TSystem,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2755 (listensFor.set(getInstance().lock()->mComponentManager->getComponentType<TListenedForComponents>()), ...);
2756 (required.set(getInstance().lock()->mComponentManager->getComponentType<TRequiredComponents>()), ...);
2758 getInstance().lock()->mSystemManager->registerSystem<TSystem>(required|listensFor, listensFor);
2761 template<
typename ...TComponents>
2766 template <
typename ...TComponents>
2771 template<
typename TSystem>
2776 template<
typename TSystem>
2778 return getInstance().lock()->mSystemManager->getSystem<TSystem>();
2781 template <
typename TSingletonSystem>
2783 std::shared_ptr<TSingletonSystem> system {
getInstance().lock()->getSystem<TSingletonSystem>() };
2784 assert(system->isSingleton() &&
"System specified is not an ECSWorld-aware singleton system");
2788 template<
typename TComponent,
typename TSystem>
2790 assert(!
isSingleton() &&
"Singletons cannot retrieve entity components through entity ID alone");
2791 return mWorld.lock()->getComponent<TComponent, TSystem>(entityID, progress);
2793 template<
typename TSystem,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2794 std::shared_ptr<BaseSystem>
System<TSystem, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>::instantiate(std::weak_ptr<ECSWorld> world) {
2795 if(isSingleton())
return shared_from_this();
2796 return std::make_shared<TSystem>(world);
2799 template <
typename TComponent>
2801 return std::make_shared<ComponentArray<TComponent>>(world);
2804 template<
typename TComponent,
typename TSystem>
2806 assert(!
isSingleton() &&
"Singletons cannot retrieve entity components through entity ID alone");
2807 mWorld.lock()->updateComponent<TComponent, TSystem>(entityID, component);
2810 template<
typename TSystem>
2812 std::string originatingSystemTypeName { TSystem::getSystemTypeName() };
2815 if((pair.second&signature) != pair.second)
continue;
2818 if(pair.first == originatingSystemTypeName)
continue;
2825 if(system.isSingleton() || !system.isEnabled(entityID))
continue;
2828 system.onEntityUpdated(entityID);
An abstract base class for all ECS component arrays.
Definition ecs_world.hpp:168
virtual void copyComponent(EntityID to, EntityID from, BaseComponentArray &other)=0
Handles the copying of a component from one entity to another, where the other entity belongs to anot...
virtual void updateComponent(EntityID to, const nlohmann::json &jsonComponent)=0
Updates the component associated with an entity based on a json description of a new component.
virtual ~BaseComponentArray()=default
Destroy the Base Component Array object.
virtual std::shared_ptr< BaseComponentArray > instantiate(std::weak_ptr< ECSWorld > world) const =0
Creates a fresh, empty component array and associates it with a new World.
std::weak_ptr< ECSWorld > mWorld
A reference to the world to which this component array belongs.
Definition ecs_world.hpp:262
virtual void handleEntityDestroyed(EntityID entityID)=0
A virtual function that handles the side-effect of destroying an entity, i.e., deleting its component...
virtual void copyComponent(EntityID to, EntityID from)=0
Handles the copying of a component from one entity to another within the same array.
virtual void addComponent(EntityID to, const nlohmann::json &jsonComponent)=0
Constructs and adds a component to an array based on its json description.
BaseComponentArray(std::weak_ptr< ECSWorld > world)
Construct a new Base Component Array object.
Definition ecs_world.hpp:176
virtual bool hasComponent(EntityID entityID) const =0
Tests whether this array has an entry for this entity.
virtual void removeComponent(EntityID entityID)=0
Removes the component associated with this entity, if present.
virtual void handlePreSimulationStep()=0
An unimplemented callback for a step that occurs before each simulation step.
The base class that acts as the interface between the engine's ECS system and a particular built-in o...
Definition ecs_world.hpp:814
virtual void onInitialize()
An overridable callback for right after an ECS world has just been created.
Definition ecs_world.hpp:958
virtual void onSimulationDeactivated()
Overridable callback called just after the ECS world owning this system has been deactivated.
Definition ecs_world.hpp:1027
virtual void onPreRenderStep(float simulationProgress)
Overridable callback called just before the render step takes place.
Definition ecs_world.hpp:1012
virtual void onSimulationPostStep(uint32_t simStepMillis)
An overridable callback called once at the end of this simulation step, and after related transform u...
Definition ecs_world.hpp:987
void updateComponent_(EntityID entityID, const TComponent &component)
The actual implementation of updateComponent for a system.
Definition ecs_world.hpp:2805
virtual ~BaseSystem()=default
Destroy the Base System object.
void removeEntity(EntityID entityID)
Removes an entity from this system.
Definition ecs_world.cpp:127
virtual void onVariableStep(float simulationProgress, uint32_t variableStepMillis)
Overridable callback called after all simulation updates (if any) for the current frame have been com...
Definition ecs_world.hpp:1005
void enableEntity(EntityID entityID)
Allows a registered entity to be influenced by this system.
Definition ecs_world.cpp:80
std::weak_ptr< ECSWorld > mWorld
A reference to the world this system belongs to.
Definition ecs_world.hpp:898
virtual void onEntityUpdated(EntityID entityID)
An overridable callback for when another system has updated a component shared by this system and an ...
Definition ecs_world.hpp:952
virtual void onEntityDisabled(EntityID entityID)
An overridable callback for when an entity has been disabled.
Definition ecs_world.hpp:945
void disableEntity(EntityID entityID)
Prevents the influencing of an entity by this system.
Definition ecs_world.cpp:99
BaseSystem(std::weak_ptr< ECSWorld > world)
Construct a new Base System object.
Definition ecs_world.hpp:821
virtual void onPostTransformUpdate(uint32_t timeStepMillis)
An overridable callback called after all the transforms in the scene are updated by the scene system.
Definition ecs_world.hpp:994
virtual std::shared_ptr< BaseSystem > instantiate(std::weak_ptr< ECSWorld > world)=0
Creates a fresh copy of this system and associates it with a new ECS World, using this system as its ...
bool isEnabled(EntityID entityID) const
Tests whether a particular entity is active for this system.
Definition ecs_world.cpp:136
virtual void onEntityEnabled(EntityID entityID)
An overridable callback for when an entity has been enabled.
Definition ecs_world.hpp:936
std::set< EntityID > mEnabledEntities
A set of all entities that are actively influenced by this system, managed by this system's ECS world...
Definition ecs_world.hpp:1041
virtual void onDestroyed()
Overridable callback called just before this system is destroyed.
Definition ecs_world.hpp:1035
virtual void onSimulationActivated()
An overridable callback for right after the ECS World has been activated.
Definition ecs_world.hpp:964
virtual void onSimulationStep(uint32_t simStepMillis)
An overridable callback called once in the middle of every simulation step.
Definition ecs_world.hpp:980
const std::set< EntityID > & getEnabledEntities()
Get a set of all entities that are influenced by this System.
Definition ecs_world.cpp:132
virtual void onPostRenderStep(float simulationProgress)
Overridable callback called just after the render step takes place.
Definition ecs_world.hpp:1019
virtual bool isSingleton() const
A method to query whether a particular System is a singleton, or is instantiated for each world in th...
Definition ecs_world.hpp:835
void addEntity(EntityID entityID, bool enabled=true)
Adds an entity to this system.
Definition ecs_world.cpp:111
std::set< EntityID > mDisabledEntities
A set of all entities that are compatible with this system, but have not been enabled for it.
Definition ecs_world.hpp:1047
virtual void onSimulationPreStep(uint32_t simStepMillis)
An overridable callback called once at the beginning of every simulation step in the game loop.
Definition ecs_world.hpp:971
TComponent getComponent_(EntityID entityID, float progress=1.f) const
The actual implementation of getComponent for a system.
Definition ecs_world.hpp:2789
bool isRegistered(EntityID entityID) const
Tests whether a particular entity can be influenced by this system.
Definition ecs_world.cpp:140
A class that implements BaseComponentArray specializing it for a component of type TComponent.
Definition ecs_world.hpp:366
std::shared_ptr< BaseComponentArray > instantiate(std::weak_ptr< ECSWorld > world) const override
Creates a new component array of the same type as this and associated with a new World.
Definition ecs_world.hpp:2800
std::unordered_map< std::size_t, EntityID > mComponentToEntity
A mapping from the index of a component to the ID of the entity that owns that component.
Definition ecs_world.hpp:500
void removeComponent(EntityID entityID) override
Removes the component associated with a specific entity, maintaining packing but not order.
Definition ecs_world.hpp:2379
virtual void handlePreSimulationStep() override
A callback for the start of a simulation step.
Definition ecs_world.hpp:2425
void addComponent(EntityID entityID, const TComponent &component)
Adds a component belonging to this entity to this array.
Definition ecs_world.hpp:2352
ComponentArray(std::weak_ptr< ECSWorld > world)
Construct a new Component Array object.
Definition ecs_world.hpp:373
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates the value of the component belonging to this entity.
Definition ecs_world.hpp:2411
std::vector< TComponent > mComponentsPrevious
An array containing the state of each entity's component as seen in the last simulation tick.
Definition ecs_world.hpp:486
bool hasComponent(EntityID entityID) const override
Tests whether an entry for a component belonging to this entity is present.
Definition ecs_world.hpp:2374
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component object.
Definition ecs_world.hpp:2403
std::unordered_map< EntityID, std::size_t > mEntityToComponentIndex
A mapping from the ID of an entity to the index in the component array that stores the entity's compo...
Definition ecs_world.hpp:493
virtual void handleEntityDestroyed(EntityID entityID) override
A callback to handle the side effect of the destruction of an entity. Here: deletion of the component...
Definition ecs_world.hpp:2418
virtual void copyComponent(EntityID to, EntityID from) override
Handles the copying of a component belonging to one entity to another.
Definition ecs_world.hpp:2433
std::vector< TComponent > mComponentsNext
An array containing the state of each entity's component as will be seen at the start of the next sim...
Definition ecs_world.hpp:480
An object that stores and manages updates to all the component arrays instantiated for this ECS World...
Definition ecs_world.hpp:511
void unregisterAll()
Unregisters all component arrays associated with this manager, as part of the destruction process for...
Definition ecs_world.cpp:393
Signature getSignature(EntityID entityID)
Get the component signature for a given entity.
Definition ecs_world.cpp:348
void copyComponent(EntityID to, EntityID from)
Copies a component value from one component to another within the same component array.
Definition ecs_world.hpp:2509
void handlePreSimulationStep()
Callback for the start of a simulation step, where the contents of every component's next member is c...
Definition ecs_world.cpp:478
std::unordered_map< EntityID, Signature > mEntityToSignature
Stores the component signature of each entity.
Definition ecs_world.hpp:798
void addComponent(EntityID entityID, const TComponent &component)
Adds a component entry for this entity in the array specified.
Definition ecs_world.hpp:2482
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component value for this entity.
Definition ecs_world.hpp:2499
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with its new value.
Definition ecs_world.hpp:2504
std::shared_ptr< BaseComponentArray > getComponentArray(const std::string &componentTypeName) const
Get the Component Array object.
Definition ecs_world.hpp:586
std::unordered_map< std::size_t, ComponentType > mHashToComponentType
Maps a component's type hash to its ComponentType.
Definition ecs_world.hpp:781
void copyComponents(EntityID to, EntityID from)
Copies all components from one entity and updates or adds them to the other.
Definition ecs_world.cpp:357
bool hasComponent(EntityID entityID) const
Tests whether an entity has a component of a particular type.
Definition ecs_world.hpp:2488
std::unordered_map< std::size_t, std::shared_ptr< BaseComponentArray > > mHashToComponentArray
Maps the hash of a component type to its corresponding ComponentArray.
Definition ecs_world.hpp:787
std::unordered_map< std::string, std::size_t > mNameToComponentHash
Maps each component type name to its corresponding hash.
Definition ecs_world.hpp:772
std::shared_ptr< ComponentArray< TComponent > > getComponentArray() const
Get the (specialized) Component Array object.
Definition ecs_world.hpp:574
void removeComponent(EntityID entityID)
Removes the component of the type specified from the entity.
Definition ecs_world.hpp:2493
ComponentManager(std::weak_ptr< ECSWorld > world)
Construct a new Component Manager object.
Definition ecs_world.hpp:518
std::weak_ptr< ECSWorld > mWorld
Holds a reference to the ECS world this component manager manages component arrays for.
Definition ecs_world.hpp:804
void handleEntityDestroyed(EntityID entityID)
Handles component arrays specific side effect of entity destruction.
Definition ecs_world.cpp:369
void registerComponentArray()
A method to allow a new component type to register itself with the ECS system for this project.
Definition ecs_world.hpp:2455
ComponentManager instantiate(std::weak_ptr< ECSWorld > world) const
Constructs a new component manager and associates it with the world passed in as argument....
Definition ecs_world.cpp:313
ComponentType getComponentType() const
Get the component type ID for a given component type.
Definition ecs_world.hpp:2475
A class that represents a set of systems, entities, and components, that are all interrelated,...
Definition ecs_world.hpp:1439
SystemType getSystemType()
Get the SystemType id for a particular system.
Definition ecs_world.hpp:2652
void cleanup()
Loses references to member systems, component arrays, and entities, resulting in their destruction.
Definition ecs_world.cpp:539
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for membership with a system, per its component signature.
Definition ecs_world.hpp:2527
void initialize()
Runs the initialization step for this world and the systems that belong to it.
Definition ecs_world.cpp:496
bool isEnabled(EntityID entityID)
Tests whether a particular entity is enabled for a particular system.
Definition ecs_world.hpp:2523
static Entity createEntityPrototype(TComponents...components)
Create a prototype entity object.
Definition ecs_world.hpp:2767
std::shared_ptr< ECSWorld > instantiate() const
Creates a new ECSWorld using the systems and component arrays present in this one as a template.
Definition ecs_world.cpp:449
void simulationPreStep(uint32_t simStepMillis)
Runs callbacks associated with the start of a simulation update.
Definition ecs_world.cpp:515
bool hasComponent(EntityID entityID) const
Tests whether an entity has a component of a specific type.
Definition ecs_world.hpp:2532
static void registerComponentTypes()
Registers ComponentArrays for each type of component present in the component type list,...
Definition ecs_world.hpp:2744
void enableEntity(EntityID entityID)
Enables an entity for a specific system.
Definition ecs_world.hpp:2672
EntityID mNextEntity
A new EntityID that has never been created before in this world.
Definition ecs_world.hpp:2069
ECSWorld()=default
Construct a new ECSWorld object.
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get a component associated with an entity.
Definition ecs_world.hpp:2682
static std::weak_ptr< const ECSWorld > getPrototype()
Get the prototype ECSWorld.
Definition ecs_world.cpp:433
static std::shared_ptr< TSingletonSystem > getSingletonSystem()
Get a system that has been marked as a singleton System.
Definition ecs_world.hpp:2782
void simulationPostStep(uint32_t simStepMillis)
Runs calbacks associated with the end of the simulation step.
Definition ecs_world.cpp:522
void simulationStep(uint32_t simStepMillis)
Runs callbacks associated with the simulation step.
Definition ecs_world.cpp:519
WorldID getID() const
Gets the ID associated with this world.
Definition ecs_world.hpp:1790
void destroyEntity(EntityID entityID)
Destroys an Entity with a specific ID.
Definition ecs_world.cpp:471
std::vector< EntityID > mDeletedIDs
A list of previously existing entity IDs that were since retired and are available to be used again.
Definition ecs_world.hpp:2061
void relocateEntity(Entity &entity)
Moves an entity from one ECSWorld to this one.
Definition ecs_world.cpp:61
void copyComponents(EntityID to, EntityID from)
Copies components from one entity to another within a single ECSWorld.
Definition ecs_world.cpp:485
void preRenderStep(float simulationProgress)
Runs callbacks associated with the start of the rendering step.
Definition ecs_world.cpp:532
std::unique_ptr< SystemManager > mSystemManager
A reference to this world SystemManager.
Definition ecs_world.hpp:2055
void activateSimulation()
Marks this world as an active one, calling the activation callbacks of systems that belong to it.
Definition ecs_world.cpp:499
void removeComponent(EntityID entityID)
Removes a component from an entity.
Definition ecs_world.hpp:2665
Entity privateCreateEntity(TComponents...components)
Creates a new entity and assigns it the components specified as arguments.
Definition ecs_world.hpp:2634
WorldID mID
The unique ID associated with this ECSWorld.
Definition ecs_world.hpp:2075
std::unique_ptr< ComponentManager > mComponentManager
A reference to this world's ComponentManager.
Definition ecs_world.hpp:2049
static std::shared_ptr< TSystem > getSystemPrototype()
Get the system object of a specific type belonging to the prototype ECSWorld.
Definition ecs_world.hpp:2777
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with a new value.
Definition ecs_world.hpp:2700
Entity createEntity(TComponents...components)
Creates a new entity that will exist in this ECSWorld.
Definition ecs_world.hpp:2762
void postRenderStep(float simulationProgress)
Runs callbacks associated with the end of the rendering step.
Definition ecs_world.cpp:535
void postTransformUpdate(uint32_t timeStepMillis)
Runs callbacks associated with the end of transform updates.
Definition ecs_world.cpp:526
std::shared_ptr< TSystem > getSystem()
Get the system object of a specific type belonging to this ECSWorld.
Definition ecs_world.hpp:2772
void disableEntity(EntityID entityID)
Disables an entity on a particular system.
Definition ecs_world.hpp:2677
void variableStep(float simulationProgress, uint32_t variableStepMillis)
Runs callbacks associated with the variable step of the game loop.
Definition ecs_world.cpp:529
void deactivateSimulation()
Marks ths world as an inactive one, and suspends operation for its member systems.
Definition ecs_world.cpp:502
static void registerSystem(SystemRegistrationArgs< TSystemDerived, std::tuple< TListenedForComponents... >, std::tuple< TRequiredComponents... > >)
Registers a System with the ECS System for this project.
static WorldID s_nextWorld
The ID the next ECSWorld to be instantiated will receive.
Definition ecs_world.hpp:2081
static std::weak_ptr< ECSWorld > getInstance()
Creates an instance of the prototype ECSWorld, and returns a reference to it.
Definition ecs_world.cpp:444
void addComponent(EntityID entityID, const TComponent &component)
Adds a new component to the entity.
Definition ecs_world.hpp:2657
The Entity is a wrapper on an entity ID, used as the primary interface between an application and the...
Definition ecs_world.hpp:2095
void addComponent(const TComponent &component)
Adds a new component to this Entity.
Definition ecs_world.hpp:2577
EntityID getID()
Gets the ID associated with this Entity.
Definition ecs_world.hpp:2137
std::weak_ptr< ECSWorld > mWorld
The world this Entity belongs to.
Definition ecs_world.hpp:2334
void updateComponent(const TComponent &newValue)
Updates the value of a component belonging to this Entity.
Definition ecs_world.hpp:2592
void disableSystem()
Disables this entity for a particular system.
Definition ecs_world.hpp:2617
EntityID mID
The ID of this entity within its owning ECSWorld.
Definition ecs_world.hpp:2328
Entity & operator=(const Entity &other)
Copies an Entity object, replacing any component values currently present on this one.
Definition ecs_world.cpp:31
bool hasComponent() const
Tests whether this entity has a particular component.
Definition ecs_world.hpp:2582
std::weak_ptr< ECSWorld > getWorld()
Get the ECSWorld object this Entity belongs to.
Definition ecs_world.hpp:2306
void enableSystem()
Enables this Entity for a particular System.
Definition ecs_world.hpp:2602
Entity(const Entity &other)
Construct a new Entity object.
Definition ecs_world.cpp:15
bool isRegistered() const
Tests whether this entity is eligible for participation with a given system.
Definition ecs_world.hpp:2612
Entity(EntityID entityID, std::shared_ptr< ECSWorld > world)
Construct a new Entity object, with a new ID as a member of a new ECSWorld.
Definition ecs_world.hpp:2322
bool isEnabled() const
Tests whether this entity is enabled for a particular system.
Definition ecs_world.hpp:2607
TComponent getComponent(float simulationProgress=1.f) const
Get the value of the component at a specific time this frame.
Definition ecs_world.hpp:2587
void removeComponent()
Removes a component from an entity.
Definition ecs_world.hpp:2597
A template class for interpolating components between simulation frames for various purposes.
Definition ecs_world.hpp:337
RangeMapperLinear mProgressLimits
a functor that performs the actual interpolation in the default case
Definition ecs_world.hpp:354
A simple linear interpolation implementation between a fixed input and output range.
Definition util.hpp:52
Helper class for registering a class at program startup.
Definition registrator.hpp:64
Holds references to all the systems belonging to this manager's ECSWorld.
Definition ecs_world.hpp:1148
void disableEntity(EntityID entityID)
Disables an entity for a particular system.
Definition ecs_world.hpp:2571
std::unordered_map< std::string, Signature > mNameToListenedForComponents
Mapping from the system type name for a system to its component listening signature.
Definition ecs_world.hpp:1395
std::weak_ptr< ECSWorld > mWorld
The world this System manager belongs to.
Definition ecs_world.hpp:1414
bool isEnabled(EntityID entityID)
Tests whether an entity is enabled for a particular system.
Definition ecs_world.hpp:2622
SystemManager(std::weak_ptr< ECSWorld > world)
Construct a new System Manager object.
Definition ecs_world.hpp:1155
SystemType getSystemType() const
Get the SystemType for a system.
Definition ecs_world.hpp:2516
std::unordered_map< std::string, std::shared_ptr< BaseSystem > > mNameToSystem
Maps system type names to references to the actual in-memory Systems they represent.
Definition ecs_world.hpp:1408
std::shared_ptr< TSystem > getSystem()
Gets an instance of a system belonging to this ECS World.
Definition ecs_world.hpp:2558
void enableEntity(EntityID entityID)
Enables the visibility of an entity to a particular system.
Definition ecs_world.hpp:2565
std::unordered_map< std::string, Signature > mNameToSignature
Mapping from the system type name for a system to its component signature.
Definition ecs_world.hpp:1387
std::unordered_map< std::string, SystemType > mNameToSystemType
Maps system type names to their corresponding SystemType values.
Definition ecs_world.hpp:1402
void handleEntityUpdatedBySystem(EntityID entityID, Signature signature, ComponentType updatedComponent)
Handles an update to an entity by running a callback on all interested systems (except the one that t...
Definition ecs_world.hpp:2811
void registerSystem(const Signature &signature, const Signature &listenedForComponents)
During static initialization, adds the new System type and its component signatures to its tables.
Definition ecs_world.hpp:2546
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for participation in a particular system.
Definition ecs_world.hpp:2628
void updateComponent(EntityID entityID, const TComponent &component)
Updates a component via this system's specialized version of updateComponent.
Definition ecs_world.hpp:1116
std::shared_ptr< BaseSystem > instantiate(std::weak_ptr< ECSWorld > world) override
Instantiates a fresh System using this one as a template, and associates it with a new world.
System(std::weak_ptr< ECSWorld > world)
Construct a new System object.
Definition ecs_world.hpp:1092
static void registerSelf()
Helper function called during static initialization to make this project's ECS system aware that this...
Definition ecs_world.hpp:2538
TComponent getComponent(EntityID entityID, float progress=1.f)
Gets the component belonging to the entity using this System's specialized version of getComponent.
Definition ecs_world.hpp:1103
static Registrator< System< TSystemDerived, std::tuple< TListenedForComponents... >, std::tuple< TRequiredComponents... > > > & s_registrator
A specialization of Registrator<T> which ensures registerSelf() is called during this project's stati...
Definition ecs_world.hpp:1134
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1062
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:103
T operator()(const T &previousState, const T &nextState, float simulationProgress=1.f) const
Returns an interpolated value for a component between two given states.
Definition ecs_world.hpp:2346
constexpr ComponentType kMaxComponents
A constant that restricts the number of definable components in a project.
Definition ecs_world.hpp:132
std::bitset< kMaxComponents > Signature
A 255 bit number, where each enabled bit represents a relationship between an entity and some ECS rel...
Definition ecs_world.hpp:154
constexpr SystemType kMaxSystems
A constant that restricts the number of definable systems in a project.
Definition ecs_world.hpp:139
ECSType SystemType
An unsigned integer representing the type of a system.
Definition ecs_world.hpp:112
std::uint8_t ECSType
A number tag used to represent components and systems.
Definition ecs_world.hpp:93
constexpr EntityID kMaxEntities
A user-set constant which limits the number of creatable entities in a single ECS system.
Definition ecs_world.hpp:119
std::pair< WorldID, EntityID > UniversalEntityID
An ID that uniquely identifies an entity.
Definition ecs_world.hpp:85
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:68
std::uint64_t WorldID
An unsigned integer representing the name of an ECS world.
Definition ecs_world.hpp:76
constexpr ECSType kMaxECSTypes
A constant used to restrict the number of definable system and component types in a project.
Definition ecs_world.hpp:125
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition camera_system.hpp:20
Contains the definition for the Registrator<T> utility class, used anywhere that automatic registrati...
A struct that describes how a JSON component description is turned into a component.
Definition ecs_world.hpp:289
static TComponent get(const nlohmann::json &jsonComponent)
Get method that automatically invokes a from_json function, found by nlohmann json.
Definition ecs_world.hpp:296
Helper function for retrieving the component type string defined as part of the component.
Definition ecs_world.hpp:543
std::string operator()()
The method that retrieves the component type string.
Definition ecs_world.hpp:549
Prevents the use of the unspecialized version of SystemRegistrationArgs.
Definition ecs_world.hpp:1529
Contains a couple of classes not tied to any part of the engine in particular, but useful to those pa...