14#ifndef TOYMAKERENGINE_ECSWORLD_H
15#define TOYMAKERENGINE_ECSWORLD_H
26#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> {
847 template<
typename TComponent>
873 template <
typename TComponent,
typename TSystem>
884 template <
typename TComponent,
typename TSystem>
911 virtual std::shared_ptr<BaseSystem>
instantiate(std::weak_ptr<ECSWorld> world) = 0;
975 (void)updatedComponentType;
976 assert(
false &&
"The base class version of onEntityUpdated should never be called");
1030 virtual void onVariableStep(
float simulationProgress, uint32_t variableStepMillis) {(void)simulationProgress; (void)variableStepMillis;}
1086 template <
typename TSystemDerived,
typename TListenedForComponentsTuple,
typename TRequiredComponentsTuple>
1087 class System{
static_assert(
false &&
"Non specialized system cannot be declared"); };
1099 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1100 class System<TSystemDerived,
std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>:
public BaseSystem {
1127 template<
typename TComponent>
1129 assert(!
isSingleton() &&
"Singletons cannot retrieve components by EntityID alone");
1140 template<
typename TComponent>
1142 assert(!
isSingleton() &&
"Singletons cannot retrieve components by EntityID alone");
1152 std::shared_ptr<BaseSystem>
instantiate(std::weak_ptr<ECSWorld> world)
override;
1160 Registrator<
System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>::getRegistrator()
1163 friend class Registrator<
System<TSystemDerived,
std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>;
1188 SystemManager instantiate(std::weak_ptr<ECSWorld> world)
const;
1199 template<
typename TSystem>
1200 void registerSystem(
const Signature& signature,
const Signature& listenedForComponents);
1206 void unregisterAll();
1214 template<
typename TSystem>
1215 std::shared_ptr<TSystem> getSystem();
1223 template<
typename TSystem>
1224 void enableEntity(
EntityID entityID);
1243 template<
typename TSystem>
1244 void disableEntity(EntityID entityID);
1252 void disableEntity(EntityID entityID, Signature entitySignature);
1262 template<
typename TSystem>
1263 SystemType getSystemType()
const;
1273 template<
typename TSystem>
1274 bool isEnabled(EntityID entityID);
1284 template <
typename TSystem>
1285 bool isRegistered(EntityID entityID);
1293 void handleEntitySignatureChanged(EntityID entityID, Signature signature);
1300 void handleEntityDestroyed(EntityID entityID);
1309 void handleEntityUpdated(EntityID entityID, Signature signature, ComponentType updatedComponent);
1320 template<
typename TSystem>
1321 void handleEntityUpdatedBySystem(EntityID entityID, Signature signature, ComponentType updatedComponent);
1328 void handleInitialize();
1335 void handleSimulationActivated();
1344 void handleSimulationPreStep(uint32_t simStepMillis);
1353 void handleSimulationStep(uint32_t simStepMillis);
1362 void handleSimulationPostStep(uint32_t simStepMillis);
1371 void handlePostTransformUpdate(uint32_t timeStepMillis);
1382 void handleVariableStep(
float simulationProgress, uint32_t variableStepMillis);
1391 void handlePreRenderStep(
float simulationProgress);
1398 void handlePostRenderStep(
float simulationProgress);
1404 void handleSimulationDeactivated();
1441 friend class ECSWorld;
1442 friend class BaseSystem;
1464 class ECSWorld:
public std::enable_shared_from_this<ECSWorld> {
1543 template<
typename ...TComponent>
1553 template <
typename TSystemDerived,
typename TListenedForComponents,
typename TRequiredComponents>
1566 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1615 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1624 template<
typename TSystem>
1635 template<
typename TSystem>
1648 template <
typename TSingletonSystem>
1661 template <
typename TSystem>
1670 template<
typename TComponent>
1681 template <
typename TSystem>
1692 template <
typename TSystem>
1704 template<
typename ...TComponents>
1718 template <
typename ...TComponents>
1791 void variableStep(
float simulationProgress, uint32_t variableStepMillis);
1833 static std::shared_ptr<ECSWorld> createWorld();
1840 static std::weak_ptr<ECSWorld> getInstance();
1879 template<
typename ...TComponents>
1899 template<
typename TSystem>
1920 template<
typename TSystem>
1921 void disableEntity(
EntityID entityID);
1929 void disableEntity(
EntityID entityID);
1940 template<
typename TComponent>
1941 void addComponent(
EntityID entityID,
const TComponent& component);
1951 void addComponent(
EntityID entityID,
const nlohmann::json& jsonComponent);
1964 template<
typename TComponent>
1965 TComponent getComponent(
EntityID entityID,
float simulationProgress=1.f)
const;
1977 template<
typename TComponent,
typename TSystem>
1978 TComponent getComponent(
EntityID entityID,
float simulationProgress=1.f)
const;
1990 template <
typename TComponent>
1991 bool hasComponent(
EntityID entityID)
const;
2004 bool hasComponent(
EntityID entityID,
const std::string& typeName)
const;
2015 template<
typename TComponent>
2016 void updateComponent(
EntityID entityID,
const TComponent& newValue);
2026 void updateComponent(
EntityID entityID,
const nlohmann::json& newValue);
2038 template<
typename TComponent,
typename TSystem>
2039 void updateComponent(
EntityID entityID,
const TComponent& newValue);
2050 template<
typename TSystem>
2051 void updateComponent(
EntityID entityID,
const nlohmann::json& newValue);
2061 template<
typename TComponent>
2062 void removeComponent(
EntityID entityID);
2070 void removeComponent(
EntityID entityID,
const std::string& typeName);
2077 void removeComponentsAll(
EntityID entityID);
2117 friend class Entity;
2118 friend class BaseSystem;
2180 void copy(
const Entity& other);
2190 template<
typename TComponent>
2191 void addComponent(
const TComponent& component);
2200 void addComponent(
const nlohmann::json& jsonComponent);
2209 template<
typename TComponent>
2210 void removeComponent();
2219 void removeComponent(
const std::string& typeName);
2231 template<
typename TComponent>
2232 bool hasComponent()
const;
2243 bool hasComponent(
const std::string& typeName)
const;
2254 template<
typename TComponent>
2255 TComponent getComponent(
float simulationProgress=1.f)
const;
2265 template<
typename TComponent>
2266 void updateComponent(
const TComponent& newValue);
2275 void updateComponent(
const nlohmann::json& jsonValue);
2286 template<
typename TSystem>
2287 bool isEnabled()
const;
2298 template <
typename TSystem>
2299 bool isRegistered()
const;
2308 template<
typename TSystem>
2309 void enableSystem();
2318 template<
typename TSystem>
2319 void disableSystem();
2326 void disableSystems();
2333 void enableSystems(
Signature systemMask);
2369 friend class ECSWorld;
2379 template<
typename T>
2381 if(simulationProgress < .5f)
return previousState;
2385 template<
typename TComponent>
2391 const TComponent componentCopy { component };
2398 template <
typename TComponent>
2403 template <
typename TComponent>
2408 template<
typename TComponent>
2413 template<
typename TComponent>
2437 template <
typename TComponent>
2445 template <
typename TComponent>
2452 template <
typename TComponent>
2459 template<
typename TComponent>
2461 std::copy<typename std::vector<TComponent>::iterator,
typename std::vector<TComponent>::iterator>(
2467 template <
typename TComponent>
2472 template <
typename TComponent>
2474 assert(to <
kMaxEntities &&
"Cannot copy to an entity with an invalid entity ID");
2489 template<
typename TComponent>
2491 const std::size_t componentHash {
typeid(TComponent).hash_code() };
2500 has already been registered");
2509 template<
typename TComponent>
2511 const std::size_t componentHash {
typeid(TComponent).hash_code() };
2516 template<
typename TComponent>
2522 template <
typename TComponent>
2527 template<
typename TComponent>
2533 template<
typename TComponent>
2538 template<
typename TComponent>
2543 template <
typename TComponent>
2550 template<
typename TSystem>
2552 const std::string systemTypeName{ TSystem::getSystemTypeName() };
2557 template <
typename TSystem>
2561 template <
typename TSystem>
2566 template <
typename TComponent>
2572 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2573 void System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>
::registerSelf() {
2580 template<
typename TSystem>
2582 const std::string systemTypeName { TSystem::getSystemTypeName() };
2592 template<
typename TSystem>
2594 std::string systemTypeName { TSystem::getSystemTypeName() };
2596 return std::dynamic_pointer_cast<TSystem>(
mNameToSystem[systemTypeName]);
2599 template<
typename TSystem>
2601 std::string systemTypeName { TSystem::getSystemTypeName() };
2605 template<
typename TSystem>
2607 std::string systemTypeName { TSystem::getSystemTypeName() };
2611 template <
typename TComponent>
2613 mWorld.lock()->addComponent<TComponent>(
mID, component);
2616 template <
typename TComponent>
2618 return mWorld.lock()->hasComponent<TComponent>(
mID);
2621 template<
typename TComponent>
2623 return mWorld.lock()->getComponent<TComponent>(
mID, simulationProgress);
2626 template<
typename TComponent>
2628 mWorld.lock()->updateComponent<TComponent>(
mID, newValue);
2631 template <
typename TComponent>
2633 mWorld.lock()->removeComponent<TComponent>(
mID);
2636 template <
typename TSystem>
2638 mWorld.lock()->enableEntity<TSystem>(
mID);
2641 template <
typename TSystem>
2643 return mWorld.lock()->isEnabled<TSystem>(
mID);
2646 template <
typename TSystem>
2648 return mWorld.lock()->isRegistered<TSystem>(
mID);
2651 template <
typename TSystem>
2653 mWorld.lock()->disableEntity<TSystem>(
mID);
2656 template <
typename TSystem>
2658 const std::string systemTypeName { TSystem::getSystemTypeName() };
2662 template <
typename TSystem>
2664 const std::string systemTypeName { TSystem::getSystemTypeName() };
2665 return mNameToSystem[systemTypeName]->isRegistered(entityID);
2668 template<
typename ...TComponents>
2680 Entity entity { nextID, shared_from_this()};
2686 template<
typename TSystem>
2691 template<
typename TComponent>
2696 template<
typename TComponent>
2698 assert(entityID <
kMaxEntities &&
"Cannot add a component to an entity that does not exist");
2701 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2704 template<
typename TComponent>
2708 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2711 template<
typename TSystem>
2716 template<
typename TSystem>
2721 template<
typename TComponent>
2726 template<
typename TComponent,
typename TSystem>
2730 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2733 &&
"This system cannot access this kind of component"
2739 template<
typename TComponent>
2749 template<
typename TComponent,
typename TSystem>
2753 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2756 &&
"This system cannot access this kind of component"
2766 template <
typename TSystem>
2770 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2773 &&
"This system cannot access this kind of component"
2783 template<
typename ...TComponents>
2785 ((
getInstance().lock()->mComponentManager->registerComponentArray<TComponents>()),...);
2788 template<
typename TSystem,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2795 (listensFor.set(getInstance().lock()->mComponentManager->getComponentType<TListenedForComponents>()), ...);
2796 (required.set(getInstance().lock()->mComponentManager->getComponentType<TRequiredComponents>()), ...);
2798 getInstance().lock()->mSystemManager->registerSystem<TSystem>(required|listensFor, listensFor);
2801 template<
typename ...TComponents>
2806 template <
typename ...TComponents>
2811 template<
typename TSystem>
2816 template<
typename TSystem>
2818 return getInstance().lock()->mSystemManager->getSystem<TSystem>();
2821 template <
typename TSingletonSystem>
2823 std::shared_ptr<TSingletonSystem> system {
getInstance().lock()->getSystem<TSingletonSystem>() };
2824 assert(system->isSingleton() &&
"System specified is not an ECSWorld-aware singleton system");
2828 template<
typename TComponent>
2830 return mWorld.lock()->getComponentType<TComponent>();
2833 template<
typename TComponent,
typename TSystem>
2835 assert(!
isSingleton() &&
"Singletons cannot retrieve entity components through entity ID alone");
2836 return mWorld.lock()->getComponent<TComponent, TSystem>(entityID, progress);
2838 template<
typename TSystem,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2839 std::shared_ptr<BaseSystem>
System<TSystem, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>::instantiate(std::weak_ptr<ECSWorld> world) {
2840 if(isSingleton())
return shared_from_this();
2841 return std::make_shared<TSystem>(world);
2844 template <
typename TComponent>
2846 return std::make_shared<ComponentArray<TComponent>>(world);
2849 template<
typename TComponent,
typename TSystem>
2851 assert(!
isSingleton() &&
"Singletons cannot retrieve entity components through entity ID alone");
2852 mWorld.lock()->updateComponent<TComponent, TSystem>(entityID, component);
2855 template<
typename TSystem>
2857 std::string originatingSystemTypeName { TSystem::getSystemTypeName() };
2860 if((pair.second&signature) != pair.second)
continue;
2863 if(pair.first == originatingSystemTypeName)
continue;
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:983
virtual void onSimulationDeactivated()
Overridable callback called just after the ECS world owning this system has been deactivated.
Definition ecs_world.hpp:1052
virtual void onPreRenderStep(float simulationProgress)
Overridable callback called just before the render step takes place.
Definition ecs_world.hpp:1037
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:1012
virtual void onEntityUpdated(EntityID entityID, ComponentType updatedComponentType)
An overridable callback for when another system has updated a component shared by this system and an ...
Definition ecs_world.hpp:972
void updateComponent_(EntityID entityID, const TComponent &component)
The actual implementation of updateComponent for a system.
Definition ecs_world.hpp:2850
virtual ~BaseSystem()=default
Destroy the Base System object.
void removeEntity(EntityID entityID)
Removes an entity from this system.
Definition ecs_world.cpp:128
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:1030
void enableEntity(EntityID entityID)
Allows a registered entity to be influenced by this system.
Definition ecs_world.cpp:81
std::weak_ptr< ECSWorld > mWorld
A reference to the world this system belongs to.
Definition ecs_world.hpp:917
virtual void onEntityDisabled(EntityID entityID)
An overridable callback for when an entity has been disabled.
Definition ecs_world.hpp:964
void disableEntity(EntityID entityID)
Prevents the influencing of an entity by this system.
Definition ecs_world.cpp:100
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:1019
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:141
virtual void onEntityEnabled(EntityID entityID)
An overridable callback for when an entity has been enabled.
Definition ecs_world.hpp:955
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:1066
virtual void onDestroyed()
Overridable callback called just before this system is destroyed.
Definition ecs_world.hpp:1060
virtual void onSimulationActivated()
An overridable callback for right after the ECS World has been activated.
Definition ecs_world.hpp:989
virtual void onSimulationStep(uint32_t simStepMillis)
An overridable callback called once in the middle of every simulation step.
Definition ecs_world.hpp:1005
const std::set< EntityID > & getEnabledEntities()
Get a set of all entities that are influenced by this System.
Definition ecs_world.cpp:133
virtual void onPostRenderStep(float simulationProgress)
Overridable callback called just after the render step takes place.
Definition ecs_world.hpp:1044
ComponentType getComponentType() const
Get the component type ID for a given component type.
Definition ecs_world.hpp:2829
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:112
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:1072
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:996
TComponent getComponent_(EntityID entityID, float progress=1.f) const
The actual implementation of getComponent for a system.
Definition ecs_world.hpp:2834
bool isRegistered(EntityID entityID) const
Tests whether a particular entity can be influenced by this system.
Definition ecs_world.cpp:145
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:2845
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:2414
virtual void handlePreSimulationStep() override
A callback for the start of a simulation step.
Definition ecs_world.hpp:2460
void addComponent(EntityID entityID, const TComponent &component)
Adds a component belonging to this entity to this array.
Definition ecs_world.hpp:2386
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:2446
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:2409
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component object.
Definition ecs_world.hpp:2438
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:2453
virtual void copyComponent(EntityID to, EntityID from) override
Handles the copying of a component belonging to one entity to another.
Definition ecs_world.hpp:2468
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:435
Signature getSignature(EntityID entityID)
Get the component signature for a given entity.
Definition ecs_world.cpp:390
void copyComponent(EntityID to, EntityID from)
Copies a component value from one component to another within the same component array.
Definition ecs_world.hpp:2544
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:520
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:2517
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component value for this entity.
Definition ecs_world.hpp:2534
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with its new value.
Definition ecs_world.hpp:2539
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:399
bool hasComponent(EntityID entityID) const
Tests whether an entity has a component of a particular type.
Definition ecs_world.hpp:2523
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:2528
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:411
void registerComponentArray()
A method to allow a new component type to register itself with the ECS system for this project.
Definition ecs_world.hpp:2490
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:355
ComponentType getComponentType() const
Get the component type ID for a given component type.
Definition ecs_world.hpp:2510
A class that represents a set of systems, entities, and components, that are all interrelated,...
Definition ecs_world.hpp:1464
SystemType getSystemType()
Get the SystemType id for a particular system.
Definition ecs_world.hpp:2687
void cleanup()
Loses references to member systems, component arrays, and entities, resulting in their destruction.
Definition ecs_world.cpp:581
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for membership with a system, per its component signature.
Definition ecs_world.hpp:2562
void initialize()
Runs the initialization step for this world and the systems that belong to it.
Definition ecs_world.cpp:538
bool isEnabled(EntityID entityID)
Tests whether a particular entity is enabled for a particular system.
Definition ecs_world.hpp:2558
static Entity createEntityPrototype(TComponents...components)
Create a prototype entity object.
Definition ecs_world.hpp:2807
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:491
void simulationPreStep(uint32_t simStepMillis)
Runs callbacks associated with the start of a simulation update.
Definition ecs_world.cpp:557
bool hasComponent(EntityID entityID) const
Tests whether an entity has a component of a specific type.
Definition ecs_world.hpp:2567
static void registerComponentTypes()
Registers ComponentArrays for each type of component present in the component type list,...
Definition ecs_world.hpp:2784
void enableEntity(EntityID entityID)
Enables an entity for a specific system.
Definition ecs_world.hpp:2712
EntityID mNextEntity
A new EntityID that has never been created before in this world.
Definition ecs_world.hpp:2103
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:2722
static std::weak_ptr< const ECSWorld > getPrototype()
Get the prototype ECSWorld.
Definition ecs_world.cpp:475
static std::shared_ptr< TSingletonSystem > getSingletonSystem()
Get a system that has been marked as a singleton System.
Definition ecs_world.hpp:2822
void simulationPostStep(uint32_t simStepMillis)
Runs calbacks associated with the end of the simulation step.
Definition ecs_world.cpp:564
void simulationStep(uint32_t simStepMillis)
Runs callbacks associated with the simulation step.
Definition ecs_world.cpp:561
WorldID getID() const
Gets the ID associated with this world.
Definition ecs_world.hpp:1824
void destroyEntity(EntityID entityID)
Destroys an Entity with a specific ID.
Definition ecs_world.cpp:513
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:2095
void relocateEntity(Entity &entity)
Moves an entity from one ECSWorld to this one.
Definition ecs_world.cpp:62
void copyComponents(EntityID to, EntityID from)
Copies components from one entity to another within a single ECSWorld.
Definition ecs_world.cpp:527
ComponentType getComponentType() const
Get the component type ID for a given component type.
Definition ecs_world.hpp:2692
void preRenderStep(float simulationProgress)
Runs callbacks associated with the start of the rendering step.
Definition ecs_world.cpp:574
std::unique_ptr< SystemManager > mSystemManager
A reference to this world SystemManager.
Definition ecs_world.hpp:2089
void activateSimulation()
Marks this world as an active one, calling the activation callbacks of systems that belong to it.
Definition ecs_world.cpp:541
void removeComponent(EntityID entityID)
Removes a component from an entity.
Definition ecs_world.hpp:2705
Entity privateCreateEntity(TComponents...components)
Creates a new entity and assigns it the components specified as arguments.
Definition ecs_world.hpp:2669
WorldID mID
The unique ID associated with this ECSWorld.
Definition ecs_world.hpp:2109
std::unique_ptr< ComponentManager > mComponentManager
A reference to this world's ComponentManager.
Definition ecs_world.hpp:2083
static std::shared_ptr< TSystem > getSystemPrototype()
Get the system object of a specific type belonging to the prototype ECSWorld.
Definition ecs_world.hpp:2817
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with a new value.
Definition ecs_world.hpp:2740
Entity createEntity(TComponents...components)
Creates a new entity that will exist in this ECSWorld.
Definition ecs_world.hpp:2802
void postRenderStep(float simulationProgress)
Runs callbacks associated with the end of the rendering step.
Definition ecs_world.cpp:577
void postTransformUpdate(uint32_t timeStepMillis)
Runs callbacks associated with the end of transform updates.
Definition ecs_world.cpp:568
std::shared_ptr< TSystem > getSystem()
Get the system object of a specific type belonging to this ECSWorld.
Definition ecs_world.hpp:2812
void disableEntity(EntityID entityID)
Disables an entity on a particular system.
Definition ecs_world.hpp:2717
void variableStep(float simulationProgress, uint32_t variableStepMillis)
Runs callbacks associated with the variable step of the game loop.
Definition ecs_world.cpp:571
void deactivateSimulation()
Marks ths world as an inactive one, and suspends operation for its member systems.
Definition ecs_world.cpp:544
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:2115
static std::weak_ptr< ECSWorld > getInstance()
Creates an instance of the prototype ECSWorld, and returns a reference to it.
Definition ecs_world.cpp:486
void addComponent(EntityID entityID, const TComponent &component)
Adds a new component to the entity.
Definition ecs_world.hpp:2697
The Entity is a wrapper on an entity ID, used as the primary interface between an application and the...
Definition ecs_world.hpp:2129
void addComponent(const TComponent &component)
Adds a new component to this Entity.
Definition ecs_world.hpp:2612
EntityID getID()
Gets the ID associated with this Entity.
Definition ecs_world.hpp:2171
std::weak_ptr< ECSWorld > mWorld
The world this Entity belongs to.
Definition ecs_world.hpp:2368
void updateComponent(const TComponent &newValue)
Updates the value of a component belonging to this Entity.
Definition ecs_world.hpp:2627
void disableSystem()
Disables this entity for a particular system.
Definition ecs_world.hpp:2652
EntityID mID
The ID of this entity within its owning ECSWorld.
Definition ecs_world.hpp:2362
Entity & operator=(const Entity &other)
Copies an Entity object, replacing any component values currently present on this one.
Definition ecs_world.cpp:32
bool hasComponent() const
Tests whether this entity has a particular component.
Definition ecs_world.hpp:2617
std::weak_ptr< ECSWorld > getWorld()
Get the ECSWorld object this Entity belongs to.
Definition ecs_world.hpp:2340
void enableSystem()
Enables this Entity for a particular System.
Definition ecs_world.hpp:2637
Entity(const Entity &other)
Construct a new Entity object.
Definition ecs_world.cpp:16
bool isRegistered() const
Tests whether this entity is eligible for participation with a given system.
Definition ecs_world.hpp:2647
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:2356
bool isEnabled() const
Tests whether this entity is enabled for a particular system.
Definition ecs_world.hpp:2642
TComponent getComponent(float simulationProgress=1.f) const
Get the value of the component at a specific time this frame.
Definition ecs_world.hpp:2622
void removeComponent()
Removes a component from an entity.
Definition ecs_world.hpp:2632
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:213
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:1173
void disableEntity(EntityID entityID)
Disables an entity for a particular system.
Definition ecs_world.hpp:2606
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:1420
std::weak_ptr< ECSWorld > mWorld
The world this System manager belongs to.
Definition ecs_world.hpp:1439
bool isEnabled(EntityID entityID)
Tests whether an entity is enabled for a particular system.
Definition ecs_world.hpp:2657
SystemManager(std::weak_ptr< ECSWorld > world)
Construct a new System Manager object.
Definition ecs_world.hpp:1180
SystemType getSystemType() const
Get the SystemType for a system.
Definition ecs_world.hpp:2551
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:1433
std::shared_ptr< TSystem > getSystem()
Gets an instance of a system belonging to this ECS World.
Definition ecs_world.hpp:2593
void enableEntity(EntityID entityID)
Enables the visibility of an entity to a particular system.
Definition ecs_world.hpp:2600
std::unordered_map< std::string, Signature > mNameToSignature
Mapping from the system type name for a system to its component signature.
Definition ecs_world.hpp:1412
std::unordered_map< std::string, SystemType > mNameToSystemType
Maps system type names to their corresponding SystemType values.
Definition ecs_world.hpp:1427
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:2856
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:2581
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for participation in a particular system.
Definition ecs_world.hpp:2663
void updateComponent(EntityID entityID, const TComponent &component)
Updates a component via this system's specialized version of updateComponent.
Definition ecs_world.hpp:1141
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:1117
static void registerSelf()
Helper function called during static initialization to make this project's ECS system aware that this...
Definition ecs_world.hpp:2573
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:1128
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:1159
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1087
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:2380
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 application.hpp:25
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:1554
Contains a couple of classes not tied to any part of the engine in particular, but useful to those pa...