R-Type 0.1
Loading...
Searching...
No Matches
rtk::factory::EntityFactory Class Reference

Builds entities from JSON blueprints and registered component builders. More...

#include <factoryDataDriven/factoryDataDriven.hpp>

Public Types

using ArchetypeID = rtk::ArchetypeID
 16-bit identifier of an entity blueprint.
 
using json = nlohmann::json
 JSON document type.
 
using ComponentBuilder
 Function that adds one component to an entity from its JSON data.
 

Public Member Functions

void registerBuilder (const std::string &name, ComponentBuilder builder)
 Associates a blueprint component key with a builder.
 
ArchetypeID registerBlueprint (const std::string &blueprintName, const json &blueprintJson)
 Stores a blueprint under a name.
 
ArchetypeID loadBlueprintFromPath (const std::string &blueprintName, const std::string &filepath)
 Loads a blueprint from a JSON file and stores it.
 
ArchetypeID getArchetypeID (const std::string &blueprintName) const
 Returns the archetype id of a blueprint name.
 
const std::string & getBlueprintName (ArchetypeID archetypeId) const
 Returns the blueprint name of an archetype id.
 
bool hasArchetype (ArchetypeID archetypeId) const
 Tells whether a blueprint with this archetype id is loaded.
 
bool hasBlueprint (const std::string &blueprintName) const
 Tells whether a blueprint with this name is loaded.
 
void buildComponents (rtk::ecs::Registry &reg, std::size_t entity, const json &componentsJson)
 Adds every component listed in a JSON object, unknown keys are ignored.
 
std::size_t createEntity (rtk::ecs::Registry &reg, const json &entityJson)
 Spawns an entity from a blueprint document.
 
std::size_t createFromBlueprint (rtk::ecs::Registry &reg, const std::string &blueprintName)
 Spawns an entity from a loaded blueprint.
 
std::size_t createFromBlueprint (rtk::ecs::Registry &reg, ArchetypeID archetypeId)
 Spawns an entity from a loaded blueprint.
 
std::size_t createNetwork (rtk::ecs::Registry &reg, ArchetypeID archetypeId, std::uint32_t networkId)
 Spawns a replicated entity and attaches its NetworkEntity.
 
std::size_t createNetwork (rtk::ecs::Registry &reg, const std::string &blueprintName, std::uint32_t networkId)
 Spawns a replicated entity and attaches its NetworkEntity.
 

Detailed Description

Builds entities from JSON blueprints and registered component builders.

Member Typedef Documentation

◆ ComponentBuilder

Initial value:
std::function<void(rtk::ecs::Registry &, std::size_t, const json &)>
nlohmann::json json
JSON document type.
Definition factoryDataDriven.hpp:59

Function that adds one component to an entity from its JSON data.

Member Function Documentation

◆ registerBuilder()

void rtk::factory::EntityFactory::registerBuilder ( const std::string & name,
ComponentBuilder builder )
inline

Associates a blueprint component key with a builder.

Parameters
nameKey used in the components object of a blueprint.
builderFunction that adds the component.

◆ registerBlueprint()

ArchetypeID rtk::factory::EntityFactory::registerBlueprint ( const std::string & blueprintName,
const json & blueprintJson )
inline

Stores a blueprint under a name.

Parameters
blueprintNameBlueprint name, hashed to get its archetype id.
blueprintJsonBlueprint document.
Returns
The archetype id of the blueprint.

◆ loadBlueprintFromPath()

ArchetypeID rtk::factory::EntityFactory::loadBlueprintFromPath ( const std::string & blueprintName,
const std::string & filepath )
inline

Loads a blueprint from a JSON file and stores it.

Parameters
blueprintNameBlueprint name, hashed to get its archetype id.
filepathPath of the JSON file.
Returns
The archetype id of the blueprint.
Exceptions
std::runtime_errorIf the file cannot be opened.

◆ getArchetypeID()

ArchetypeID rtk::factory::EntityFactory::getArchetypeID ( const std::string & blueprintName) const
inlinenodiscard

Returns the archetype id of a blueprint name.

Parameters
blueprintNameBlueprint name.
Returns
The archetype id, whether or not the blueprint is loaded.

◆ getBlueprintName()

const std::string & rtk::factory::EntityFactory::getBlueprintName ( ArchetypeID archetypeId) const
inlinenodiscard

Returns the blueprint name of an archetype id.

Parameters
archetypeIdArchetype id.
Returns
The blueprint name.
Exceptions
std::runtime_errorIf no loaded blueprint has this id.

◆ hasArchetype()

bool rtk::factory::EntityFactory::hasArchetype ( ArchetypeID archetypeId) const
inlinenodiscard

Tells whether a blueprint with this archetype id is loaded.

Parameters
archetypeIdArchetype id.
Returns
True if the blueprint is loaded.

◆ hasBlueprint()

bool rtk::factory::EntityFactory::hasBlueprint ( const std::string & blueprintName) const
inlinenodiscard

Tells whether a blueprint with this name is loaded.

Parameters
blueprintNameBlueprint name.
Returns
True if the blueprint is loaded.

◆ buildComponents()

void rtk::factory::EntityFactory::buildComponents ( rtk::ecs::Registry & reg,
std::size_t entity,
const json & componentsJson )
inline

Adds every component listed in a JSON object, unknown keys are ignored.

Parameters
regRegistry owning the entity.
entityTarget entity.
componentsJsonObject mapping builder keys to component data.

◆ createEntity()

std::size_t rtk::factory::EntityFactory::createEntity ( rtk::ecs::Registry & reg,
const json & entityJson )
inline

Spawns an entity from a blueprint document.

Parameters
regRegistry receiving the entity.
entityJsonDocument with an optional components object.
Returns
The new entity.

◆ createFromBlueprint() [1/2]

std::size_t rtk::factory::EntityFactory::createFromBlueprint ( rtk::ecs::Registry & reg,
const std::string & blueprintName )
inline

Spawns an entity from a loaded blueprint.

Parameters
regRegistry receiving the entity.
blueprintNameBlueprint name.
Returns
The new entity.
Exceptions
std::runtime_errorIf the blueprint is not loaded.

◆ createFromBlueprint() [2/2]

std::size_t rtk::factory::EntityFactory::createFromBlueprint ( rtk::ecs::Registry & reg,
ArchetypeID archetypeId )
inline

Spawns an entity from a loaded blueprint.

Parameters
regRegistry receiving the entity.
archetypeIdArchetype id of the blueprint.
Returns
The new entity.
Exceptions
std::runtime_errorIf the blueprint is not loaded.

◆ createNetwork() [1/2]

std::size_t rtk::factory::EntityFactory::createNetwork ( rtk::ecs::Registry & reg,
ArchetypeID archetypeId,
std::uint32_t networkId )
inline

Spawns a replicated entity and attaches its NetworkEntity.

Parameters
regRegistry receiving the entity.
archetypeIdArchetype id of the blueprint.
networkIdNetwork id of the entity.
Returns
The new entity.
Exceptions
std::runtime_errorIf the blueprint is not loaded.

◆ createNetwork() [2/2]

std::size_t rtk::factory::EntityFactory::createNetwork ( rtk::ecs::Registry & reg,
const std::string & blueprintName,
std::uint32_t networkId )
inline

Spawns a replicated entity and attaches its NetworkEntity.

Parameters
regRegistry receiving the entity.
blueprintNameBlueprint name.
networkIdNetwork id of the entity.
Returns
The new entity.
Exceptions
std::runtime_errorIf the blueprint is not loaded.