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::ComponentArray< TComponent > Class Template Reference

A class that implements BaseComponentArray specializing it for a component of type TComponent. More...

#include <ecs_world.hpp>

Inheritance diagram for ToyMaker::ComponentArray< TComponent >:
ToyMaker::BaseComponentArray

Public Member Functions

 ComponentArray (std::weak_ptr< ECSWorld > world)
 Construct a new Component Array object.
 
- Public Member Functions inherited from ToyMaker::BaseComponentArray
 BaseComponentArray (std::weak_ptr< ECSWorld > world)
 Construct a new Base Component Array object.
 
virtual ~BaseComponentArray ()=default
 Destroy the Base Component Array object.
 

Private Member Functions

std::shared_ptr< BaseComponentArrayinstantiate (std::weak_ptr< ECSWorld > world) const override
 Creates a new component array of the same type as this and associated with a new World.
 
void addComponent (EntityID entityID, const TComponent &component)
 Adds a component belonging to this entity to this array.
 
void addComponent (EntityID entityID, const nlohmann::json &componentJSON) override
 Adds a component belonging to this entity to this array based on its JSON description.
 
void removeComponent (EntityID entityID) override
 Removes the component associated with a specific entity, maintaining packing but not order.
 
TComponent getComponent (EntityID entityID, float simulationProgress=1.f) const
 Get the component object.
 
bool hasComponent (EntityID entityID) const override
 Tests whether an entry for a component belonging to this entity is present.
 
void updateComponent (EntityID entityID, const TComponent &newValue)
 Updates the value of the component belonging to this entity.
 
void updateComponent (EntityID entityID, const nlohmann::json &value) override
 Updates the value of the component belonging to this entity based on a JSON description of the new value.
 
virtual void handleEntityDestroyed (EntityID entityID) override
 A callback to handle the side effect of the destruction of an entity. Here: deletion of the component associated with it.
 
virtual void handlePreSimulationStep () override
 A callback for the start of a simulation step.
 
virtual void copyComponent (EntityID to, EntityID from) override
 Handles the copying of a component belonging to one entity to another.
 
virtual void copyComponent (EntityID to, EntityID from, BaseComponentArray &other) override
 Handles the copying of a component belonging to one entity to another.
 

Private Attributes

std::vector< TComponent > mComponentsNext {}
 An array containing the state of each entity's component as will be seen at the start of the next simulation tick.
 
std::vector< TComponent > mComponentsPrevious {}
 An array containing the state of each entity's component as seen in the last simulation tick.
 
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 component.
 
std::unordered_map< std::size_t, EntityIDmComponentToEntity {}
 A mapping from the index of a component to the ID of the entity that owns that component.
 

Friends

class ComponentManager
 

Additional Inherited Members

- Protected Attributes inherited from ToyMaker::BaseComponentArray
std::weak_ptr< ECSWorldmWorld {}
 A reference to the world to which this component array belongs.
 

Detailed Description

template<typename TComponent>
class ToyMaker::ComponentArray< TComponent >

A class that implements BaseComponentArray specializing it for a component of type TComponent.

Template Parameters
TComponentThe type of component the array is being made for.
See also
ECSWorld::registerComponentTypes()

Constructor & Destructor Documentation

◆ ComponentArray()

template<typename TComponent>
ToyMaker::ComponentArray< TComponent >::ComponentArray ( std::weak_ptr< ECSWorld > world)
inlineexplicit

Construct a new Component Array object.

Parameters
worldThe world to which this component array will belong.

Member Function Documentation

◆ addComponent() [1/2]

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::addComponent ( EntityID entityID,
const nlohmann::json & componentJSON )
overrideprivatevirtual

Adds a component belonging to this entity to this array based on its JSON description.

Parameters
entityIDThe entity to which the component will belong
componentJSONA JSON description of the value of the component

Implements ToyMaker::BaseComponentArray.

◆ addComponent() [2/2]

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

Adds a component belonging to this entity to this array.

Parameters
entityIDThe entity to which the component will belong
componentThe value of the component

◆ copyComponent() [1/2]

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::copyComponent ( EntityID to,
EntityID from )
overrideprivatevirtual

Handles the copying of a component belonging to one entity to another.

Parameters
toThe entity to which the copied component goes
fromThe entity from which the component is copied, belonging to the same ComponentArray<T>

Implements ToyMaker::BaseComponentArray.

◆ copyComponent() [2/2]

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::copyComponent ( EntityID to,
EntityID from,
BaseComponentArray< TComponent > & other )
overrideprivatevirtual

Handles the copying of a component belonging to one entity to another.

Parameters
toThe entity to which the copied component goes
fromThe entity from which the component is copied
otherThe ComponentArray<T> which holds the component belonging to from

Implements ToyMaker::BaseComponentArray.

◆ getComponent()

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

Get the component object.

Parameters
entityIDThe entity to whom the component belongs
simulationProgressProgress towards the next simulation tick. By default 1.0, where the latest version of the component is retrieved.
Returns
TComponent The (interpolated) component

◆ handleEntityDestroyed()

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::handleEntityDestroyed ( EntityID entityID)
overrideprivatevirtual

A callback to handle the side effect of the destruction of an entity. Here: deletion of the component associated with it.

Parameters
entityIDThe entity being destroyed

Implements ToyMaker::BaseComponentArray.

◆ handlePreSimulationStep()

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::handlePreSimulationStep ( )
overrideprivatevirtual

A callback for the start of a simulation step.

This is where the next member array is copied into the previous member array.

Implements ToyMaker::BaseComponentArray.

◆ hasComponent()

template<typename TComponent>
bool ToyMaker::ComponentArray< TComponent >::hasComponent ( EntityID entityID) const
overrideprivatevirtual

Tests whether an entry for a component belonging to this entity is present.

Parameters
entityIDThe entity being tested.
Return values
trueComponent belonging to the entity is present;
falseComponent belonging to the entity is absent;

Implements ToyMaker::BaseComponentArray.

◆ instantiate()

template<typename TComponent>
std::shared_ptr< BaseComponentArray > ToyMaker::ComponentArray< TComponent >::instantiate ( std::weak_ptr< ECSWorld > world) const
overrideprivatevirtual

Creates a new component array of the same type as this and associated with a new World.

Parameters
worldThe world to which the newly constructed ComponentArray will belong
Returns
std::shared_ptr<BaseComponentArray> A shared pointer to the new component array

Implements ToyMaker::BaseComponentArray.

◆ removeComponent()

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::removeComponent ( EntityID entityID)
overrideprivatevirtual

Removes the component associated with a specific entity, maintaining packing but not order.

Parameters
entityIDThe entity associated with the component being removed

Implements ToyMaker::BaseComponentArray.

◆ updateComponent() [1/2]

template<typename TComponent>
void ToyMaker::ComponentArray< TComponent >::updateComponent ( EntityID entityID,
const nlohmann::json & value )
overrideprivatevirtual

Updates the value of the component belonging to this entity based on a JSON description of the new value.

Parameters
entityIDThe ID of the entity whose component is being updated
valueThe value to which the component will be updated, described in JSON

Implements ToyMaker::BaseComponentArray.

◆ updateComponent() [2/2]

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

Updates the value of the component belonging to this entity.

Parameters
entityIDThe ID of the entity whose component is being updated
newValueThe value to which the component will be updated

Member Data Documentation

◆ mComponentToEntity

template<typename TComponent>
std::unordered_map<std::size_t, EntityID> ToyMaker::ComponentArray< TComponent >::mComponentToEntity {}
private

A mapping from the index of a component to the ID of the entity that owns that component.

See also
mEntityToComponentIndex

◆ mEntityToComponentIndex

template<typename TComponent>
std::unordered_map<EntityID, std::size_t> ToyMaker::ComponentArray< TComponent >::mEntityToComponentIndex {}
private

A mapping from the ID of an entity to the index in the component array that stores the entity's component.

See also
mComponentToEntity

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