TailorMade 2.0
Tailor Made is a high-performance C++20 ECS library with on-the-fly entity creation and JSON-based definitions for entities, components, and their relationships.
 
Loading...
Searching...
No Matches
Environment Class Reference

#include <Environment.h>

Public Member Functions

 Environment (std::shared_ptr< EntityManager > entityManager)
 Constructor of an Environment without files, the only need is an EntityManager.
 
 Environment (const std::string &entitiesPath, const std::string &componentsPath, const std::string &subscriptionsPath)
 Constructor which take the paths of the JSON's description of the entities, components and subscriptions of this ECS environment.
 
void addManager (std::shared_ptr< ComponentManager > manager)
 Let you add a ComponentManager from the environment interface.
 
std::vector< std::shared_ptr< ComponentManager > > getManagers ()
 Return all the ComponentManagers.
 
std::shared_ptr< ComponentManagergetManager (const std::string &name)
 Return a specific ComponentManager.
 
std::shared_ptr< EntityManagergetEntityManager ()
 Return the EntityManager.
 
void setEntityState (const std::string &name, bool state, bool share=true)
 Let you set an entity's state with its name.
 
void setEntityState (int entity, bool state, bool share=true)
 Let you set an entity's state with its ID.
 
void setEntitiesState (const std::string &prefixOrTag, bool state, bool share=true, bool isPrefix=true)
 Let you update the state of all entities with a certain prefix in their name or with a certain tag.
 
void setState (int entity, const std::string &compName, bool state, bool share=true)
 Set the state of a specific component for an entity, with its ID.
 
void setState (const std::string &name, const std::string &compName, bool state, bool share=true)
 Set the state of a specific component for an entity, with its name.
 
void setStates (const std::string &prefixOrTag, const std::string &compName, bool state, bool share=true, bool isPrefix=true)
 Set the state of a set of entities for a specific component.
 
bool getState (int entity, const std::string &compName)
 Return the state of an entity's component, with its ID.
 
bool getState (const std::string &name, const std::string &compName)
 Return the state of an entity's component, with its name.
 
int getEntity (const std::string &name)
 Return an entity's ID from its name.
 
const std::string & getName (int entity)
 Return an entity's name from its ID.
 
int createEntity (const std::string &name, bool createFile=false, bool share=true)
 Create a new entity and return its ID.
 
void removeEntity (const std::string &name, bool share=true)
 Remove an entity from the EntityManager.
 
std::vector< std::shared_ptr< Component > > getComponents (int entity)
 Return all the components from an entity, with its ID.
 
std::vector< std::shared_ptr< Component > > getComponents (const std::string &name)
 Return all the components of an entity, with its name.
 
std::shared_ptr< ComponentgetComponent (int entity, const std::string &name)
 Return the component of a specific entity, with its ID.
 
std::shared_ptr< ComponentgetComponent (const std::string &entityName, const std::string &name)
 Return the component of a specific entity, with its name.
 
bool hasComponent (const std::string &name, const std::string &compName)
 Return true if the given entity possess the given component, false otherwise.
 
bool hasComponent (int entity, const std::string &compName)
 Return true if the given entity possess the given component, false otherwise.
 
bool hasTag (int entity, const std::string &tagName)
 Return true if the entity have the given tag, false otherwise.
 
bool hasTag (const std::string &name, const std::string &tagName)
 Return true if the entity have the given tag, false otherwise.
 
void addTag (int entity, const std::string &tag, bool share=true)
 Add a tag to an entity.
 
void addTag (const std::string &entity, const std::string &tag, bool share=true)
 Add a tag to an entity.
 
void save (int entity)
 Let you save the subscription of an entity in a file, it will preserve the current values of the components, therefore it can be used as a saving system.
 
void save (const std::string &name)
 Let you save the subscription of an entity in a file, it will preserve the current values of the components, therefore it can be used as a saving system.
 
void join (std::function< void(int)> callback, size_t ID)
 Let you join the Environment's update list.
 
void notify (int entity)
 Will notify every callbacks of an update in the environment.
 
void notify (size_t ID)
 Will share with a specific system all the entities currently in the environment.
 
int copy (const std::string &original, const std::string &copy, bool createFile=false, bool share=true)
 Let you create a new entity, which is a perfect copy of the original.
 
void give (const std::string &component, int giver, int receiver, bool copy, bool share=true)
 Give the ownership, or make a copy, of an entity's component to another component.
 
void makeSnapshot (const std::string &snapshotName, const std::vector< std::string > &toSave={}, const std::vector< std::string > &components={})
 Make and store a snapshot of the desired entities and components.
 
void loadSnapshot (const std::string &snapshotName)
 Load the snapshot from the given name if it exist.
 
void clearSnapshot (const std::string &snapshotName)
 Let you clear a snapshot from its name.
 

Constructor & Destructor Documentation

◆ Environment() [1/2]

Environment::Environment ( std::shared_ptr< EntityManager > entityManager)

Constructor of an Environment without files, the only need is an EntityManager.

Parameters
entityManagerThe EntityManager which will be used by this environment.

◆ Environment() [2/2]

Environment::Environment ( const std::string & entitiesPath,
const std::string & componentsPath,
const std::string & subscriptionsPath )

Constructor which take the paths of the JSON's description of the entities, components and subscriptions of this ECS environment.

The entire environment settings are loaded from these files.

Parameters
entitiesPathThe entities' root directory.
componentsPathThe components' root directory.
subscriptionsPathThe subscriptions' root directory.

Member Function Documentation

◆ addManager()

void Environment::addManager ( std::shared_ptr< ComponentManager > manager)

Let you add a ComponentManager from the environment interface.

Parameters
managerA shared_ptr towards the ComponentManagers which will be added to the environment.

◆ addTag() [1/2]

void Environment::addTag ( const std::string & entity,
const std::string & tag,
bool share = true )

Add a tag to an entity.

Parameters
nameEntity's name.
tagThe tag to add.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ addTag() [2/2]

void Environment::addTag ( int entity,
const std::string & tag,
bool share = true )

Add a tag to an entity.

Parameters
entityEntity's ID.
tagThe tag to add.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ clearSnapshot()

void Environment::clearSnapshot ( const std::string & snapshotName)

Let you clear a snapshot from its name.

Parameters
snapshotNameName of the snapshot.

◆ copy()

int Environment::copy ( const std::string & original,
const std::string & copy,
bool createFile = false,
bool share = true )

Let you create a new entity, which is a perfect copy of the original.

The new entity will have the name "copy" and its ID is returned.

Parameters
originalThe name of the original entity.
copyThe desired name for the copy.
createFileIf true, an entity's file is created in the root directory.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ createEntity()

int Environment::createEntity ( const std::string & name,
bool createFile = false,
bool share = true )

Create a new entity and return its ID.

If createFile is true, the entity's file will be create in the root directory.

Parameters
nameEntity's name.
createFileIf true, an entity's file is created in the root directory.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ getComponent() [1/2]

std::shared_ptr< Component > Environment::getComponent ( const std::string & entityName,
const std::string & name )

Return the component of a specific entity, with its name.

Parameters
nameEntity's name.
nameThe name of the component.

◆ getComponent() [2/2]

std::shared_ptr< Component > Environment::getComponent ( int entity,
const std::string & name )

Return the component of a specific entity, with its ID.

Parameters
entityEntity's ID.
nameThe name of the component.

◆ getComponents() [1/2]

std::vector< std::shared_ptr< Component > > Environment::getComponents ( const std::string & name)

Return all the components of an entity, with its name.

Parameters
nameEntity's name.

◆ getComponents() [2/2]

std::vector< std::shared_ptr< Component > > Environment::getComponents ( int entity)

Return all the components from an entity, with its ID.

Parameters
entityEntity's ID.

◆ getEntity()

int Environment::getEntity ( const std::string & name)

Return an entity's ID from its name.

Parameters
nameEntity's name.

◆ getEntityManager()

std::shared_ptr< EntityManager > Environment::getEntityManager ( )

Return the EntityManager.

◆ getManager()

std::shared_ptr< ComponentManager > Environment::getManager ( const std::string & name)

Return a specific ComponentManager.

Parameters
nameThe name of the desired ComponentManager.

◆ getManagers()

std::vector< std::shared_ptr< ComponentManager > > Environment::getManagers ( )

Return all the ComponentManagers.

◆ getName()

const std::string & Environment::getName ( int entity)

Return an entity's name from its ID.

Parameters
entityEntity's ID.

◆ getState() [1/2]

bool Environment::getState ( const std::string & name,
const std::string & compName )

Return the state of an entity's component, with its name.

Parameters
entityNameEntity's name.
compNameThe name of the component.

◆ getState() [2/2]

bool Environment::getState ( int entity,
const std::string & compName )

Return the state of an entity's component, with its ID.

Parameters
entityEntity's ID.
compNameThe name of the component.

◆ give()

void Environment::give ( const std::string & component,
int giver,
int receiver,
bool copy,
bool share = true )

Give the ownership, or make a copy, of an entity's component to another component.

Parameters
componentName of the component to give.
giverThe ID of the entity which give its component.
receiverThe ID of the entity which take the component.
copyIf true the component is just copied, otherwise the giver doesn't have the component anymore.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ hasComponent() [1/2]

bool Environment::hasComponent ( const std::string & name,
const std::string & compName )

Return true if the given entity possess the given component, false otherwise.

Parameters
nameEntity's name.
compNameComponent's name.

◆ hasComponent() [2/2]

bool Environment::hasComponent ( int entity,
const std::string & compName )

Return true if the given entity possess the given component, false otherwise.

Parameters
entityEntity's ID.
compNameComponent's name.

◆ hasTag() [1/2]

bool Environment::hasTag ( const std::string & name,
const std::string & tagName )

Return true if the entity have the given tag, false otherwise.

Parameters
nameEntity's name.
tagThe tag to search for.

◆ hasTag() [2/2]

bool Environment::hasTag ( int entity,
const std::string & tagName )

Return true if the entity have the given tag, false otherwise.

Parameters
entityEntity's ID.
tagThe tag to search for.

◆ join()

void Environment::join ( std::function< void(int)> callback,
size_t ID )

Let you join the Environment's update list.

This callback will be called every time a notification is send.

Parameters
callbackFunction which take an entity's ID and decide if it should be added to a system or not.
IDSystem's ID, to identify its callback.

◆ loadSnapshot()

void Environment::loadSnapshot ( const std::string & snapshotName)

Load the snapshot from the given name if it exist.

All the entities saved and the components saved of the snapshot will replace the current ones.

Parameters
snapshotNameName of the snapshot.

◆ makeSnapshot()

void Environment::makeSnapshot ( const std::string & snapshotName,
const std::vector< std::string > & toSave = {},
const std::vector< std::string > & components = {} )

Make and store a snapshot of the desired entities and components.

By default, toSave and components are empty, this means that every entities and every components should be saved.

The snapshot will be saved with the name "snapshotName", if it already exist the old snapshot is replaced.

Parameters
snapshotNameName of the snapshot.
toSaveList of entities to save, empty means all.
componentsList of components to save, empty means all those of the entities.

◆ notify() [1/2]

void Environment::notify ( int entity)

Will notify every callbacks of an update in the environment.

Only the updated entity is concerned.

Parameters
entityThe updated entity to share.

◆ notify() [2/2]

void Environment::notify ( size_t ID)

Will share with a specific system all the entities currently in the environment.

If the callback doesn't exist, do nothing.

Let you update entirely (or resync) a system's entities list.

Parameters
IDSystem's ID, identify its callback.

◆ removeEntity()

void Environment::removeEntity ( const std::string & name,
bool share = true )

Remove an entity from the EntityManager.

Warning
The removed entity's ID will be reused.
Parameters
nameEntity's name.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ save() [1/2]

void Environment::save ( const std::string & name)

Let you save the subscription of an entity in a file, it will preserve the current values of the components, therefore it can be used as a saving system.

If the file is already present in the directory folder or its subfolders, it will be replaced, otherwise it will be created in the directory folder of the subscriptions.

Parameters
nameThe name of the entity to save.

◆ save() [2/2]

void Environment::save ( int entity)

Let you save the subscription of an entity in a file, it will preserve the current values of the components, therefore it can be used as a saving system.

If the file is already present in the directory folder or its subfolders, it will be replaced, otherwise it will be created in the directory folder of the subscriptions.

Parameters
entityThe ID of the entity to save.

◆ setEntitiesState()

void Environment::setEntitiesState ( const std::string & prefixOrTag,
bool state,
bool share = true,
bool isPrefix = true )

Let you update the state of all entities with a certain prefix in their name or with a certain tag.

Apply this state to every components of these entities.

Parameters
prefixOrTagPrefix or tag for the research of entities.
stateThe new state to apply.
shareTells the method if you want the update to be shared to the systems. (default : true)
isPrefixTells if the first parameter should be considered as a prefix or a tag.

◆ setEntityState() [1/2]

void Environment::setEntityState ( const std::string & name,
bool state,
bool share = true )

Let you set an entity's state with its name.

Apply this state to every components of this entity.

Parameters
nameEntity's name.
stateThe new state to apply.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ setEntityState() [2/2]

void Environment::setEntityState ( int entity,
bool state,
bool share = true )

Let you set an entity's state with its ID.

Apply this state to every components of this entity.

Parameters
entityEntity's ID.
stateThe new state to apply.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ setState() [1/2]

void Environment::setState ( const std::string & name,
const std::string & compName,
bool state,
bool share = true )

Set the state of a specific component for an entity, with its name.

Parameters
nameEntity's name.
compNameThe name of the component.
stateThe new state to apply.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ setState() [2/2]

void Environment::setState ( int entity,
const std::string & compName,
bool state,
bool share = true )

Set the state of a specific component for an entity, with its ID.

Parameters
entityEntity's ID.
compNameThe name of the component.
stateThe new state to apply.
shareTells the method if you want the update to be shared to the systems. (default : true)

◆ setStates()

void Environment::setStates ( const std::string & prefixOrTag,
const std::string & compName,
bool state,
bool share = true,
bool isPrefix = true )

Set the state of a set of entities for a specific component.

The entities can be gathered from a prefix or from a tag.

Parameters
prefixOrTagPrefix or tag for the research of entities.
compNameThe name of the component.
stateThe new state to apply.
shareTells the method if you want the update to be shared to the systems. (default : true)
isPrefixTells if the first parameter should be considered as a prefix or a tag.

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