14#ifndef TOYMAKERENGINE_ECSWORLD_H
15#define TOYMAKERENGINE_ECSWORLD_H
24#include <unordered_map>
27#include <nlohmann/json.hpp>
252 virtual std::shared_ptr<BaseComponentArray>
instantiate(std::weak_ptr<ECSWorld> world)
const = 0;
286 template <
typename TComponent,
typename Enable=
void>
294 static TComponent
get(
const nlohmann::json& jsonComponent){
297 TComponent component = jsonComponent;
312 template <
typename TComponent,
typename Enable>
314 static std::shared_ptr<TComponent>
get(
const nlohmann::json& jsonComponent) {
317 std::shared_ptr<TComponent> component {
new TComponent{} = jsonComponent };
345 T
operator() (
const T& previousState,
const T& nextState,
float simulationProgress=1.f)
const;
363 template<
typename TComponent>
381 std::shared_ptr<BaseComponentArray>
instantiate(std::weak_ptr<ECSWorld> world)
const override;
532 template<
typename TComponent>
540 template <
typename TComponent>
548 return TComponent::getComponentTypeName();
558 template <
typename TComponent>
561 return TComponent::getComponentTypeName();
571 template<
typename TComponent>
573 const std::size_t componentHash {
typeid(TComponent).hash_code() };
575 return std::dynamic_pointer_cast<ComponentArray<TComponent>>(
mHashToComponentArray.at(componentHash));
584 std::shared_ptr<BaseComponentArray>
getComponentArray(
const std::string& componentTypeName)
const {
595 template<
typename TComponent>
630 template<
typename TComponent>
647 template<
typename TComponent>
668 template<
typename TComponent>
691 template<
typename TComponent>
703 template<
typename TComponent>
723 template<
typename TComponent>
804 friend class ECSWorld;
812 class BaseSystem :
public std::enable_shared_from_this<BaseSystem> {
845 template<
typename TComponent>
871 template <
typename TComponent,
typename TSystem>
882 template <
typename TComponent,
typename TSystem>
909 virtual std::shared_ptr<BaseSystem>
instantiate(std::weak_ptr<ECSWorld> world) = 0;
973 (void)updatedComponentType;
974 assert(
false &&
"The base class version of onEntityUpdated should never be called");
1028 virtual void onVariableStep(
float simulationProgress, uint32_t variableStepMillis) {(void)simulationProgress; (void)variableStepMillis;}
1084 template <
typename TSystemDerived,
typename TListenedForComponentsTuple,
typename TRequiredComponentsTuple>
1085 class System{
static_assert(
false &&
"Non specialized system cannot be declared"); };
1097 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1098 class System<TSystemDerived,
std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>:
public BaseSystem {
1125 template<
typename TComponent>
1127 assert(!
isSingleton() &&
"Singletons cannot retrieve components by EntityID alone");
1138 template<
typename TComponent>
1140 assert(!
isSingleton() &&
"Singletons cannot retrieve components by EntityID alone");
1150 std::shared_ptr<BaseSystem>
instantiate(std::weak_ptr<ECSWorld> world)
override;
1158 Registrator<
System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>::getRegistrator()
1161 friend class Registrator<
System<TSystemDerived,
std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>>;
1186 SystemManager instantiate(std::weak_ptr<ECSWorld> world)
const;
1197 template<
typename TSystem>
1198 void registerSystem(
const Signature& signature,
const Signature& listenedForComponents);
1204 void unregisterAll();
1212 template<
typename TSystem>
1213 std::shared_ptr<TSystem> getSystem();
1221 template<
typename TSystem>
1222 void enableEntity(
EntityID entityID);
1241 template<
typename TSystem>
1242 void disableEntity(EntityID entityID);
1250 void disableEntity(EntityID entityID, Signature entitySignature);
1260 template<
typename TSystem>
1261 SystemType getSystemType()
const;
1271 template<
typename TSystem>
1272 bool isEnabled(EntityID entityID);
1282 template <
typename TSystem>
1283 bool isRegistered(EntityID entityID);
1291 void handleEntitySignatureChanged(EntityID entityID, Signature signature);
1298 void handleEntityDestroyed(EntityID entityID);
1307 void handleEntityUpdated(EntityID entityID, Signature signature, ComponentType updatedComponent);
1318 template<
typename TSystem>
1319 void handleEntityUpdatedBySystem(EntityID entityID, Signature signature, ComponentType updatedComponent);
1326 void handleInitialize();
1333 void handleSimulationActivated();
1342 void handleSimulationPreStep(uint32_t simStepMillis);
1351 void handleSimulationStep(uint32_t simStepMillis);
1360 void handleSimulationPostStep(uint32_t simStepMillis);
1369 void handlePostTransformUpdate(uint32_t timeStepMillis);
1380 void handleVariableStep(
float simulationProgress, uint32_t variableStepMillis);
1389 void handlePreRenderStep(
float simulationProgress);
1396 void handlePostRenderStep(
float simulationProgress);
1402 void handleSimulationDeactivated();
1439 friend class ECSWorld;
1440 friend class BaseSystem;
1462 class ECSWorld:
public std::enable_shared_from_this<ECSWorld> {
1541 template<
typename ...TComponent>
1551 template <
typename TSystemDerived,
typename TListenedForComponents,
typename TRequiredComponents>
1564 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1613 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
1622 template<
typename TSystem>
1633 template<
typename TSystem>
1646 template <
typename TSingletonSystem>
1659 template <
typename TSystem>
1668 template<
typename TComponent>
1679 template <
typename TSystem>
1690 template <
typename TSystem>
1702 template<
typename ...TComponents>
1716 template <
typename ...TComponents>
1789 void variableStep(
float simulationProgress, uint32_t variableStepMillis);
1831 static std::shared_ptr<ECSWorld> createWorld();
1838 static std::weak_ptr<ECSWorld> getInstance();
1877 template<
typename ...TComponents>
1897 template<
typename TSystem>
1918 template<
typename TSystem>
1919 void disableEntity(
EntityID entityID);
1927 void disableEntity(
EntityID entityID);
1938 template<
typename TComponent>
1939 void addComponent(
EntityID entityID,
const TComponent& component);
1949 void addComponent(
EntityID entityID,
const nlohmann::json& jsonComponent);
1962 template<
typename TComponent>
1963 TComponent getComponent(
EntityID entityID,
float simulationProgress=1.f)
const;
1975 template<
typename TComponent,
typename TSystem>
1976 TComponent getComponent(
EntityID entityID,
float simulationProgress=1.f)
const;
1988 template <
typename TComponent>
1989 bool hasComponent(
EntityID entityID)
const;
2002 bool hasComponent(
EntityID entityID,
const std::string& typeName)
const;
2013 template<
typename TComponent>
2014 void updateComponent(
EntityID entityID,
const TComponent& newValue);
2024 void updateComponent(
EntityID entityID,
const nlohmann::json& newValue);
2036 template<
typename TComponent,
typename TSystem>
2037 void updateComponent(
EntityID entityID,
const TComponent& newValue);
2048 template<
typename TSystem>
2049 void updateComponent(
EntityID entityID,
const nlohmann::json& newValue);
2059 template<
typename TComponent>
2060 void removeComponent(
EntityID entityID);
2068 void removeComponent(
EntityID entityID,
const std::string& typeName);
2075 void removeComponentsAll(
EntityID entityID);
2115 friend class Entity;
2116 friend class BaseSystem;
2178 void copy(
const Entity& other);
2188 template<
typename TComponent>
2189 void addComponent(
const TComponent& component);
2198 void addComponent(
const nlohmann::json& jsonComponent);
2207 template<
typename TComponent>
2208 void removeComponent();
2217 void removeComponent(
const std::string& typeName);
2229 template<
typename TComponent>
2230 bool hasComponent()
const;
2241 bool hasComponent(
const std::string& typeName)
const;
2252 template<
typename TComponent>
2253 TComponent getComponent(
float simulationProgress=1.f)
const;
2263 template<
typename TComponent>
2264 void updateComponent(
const TComponent& newValue);
2273 void updateComponent(
const nlohmann::json& jsonValue);
2284 template<
typename TSystem>
2285 bool isEnabled()
const;
2296 template <
typename TSystem>
2297 bool isRegistered()
const;
2306 template<
typename TSystem>
2307 void enableSystem();
2316 template<
typename TSystem>
2317 void disableSystem();
2324 void disableSystems();
2331 void enableSystems(
Signature systemMask);
2367 friend class ECSWorld;
2377 template<
typename T>
2379 if(simulationProgress < .5f)
return previousState;
2383 template<
typename TComponent>
2389 const TComponent componentCopy { component };
2396 template <
typename TComponent>
2401 template <
typename TComponent>
2406 template<
typename TComponent>
2411 template<
typename TComponent>
2435 template <
typename TComponent>
2443 template <
typename TComponent>
2450 template <
typename TComponent>
2457 template<
typename TComponent>
2459 std::copy<typename std::vector<TComponent>::iterator,
typename std::vector<TComponent>::iterator>(
2465 template <
typename TComponent>
2470 template <
typename TComponent>
2472 assert(to <
kMaxEntities &&
"Cannot copy to an entity with an invalid entity ID");
2487 template<
typename TComponent>
2489 const std::size_t componentHash {
typeid(TComponent).hash_code() };
2498 has already been registered");
2507 template<
typename TComponent>
2509 const std::size_t componentHash {
typeid(TComponent).hash_code() };
2514 template<
typename TComponent>
2520 template <
typename TComponent>
2525 template<
typename TComponent>
2531 template<
typename TComponent>
2536 template<
typename TComponent>
2541 template <
typename TComponent>
2548 template<
typename TSystem>
2550 const std::string systemTypeName{ TSystem::getSystemTypeName() };
2555 template <
typename TSystem>
2559 template <
typename TSystem>
2564 template <
typename TComponent>
2570 template <
typename TSystemDerived,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2571 void System<TSystemDerived, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>
::registerSelf() {
2578 template<
typename TSystem>
2580 const std::string systemTypeName { TSystem::getSystemTypeName() };
2590 template<
typename TSystem>
2592 std::string systemTypeName { TSystem::getSystemTypeName() };
2594 return std::dynamic_pointer_cast<TSystem>(
mNameToSystem[systemTypeName]);
2597 template<
typename TSystem>
2599 std::string systemTypeName { TSystem::getSystemTypeName() };
2603 template<
typename TSystem>
2605 std::string systemTypeName { TSystem::getSystemTypeName() };
2609 template <
typename TComponent>
2611 mWorld.lock()->addComponent<TComponent>(
mID, component);
2614 template <
typename TComponent>
2616 return mWorld.lock()->hasComponent<TComponent>(
mID);
2619 template<
typename TComponent>
2621 return mWorld.lock()->getComponent<TComponent>(
mID, simulationProgress);
2624 template<
typename TComponent>
2626 mWorld.lock()->updateComponent<TComponent>(
mID, newValue);
2629 template <
typename TComponent>
2631 mWorld.lock()->removeComponent<TComponent>(
mID);
2634 template <
typename TSystem>
2636 mWorld.lock()->enableEntity<TSystem>(
mID);
2639 template <
typename TSystem>
2641 return mWorld.lock()->isEnabled<TSystem>(
mID);
2644 template <
typename TSystem>
2646 return mWorld.lock()->isRegistered<TSystem>(
mID);
2649 template <
typename TSystem>
2651 mWorld.lock()->disableEntity<TSystem>(
mID);
2654 template <
typename TSystem>
2656 const std::string systemTypeName { TSystem::getSystemTypeName() };
2660 template <
typename TSystem>
2662 const std::string systemTypeName { TSystem::getSystemTypeName() };
2663 return mNameToSystem[systemTypeName]->isRegistered(entityID);
2666 template<
typename ...TComponents>
2678 Entity entity { nextID, shared_from_this()};
2684 template<
typename TSystem>
2689 template<
typename TComponent>
2694 template<
typename TComponent>
2696 assert(entityID <
kMaxEntities &&
"Cannot add a component to an entity that does not exist");
2699 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2702 template<
typename TComponent>
2706 mSystemManager->handleEntitySignatureChanged(entityID, signature);
2709 template<
typename TSystem>
2714 template<
typename TSystem>
2719 template<
typename TComponent>
2724 template<
typename TComponent,
typename TSystem>
2728 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2731 &&
"This system cannot access this kind of component"
2737 template<
typename TComponent>
2747 template<
typename TComponent,
typename TSystem>
2751 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2754 &&
"This system cannot access this kind of component"
2764 template <
typename TSystem>
2768 mSystemManager->mNameToSignature.at(TSystem::getSystemTypeName())
2771 &&
"This system cannot access this kind of component"
2781 template<
typename ...TComponents>
2783 ((
getInstance().lock()->mComponentManager->registerComponentArray<TComponents>()),...);
2786 template<
typename TSystem,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2793 (listensFor.set(getInstance().lock()->mComponentManager->getComponentType<TListenedForComponents>()), ...);
2794 (required.set(getInstance().lock()->mComponentManager->getComponentType<TRequiredComponents>()), ...);
2796 getInstance().lock()->mSystemManager->registerSystem<TSystem>(required|listensFor, listensFor);
2799 template<
typename ...TComponents>
2804 template <
typename ...TComponents>
2809 template<
typename TSystem>
2814 template<
typename TSystem>
2816 return getInstance().lock()->mSystemManager->getSystem<TSystem>();
2819 template <
typename TSingletonSystem>
2821 std::shared_ptr<TSingletonSystem> system {
getInstance().lock()->getSystem<TSingletonSystem>() };
2822 assert(system->isSingleton() &&
"System specified is not an ECSWorld-aware singleton system");
2826 template<
typename TComponent>
2828 return mWorld.lock()->getComponentType<TComponent>();
2831 template<
typename TComponent,
typename TSystem>
2833 assert(!
isSingleton() &&
"Singletons cannot retrieve entity components through entity ID alone");
2834 return mWorld.lock()->getComponent<TComponent, TSystem>(entityID, progress);
2836 template<
typename TSystem,
typename ...TListenedForComponents,
typename ...TRequiredComponents>
2837 std::shared_ptr<BaseSystem>
System<TSystem, std::tuple<TListenedForComponents...>, std::tuple<TRequiredComponents...>>::instantiate(std::weak_ptr<ECSWorld> world) {
2838 if(isSingleton())
return shared_from_this();
2839 return std::make_shared<TSystem>(world);
2842 template <
typename TComponent>
2844 return std::make_shared<ComponentArray<TComponent>>(world);
2847 template<
typename TComponent,
typename TSystem>
2849 assert(!
isSingleton() &&
"Singletons cannot retrieve entity components through entity ID alone");
2850 mWorld.lock()->updateComponent<TComponent, TSystem>(entityID, component);
2853 template<
typename TSystem>
2855 std::string originatingSystemTypeName { TSystem::getSystemTypeName() };
2858 if((pair.second&signature) != pair.second)
continue;
2861 if(pair.first == originatingSystemTypeName)
continue;
An abstract base class for all ECS component arrays.
Definition ecs_world.hpp:166
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:260
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:174
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:812
virtual void onInitialize()
An overridable callback for right after an ECS world has just been created.
Definition ecs_world.hpp:981
virtual void onSimulationDeactivated()
Overridable callback called just after the ECS world owning this system has been deactivated.
Definition ecs_world.hpp:1050
virtual void onPreRenderStep(float simulationProgress)
Overridable callback called just before the render step takes place.
Definition ecs_world.hpp:1035
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:1010
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:970
void updateComponent_(EntityID entityID, const TComponent &component)
The actual implementation of updateComponent for a system.
Definition ecs_world.hpp:2848
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:1028
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:915
virtual void onEntityDisabled(EntityID entityID)
An overridable callback for when an entity has been disabled.
Definition ecs_world.hpp:962
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:819
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:1017
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:953
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:1064
virtual void onDestroyed()
Overridable callback called just before this system is destroyed.
Definition ecs_world.hpp:1058
virtual void onSimulationActivated()
An overridable callback for right after the ECS World has been activated.
Definition ecs_world.hpp:987
virtual void onSimulationStep(uint32_t simStepMillis)
An overridable callback called once in the middle of every simulation step.
Definition ecs_world.hpp:1003
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:1042
ComponentType getComponentType() const
Get the component type ID for a given component type.
Definition ecs_world.hpp:2827
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:833
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:1070
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:994
TComponent getComponent_(EntityID entityID, float progress=1.f) const
The actual implementation of getComponent for a system.
Definition ecs_world.hpp:2832
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:364
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:2843
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:498
void removeComponent(EntityID entityID) override
Removes the component associated with a specific entity, maintaining packing but not order.
Definition ecs_world.hpp:2412
virtual void handlePreSimulationStep() override
A callback for the start of a simulation step.
Definition ecs_world.hpp:2458
void addComponent(EntityID entityID, const TComponent &component)
Adds a component belonging to this entity to this array.
Definition ecs_world.hpp:2384
ComponentArray(std::weak_ptr< ECSWorld > world)
Construct a new Component Array object.
Definition ecs_world.hpp:371
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates the value of the component belonging to this entity.
Definition ecs_world.hpp:2444
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:484
bool hasComponent(EntityID entityID) const override
Tests whether an entry for a component belonging to this entity is present.
Definition ecs_world.hpp:2407
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component object.
Definition ecs_world.hpp:2436
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:491
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:2451
virtual void copyComponent(EntityID to, EntityID from) override
Handles the copying of a component belonging to one entity to another.
Definition ecs_world.hpp:2466
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:478
An object that stores and manages updates to all the component arrays instantiated for this ECS World...
Definition ecs_world.hpp:509
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:2542
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:796
void addComponent(EntityID entityID, const TComponent &component)
Adds a component entry for this entity in the array specified.
Definition ecs_world.hpp:2515
TComponent getComponent(EntityID entityID, float simulationProgress=1.f) const
Get the component value for this entity.
Definition ecs_world.hpp:2532
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with its new value.
Definition ecs_world.hpp:2537
std::shared_ptr< BaseComponentArray > getComponentArray(const std::string &componentTypeName) const
Get the Component Array object.
Definition ecs_world.hpp:584
std::unordered_map< std::size_t, ComponentType > mHashToComponentType
Maps a component's type hash to its ComponentType.
Definition ecs_world.hpp:779
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:2521
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:785
std::unordered_map< std::string, std::size_t > mNameToComponentHash
Maps each component type name to its corresponding hash.
Definition ecs_world.hpp:770
std::shared_ptr< ComponentArray< TComponent > > getComponentArray() const
Get the (specialized) Component Array object.
Definition ecs_world.hpp:572
void removeComponent(EntityID entityID)
Removes the component of the type specified from the entity.
Definition ecs_world.hpp:2526
ComponentManager(std::weak_ptr< ECSWorld > world)
Construct a new Component Manager object.
Definition ecs_world.hpp:516
std::weak_ptr< ECSWorld > mWorld
Holds a reference to the ECS world this component manager manages component arrays for.
Definition ecs_world.hpp:802
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:2488
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:2508
A class that represents a set of systems, entities, and components, that are all interrelated,...
Definition ecs_world.hpp:1462
SystemType getSystemType()
Get the SystemType id for a particular system.
Definition ecs_world.hpp:2685
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:2560
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:2556
static Entity createEntityPrototype(TComponents...components)
Create a prototype entity object.
Definition ecs_world.hpp:2805
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:2565
static void registerComponentTypes()
Registers ComponentArrays for each type of component present in the component type list,...
Definition ecs_world.hpp:2782
void enableEntity(EntityID entityID)
Enables an entity for a specific system.
Definition ecs_world.hpp:2710
EntityID mNextEntity
A new EntityID that has never been created before in this world.
Definition ecs_world.hpp:2101
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:2720
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:2820
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:1822
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:2093
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:2690
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:2087
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:2703
Entity privateCreateEntity(TComponents...components)
Creates a new entity and assigns it the components specified as arguments.
Definition ecs_world.hpp:2667
WorldID mID
The unique ID associated with this ECSWorld.
Definition ecs_world.hpp:2107
std::unique_ptr< ComponentManager > mComponentManager
A reference to this world's ComponentManager.
Definition ecs_world.hpp:2081
static std::shared_ptr< TSystem > getSystemPrototype()
Get the system object of a specific type belonging to the prototype ECSWorld.
Definition ecs_world.hpp:2815
void updateComponent(EntityID entityID, const TComponent &newValue)
Updates a component belonging to an entity with a new value.
Definition ecs_world.hpp:2738
Entity createEntity(TComponents...components)
Creates a new entity that will exist in this ECSWorld.
Definition ecs_world.hpp:2800
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:2810
void disableEntity(EntityID entityID)
Disables an entity on a particular system.
Definition ecs_world.hpp:2715
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:2113
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:2695
The Entity is a wrapper on an entity ID, used as the primary interface between an application and the...
Definition ecs_world.hpp:2127
void addComponent(const TComponent &component)
Adds a new component to this Entity.
Definition ecs_world.hpp:2610
EntityID getID()
Gets the ID associated with this Entity.
Definition ecs_world.hpp:2169
std::weak_ptr< ECSWorld > mWorld
The world this Entity belongs to.
Definition ecs_world.hpp:2366
void updateComponent(const TComponent &newValue)
Updates the value of a component belonging to this Entity.
Definition ecs_world.hpp:2625
void disableSystem()
Disables this entity for a particular system.
Definition ecs_world.hpp:2650
EntityID mID
The ID of this entity within its owning ECSWorld.
Definition ecs_world.hpp:2360
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:2615
std::weak_ptr< ECSWorld > getWorld()
Get the ECSWorld object this Entity belongs to.
Definition ecs_world.hpp:2338
void enableSystem()
Enables this Entity for a particular System.
Definition ecs_world.hpp:2635
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:2645
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:2354
bool isEnabled() const
Tests whether this entity is enabled for a particular system.
Definition ecs_world.hpp:2640
TComponent getComponent(float simulationProgress=1.f) const
Get the value of the component at a specific time this frame.
Definition ecs_world.hpp:2620
void removeComponent()
Removes a component from an entity.
Definition ecs_world.hpp:2630
A template class for interpolating components between simulation frames for various purposes.
Definition ecs_world.hpp:335
RangeMapperLinear mProgressLimits
a functor that performs the actual interpolation in the default case
Definition ecs_world.hpp:352
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:1171
void disableEntity(EntityID entityID)
Disables an entity for a particular system.
Definition ecs_world.hpp:2604
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:1418
std::weak_ptr< ECSWorld > mWorld
The world this System manager belongs to.
Definition ecs_world.hpp:1437
bool isEnabled(EntityID entityID)
Tests whether an entity is enabled for a particular system.
Definition ecs_world.hpp:2655
SystemManager(std::weak_ptr< ECSWorld > world)
Construct a new System Manager object.
Definition ecs_world.hpp:1178
SystemType getSystemType() const
Get the SystemType for a system.
Definition ecs_world.hpp:2549
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:1431
std::shared_ptr< TSystem > getSystem()
Gets an instance of a system belonging to this ECS World.
Definition ecs_world.hpp:2591
void enableEntity(EntityID entityID)
Enables the visibility of an entity to a particular system.
Definition ecs_world.hpp:2598
std::unordered_map< std::string, Signature > mNameToSignature
Mapping from the system type name for a system to its component signature.
Definition ecs_world.hpp:1410
std::unordered_map< std::string, SystemType > mNameToSystemType
Maps system type names to their corresponding SystemType values.
Definition ecs_world.hpp:1425
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:2854
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:2579
bool isRegistered(EntityID entityID)
Tests whether an entity is eligible for participation in a particular system.
Definition ecs_world.hpp:2661
void updateComponent(EntityID entityID, const TComponent &component)
Updates a component via this system's specialized version of updateComponent.
Definition ecs_world.hpp:1139
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:1115
static void registerSelf()
Helper function called during static initialization to make this project's ECS system aware that this...
Definition ecs_world.hpp:2571
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:1126
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:1157
A system template that disables systems with this form of declaration.
Definition ecs_world.hpp:1085
ECSType ComponentType
An unsigned integer representing the type of a component.
Definition ecs_world.hpp:101
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:2378
constexpr ComponentType kMaxComponents
A constant that restricts the number of definable components in a project.
Definition ecs_world.hpp:130
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:152
constexpr SystemType kMaxSystems
A constant that restricts the number of definable systems in a project.
Definition ecs_world.hpp:137
ECSType SystemType
An unsigned integer representing the type of a system.
Definition ecs_world.hpp:110
std::uint8_t ECSType
A number tag used to represent components and systems.
Definition ecs_world.hpp:91
constexpr EntityID kMaxEntities
A user-set constant which limits the number of creatable entities in a single ECS system.
Definition ecs_world.hpp:117
std::pair< WorldID, EntityID > UniversalEntityID
An ID that uniquely identifies an entity.
Definition ecs_world.hpp:83
std::uint64_t EntityID
A single unsigned integer used as a name for an entity managed by an ECS system.
Definition ecs_world.hpp:66
std::uint64_t WorldID
An unsigned integer representing the name of an ECS world.
Definition ecs_world.hpp:74
constexpr ECSType kMaxECSTypes
A constant used to restrict the number of definable system and component types in a project.
Definition ecs_world.hpp:123
Namespace containing all class definitions and functions related to the ToyMaker engine.
Definition application.hpp:26
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:287
static TComponent get(const nlohmann::json &jsonComponent)
Get method that automatically invokes a from_json function, found by nlohmann json.
Definition ecs_world.hpp:294
Helper function for retrieving the component type string defined as part of the component.
Definition ecs_world.hpp:541
std::string operator()()
The method that retrieves the component type string.
Definition ecs_world.hpp:547
Prevents the use of the unspecialized version of SystemRegistrationArgs.
Definition ecs_world.hpp:1552
Contains a couple of classes not tied to any part of the engine in particular, but useful to those pa...