Game of Ur 0.3.3
This is a computer adaptation of Game of Ur, written in C++ mainly using SDL and OpenGL.
Loading...
Searching...
No Matches
ToyMaker::ComponentManager Class Reference

An object that stores and manages updates to all the component arrays instantiated for this ECS World. More...

#include <ecs_world.hpp>

Classes

struct  getComponentTypeName
 Helper function for retrieving the component type string defined as part of the component. More...
 
struct  getComponentTypeName< std::shared_ptr< TComponent > >
 A specialization of getComponentTypeName for components which are wrapped in shared pointers. More...
 

Public Member Functions

 ComponentManager (std::weak_ptr< ECSWorld > world)
 Construct a new Component Manager object.
 

Private Member Functions

ComponentManager instantiate (std::weak_ptr< ECSWorld > world) const
 Constructs a new component manager and associates it with the world passed in as argument. Creates a fresh, but empty, set of component arrays.
 
template<typename TComponent>
void registerComponentArray ()
 A method to allow a new component type to register itself with the ECS system for this project.
 
template<typename TComponent>
std::shared_ptr< ComponentArray< TComponent > > getComponentArray () const
 Get the (specialized) Component Array object.
 
std::shared_ptr< BaseComponentArraygetComponentArray (const std::string &componentTypeName) const
 Get the Component Array object.
 
template<typename TComponent>
ComponentType getComponentType () const
 Get the component type ID for a given component type.
 
ComponentType getComponentType (const std::string &typeName) const
 Get the component type ID for a given component type string.
 
Signature getSignature (EntityID entityID)
 Get the component signature for a given entity.
 
template<typename TComponent>
void addComponent (EntityID entityID, const TComponent &component)
 Adds a component entry for this entity in the array specified.
 
void addComponent (EntityID entityID, const nlohmann::json &jsonComponent)
 Adds a component entry for this entity in the array specified (in the JSON description).
 
template<typename TComponent>
void removeComponent (EntityID entityID)
 Removes the component of the type specified from the entity.
 
void removeComponent (EntityID entityID, const std::string &type)
 Removes the component of the type specified from the entity.
 
template<typename TComponent>
bool hasComponent (EntityID entityID) const
 Tests whether an entity has a component of a particular type.
 
bool hasComponent (EntityID entityID, const std::string &type)
 Tests whether an entity has a component of a particular type.
 
template<typename TComponent>
TComponent getComponent (EntityID entityID, float simulationProgress=1.f) const
 Get the component value for this entity.
 
template<typename TComponent>
void updateComponent (EntityID entityID, const TComponent &newValue)
 Updates a component belonging to an entity with its new value.
 
void updateComponent (EntityID entityID, const nlohmann::json &componentProperties)
 Updates a component belonging to an entity with its new value(described in JSON).
 
template<typename TComponent>
void copyComponent (EntityID to, EntityID from)
 Copies a component value from one component to another within the same component array.
 
void copyComponents (EntityID to, EntityID from)
 Copies all components from one entity and updates or adds them to the other.
 
void copyComponents (EntityID to, EntityID from, ComponentManager &other)
 Copies all components from one entity and updates or adds them to the other, where the source entity may belong to a different ECS World.
 
void handleEntityDestroyed (EntityID entityID)
 Handles component arrays specific side effect of entity destruction.
 
void handlePreSimulationStep ()
 Callback for the start of a simulation step, where the contents of every component's next member is copied into its previous member.
 
void unregisterAll ()
 Unregisters all component arrays associated with this manager, as part of the destruction process for the world this component manager belongs to.
 

Private Attributes

std::unordered_map< std::string, std::size_t > mNameToComponentHash {}
 Maps each component type name to its corresponding hash.
 
std::unordered_map< std::size_t, ComponentTypemHashToComponentType {}
 Maps a component's type hash to its ComponentType.
 
std::unordered_map< std::size_t, std::shared_ptr< BaseComponentArray > > mHashToComponentArray {}
 Maps the hash of a component type to its corresponding ComponentArray.
 
std::unordered_map< EntityID, SignaturemEntityToSignature {}
 Stores the component signature of each entity.
 
std::weak_ptr< ECSWorldmWorld
 Holds a reference to the ECS world this component manager manages component arrays for.
 

Friends

class ECSWorld
 

Detailed Description

An object that stores and manages updates to all the component arrays instantiated for this ECS World.

Constructor & Destructor Documentation

◆ ComponentManager()

ToyMaker::ComponentManager::ComponentManager ( std::weak_ptr< ECSWorld > world)
inlineexplicit

Construct a new Component Manager object.

Parameters
worldThe world to which this component manager belongs.

Member Function Documentation

◆ addComponent() [1/2]

void ComponentManager::addComponent ( EntityID entityID,
const nlohmann::json & jsonComponent )
private

Adds a component entry for this entity in the array specified (in the JSON description).

Parameters
entityIDThe entity to which the component is being added.
jsonComponentThe value of the added component, described in JSON.

◆ addComponent() [2/2]

template<typename TComponent>
void ToyMaker::ComponentManager::addComponent ( EntityID entityID,
const TComponent & component )
private

Adds a component entry for this entity in the array specified.

Template Parameters
TComponentThe type of component being added.
Parameters
entityIDThe entity to which the component is being added.
componentThe value of the added component.

◆ copyComponent()

template<typename TComponent>
void ToyMaker::ComponentManager::copyComponent ( EntityID to,
EntityID from )
private

Copies a component value from one component to another within the same component array.

Template Parameters
TComponentThe type of component being copied.
Parameters
toThe entity to which the component is copied.
fromThe entity the component is copied from.

◆ copyComponents() [1/2]

void ComponentManager::copyComponents ( EntityID to,
EntityID from )
private

Copies all components from one entity and updates or adds them to the other.

Parameters
toThe entity to which the copied components will be added or updated.
fromThe entity from which the components will be copied.

◆ copyComponents() [2/2]

void ComponentManager::copyComponents ( EntityID to,
EntityID from,
ComponentManager & other )
private

Copies all components from one entity and updates or adds them to the other, where the source entity may belong to a different ECS World.

Parameters
toThe entity to which the copied components will be added or updated.
fromThe entity from which components will be copied.
otherThe component manager belonging to the same world as from.

◆ getComponent()

template<typename TComponent>
TComponent ToyMaker::ComponentManager::getComponent ( EntityID entityID,
float simulationProgress = 1.f ) const
private

Get the component value for this entity.

Template Parameters
TComponentThe type of component value to retrieve.
Parameters
entityIDThe entity whose component is being retrieved.
simulationProgressProgress towards the next simulation tick, a value between 0.0 and 1.0.
Returns
TComponent A copy of the entity's component.

◆ getComponentArray() [1/2]

template<typename TComponent>
std::shared_ptr< ComponentArray< TComponent > > ToyMaker::ComponentManager::getComponentArray ( ) const
inlineprivate

Get the (specialized) Component Array object.

Template Parameters
TComponentThe type of component whose array is being retrieved.
Returns
std::shared_ptr<ComponentArray<TComponent>> (Specialized) shared pointer to said component array.

◆ getComponentArray() [2/2]

std::shared_ptr< BaseComponentArray > ToyMaker::ComponentManager::getComponentArray ( const std::string & componentTypeName) const
inlineprivate

Get the Component Array object.

Parameters
componentTypeNameThe component type string for the desired component array.
Returns
std::shared_ptr<BaseComponentArray> (Unspecialized) shared pointer to said component array.

◆ getComponentType() [1/2]

template<typename TComponent>
ComponentType ToyMaker::ComponentManager::getComponentType ( ) const
private

Get the component type ID for a given component type.

Template Parameters
TComponentThe type of the component type ID being retrieved.
Returns
ComponentType The ID of the component type.

◆ getComponentType() [2/2]

ComponentType ComponentManager::getComponentType ( const std::string & typeName) const
private

Get the component type ID for a given component type string.

Parameters
typeNamethe component type string for the component type.
Returns
ComponentType The ID of the component type.

◆ getSignature()

Signature ComponentManager::getSignature ( EntityID entityID)
private

Get the component signature for a given entity.

The component signature describes which components are present for a given object. The position of each bit in the signature corresponds to the ComponentType that that bit represents.

  • 1 indicates that a component of that particular type is associated with the entity.
  • 0 indicates that there is no component of that type associated with the entity.
Parameters
entityIDThe entity whose component signature is being retrieved.
Returns
Signature A bitset representing the presence or absence of specific components for an entity.
See also
ComponentType
mEntityToSignature

◆ handleEntityDestroyed()

void ComponentManager::handleEntityDestroyed ( EntityID entityID)
private

Handles component arrays specific side effect of entity destruction.

Removes entry from every component array belonging to this entity.

Parameters
entityIDThe entity being destroyed.

◆ handlePreSimulationStep()

void ComponentManager::handlePreSimulationStep ( )
private

Callback for the start of a simulation step, where the contents of every component's next member is copied into its previous member.

Todo
rename to handleSimulationPreStep

◆ hasComponent() [1/2]

template<typename TComponent>
bool ToyMaker::ComponentManager::hasComponent ( EntityID entityID) const
private

Tests whether an entity has a component of a particular type.

Template Parameters
TComponentThe type of component whose existence is being tested.
Parameters
entityIDThe entity being queried.
Return values
trueThe component is present;
falseThe component is absent;
See also
bool hasComponent(EntityID entityID, const std::string& type)

◆ hasComponent() [2/2]

bool ComponentManager::hasComponent ( EntityID entityID,
const std::string & type )
private

Tests whether an entity has a component of a particular type.

Parameters
entityIDThe entity being queried.
typeThe component type string of the component whose existence is being tested.
Return values
trueThe component is present;
falseThe component is absent;
See also
template <typename TComponent> bool hasComponent(EntityID entityID) const

◆ instantiate()

ComponentManager ComponentManager::instantiate ( std::weak_ptr< ECSWorld > world) const
private

Constructs a new component manager and associates it with the world passed in as argument. Creates a fresh, but empty, set of component arrays.

Parameters
worldThe world to which the new component manager will belong.
Returns
ComponentManager The new component manager constructed, using this component manager as a template

◆ registerComponentArray()

template<typename TComponent>
void ToyMaker::ComponentManager::registerComponentArray ( )
private

A method to allow a new component type to register itself with the ECS system for this project.

Template Parameters
TComponentThe type of component being registered.

◆ removeComponent() [1/2]

template<typename TComponent>
void ToyMaker::ComponentManager::removeComponent ( EntityID entityID)
private

Removes the component of the type specified from the entity.

Template Parameters
TComponentThe type of component being removed.
Parameters
entityIDThe entity whose component is being removed.

◆ removeComponent() [2/2]

void ComponentManager::removeComponent ( EntityID entityID,
const std::string & type )
private

Removes the component of the type specified from the entity.

Parameters
entityIDThe entity whose component is being removed.
typeThe component type string of the component being removed.

◆ updateComponent() [1/2]

void ComponentManager::updateComponent ( EntityID entityID,
const nlohmann::json & componentProperties )
private

Updates a component belonging to an entity with its new value(described in JSON).

Parameters
entityIDThe entity whose component is being updated.
componentPropertiesThe new value for the component, described in JSON.
See also
void updateComponent<TComponent>(EntityID entityID, const TComponent& newValue)

◆ updateComponent() [2/2]

template<typename TComponent>
void ToyMaker::ComponentManager::updateComponent ( EntityID entityID,
const TComponent & newValue )
private

Updates a component belonging to an entity with its new value.

Template Parameters
TComponentThe type of component being updated.
Parameters
entityIDThe entity whose component is being updated.
newValueThe new value for the component.
See also
void updateComponent(EntityID entityID, const nlohmann::json& componentProperties)

Member Data Documentation

◆ mEntityToSignature

std::unordered_map<EntityID, Signature> ToyMaker::ComponentManager::mEntityToSignature {}
private

Stores the component signature of each entity.

The position of each bit in the signature corresponds to a ComponentType managed by the ComponentManager. A given entity either has or doesn't have a particular component according as the corresponding bit in the signature is set or unset.

See also
ComponentType
Signature
getSignature

◆ mHashToComponentType

std::unordered_map<std::size_t, ComponentType> ToyMaker::ComponentManager::mHashToComponentType {}
private

Maps a component's type hash to its ComponentType.

The hash of a component is provided by the STL type_traits library. The map allows us to retrieve the ECS Type ID (used in entity component signatures) for a type with a particular hash

See also
ComponentType

◆ mNameToComponentHash

std::unordered_map<std::string, std::size_t> ToyMaker::ComponentManager::mNameToComponentHash {}
private

Maps each component type name to its corresponding hash.

See also
mHashToComponentArray

The documentation for this class was generated from the following files: