R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ecs::Registry Class Reference

Manages entities, their signatures and component type registrations. More...

#include <Registry.hpp>

Collaboration diagram for ecs::Registry:
Collaboration graph

Public Member Functions

 Registry ()
 Construct a new Registry object.
 
 ~Registry ()
 Destroy the Registry object and clear internal containers.
 
Address newEntity ()
 Create and register a new entity, returning its Address.
 
template<typename T >
void setComponent (Address address, const T &component)
 Set/add a component to an entity with its data.
 
template<typename T >
T & getComponent (Address address)
 Get a component from an entity.
 
template<typename T >
bool hasComponent (Address address)
 Check if an entity has a specific component.
 
template<typename T >
void removeComponent (Address address)
 Remove a component from an entity.
 
template<typename T >
void addEntityProp (Address address)
 Attach a component type T to an entity (set the component bit).
 
void destroyEntity (Address address)
 Remove an entity and its Signature from the registry.
 
Signature getSignature (Address address)
 Retrieve the Signature for a given entity address.
 
template<typename... Components>
std::vector< Addressview ()
 Get all entities that have a specific set of components.
 
std::vector< AddressgetEntitiesWithMask (Signature requiredMask)
 Get all entities matching a specific component mask.
 

Private Member Functions

Address _generateAddress ()
 
Signature _registerComponent (ComponentType componentType)
 

Private Attributes

std::shared_mutex _mutex
 
std::unordered_map< Address, Signature_signatures
 
Address _nextAddress
 
std::priority_queue< Address, std::vector< Address >, std::greater< Address > > _freeAddresses = {}
 
std::unordered_map< ComponentType, Signature_componentMap = {}
 
std::unordered_map< ComponentType, std::unordered_map< Address, std::any > > _componentStorage = {}
 

Detailed Description

Manages entities, their signatures and component type registrations.

Responsibilities:

  • Generate unique sequential addresses for new entities, reusing freed addresses when possible.
  • Maintain a mapping from Address -> Signature (which components an entity has).
  • Maintain a mapping from component type (std::type_index) -> Signature bit.

Notes:

  • The number of distinct component types is limited by N_MAX_COMPONENTS.
  • Signatures are implemented as std::bitset and each registered component occupies a single bit.

Definition at line 68 of file Registry.hpp.

Constructor & Destructor Documentation

◆ Registry()

ecs::Registry::Registry ( )

Construct a new Registry object.

Initializes internal maps. Addresses are generated sequentially and freed addresses are reused from a pool.

Definition at line 11 of file Registry.cpp.

◆ ~Registry()

ecs::Registry::~Registry ( )

Destroy the Registry object and clear internal containers.

Definition at line 13 of file Registry.cpp.

References _componentMap, _componentStorage, and _signatures.

Member Function Documentation

◆ _generateAddress()

Address ecs::Registry::_generateAddress ( )
private

Definition at line 19 of file Registry.cpp.

References _freeAddresses, and _nextAddress.

Referenced by newEntity().

◆ _registerComponent()

Signature ecs::Registry::_registerComponent ( ComponentType  componentType)
private

Definition at line 32 of file Registry.cpp.

References _componentMap, and N_MAX_COMPONENTS.

◆ addEntityProp()

template<typename T >
void ecs::Registry::addEntityProp ( Address  address)

Attach a component type T to an entity (set the component bit).

Deprecated:
Use setComponent() instead to store component data. This method only sets the signature bit without storing data.

If the component type T is not yet registered, it will be registered automatically. If the component registration fails (component limit reached), no change is made.

Template Parameters
TThe component type to add.
Parameters
addressThe entity Address to which the component is added.

◆ destroyEntity()

void ecs::Registry::destroyEntity ( Address  address)

Remove an entity and its Signature from the registry.

Parameters
addressThe Address of the entity to destroy.

Definition at line 56 of file Registry.cpp.

References _componentStorage, _freeAddresses, _mutex, and _signatures.

Referenced by ecs::CollisionSystem::update().

◆ getComponent()

◆ getEntitiesWithMask()

std::vector< Address > ecs::Registry::getEntitiesWithMask ( Signature  requiredMask)

Get all entities matching a specific component mask.

Returns a vector of entity addresses that have all the components specified by the bitmask. This is the low-level filtering method used by systems for efficient entity queries.

Parameters
requiredMaskBitmask of required components (from ComponentMask)
Returns
std::vector<Address> Vector of entity addresses matching the mask.
// Get entities matching a specific mask
ComponentMask mask = (1ULL << getComponentType<Transform>()) |
(1ULL << getComponentType<Velocity>());
auto entities = registry.getEntitiesWithMask(mask);
std::uint64_t ComponentMask
Type alias for component bitmask.
Definition ISystem.hpp:24

Definition at line 79 of file Registry.cpp.

References _mutex, and _signatures.

Referenced by ecs::MapSystem::_applyScrolling(), scripting::LuaSystemAdapter::update(), ecs::AISystem::update(), ecs::AnimationSystem::update(), ecs::BoundarySystem::update(), ecs::CollisionSystem::update(), ecs::HealthSystem::update(), ecs::MapSystem::update(), ecs::MovementSystem::update(), ecs::OrbitalSystem::update(), and ecs::WeaponSystem::update().

◆ getSignature()

Signature ecs::Registry::getSignature ( Address  address)

Retrieve the Signature for a given entity address.

If the address is not present, a zero (empty) Signature is returned.

Parameters
addressThe entity Address to query.
Returns
Signature The Signature of the entity or zero if not found.

Definition at line 70 of file Registry.cpp.

References _mutex, and _signatures.

Referenced by ecs::wrapper::Entity::isValid().

◆ hasComponent()

◆ newEntity()

Address ecs::Registry::newEntity ( )

Create and register a new entity, returning its Address.

A new unique Address is generated and an empty Signature (no components) is associated with it.

Returns
Address the new entity's address.

Definition at line 47 of file Registry.cpp.

References _generateAddress(), _mutex, and _signatures.

Referenced by ecs::SpawnSystem::_spawnEnemy(), ecs::PrefabFactory::createEnemy(), ecs::PrefabFactory::createEnemy(), ecs::PrefabFactory::createEnemyFromRegistry(), ecs::PrefabFactory::createHealthPack(), ecs::PrefabFactory::createPlayer(), ecs::PrefabFactory::createPowerUp(), ecs::PrefabFactory::createProjectile(), ecs::WeaponSystem::createProjectile(), and ecs::PrefabFactory::createWall().

Here is the call graph for this function:

◆ removeComponent()

template<typename T >
void ecs::Registry::removeComponent ( Address  address)

Remove a component from an entity.

Removes the component data and updates the entity's signature.

Template Parameters
TThe component type to remove.
Parameters
addressThe entity Address.

Referenced by ecs::BuffSystem::update().

◆ setComponent()

template<typename T >
void ecs::Registry::setComponent ( Address  address,
const T &  component 
)

Set/add a component to an entity with its data.

If the component type is not yet registered, it will be registered automatically. Updates the entity's signature and stores the component data.

Template Parameters
TThe component type to set.
Parameters
addressThe entity Address.
componentThe component data to store.
Exceptions
std::runtime_errorif component limit is reached or entity doesn't exist.

Referenced by ecs::SpawnSystem::_spawnEnemy(), ecs::PrefabFactory::createEnemy(), ecs::PrefabFactory::createEnemy(), ecs::PrefabFactory::createEnemyFromRegistry(), ecs::PrefabFactory::createHealthPack(), ecs::PrefabFactory::createOrbitalModule(), ecs::PrefabFactory::createPlayer(), ecs::PrefabFactory::createPowerUp(), ecs::PrefabFactory::createProjectile(), ecs::WeaponSystem::createProjectile(), ecs::PrefabFactory::createWall(), ecs::CollisionSystem::handlePickup(), ecs::BoundarySystem::update(), ecs::HealthSystem::update(), and ecs::OrbitalSystem::updateOrbitalPosition().

◆ view()

template<typename... Components>
std::vector< Address > ecs::Registry::view ( )

Get all entities that have a specific set of components.

Returns a vector of entity addresses that possess all the specified component types. This allows efficient iteration over entities matching a specific archetype.

Template Parameters
ComponentsThe component types to filter by.
Returns
std::vector<Address> Vector of entity addresses with all specified components.
// Get all entities with both Transform and Velocity
auto entities = registry.view<Transform, Velocity>();
for (auto entity : entities) {
auto& transform = registry.getComponent<Transform>(entity);
auto& velocity = registry.getComponent<Velocity>(entity);
// process...
}
Component representing position, rotation and scale in 2D space.
Definition Transform.hpp:20
Component representing movement direction and speed.
Definition Velocity.hpp:20

Referenced by ecs::wrapper::ECSWorld::clear(), ecs::BuffSystem::update(), and ecs::SpawnSystem::update().

Member Data Documentation

◆ _componentMap

std::unordered_map<ComponentType, Signature> ecs::Registry::_componentMap = {}
private

Definition at line 81 of file Registry.hpp.

Referenced by _registerComponent(), and ~Registry().

◆ _componentStorage

std::unordered_map<ComponentType, std::unordered_map<Address, std::any> > ecs::Registry::_componentStorage = {}
private

Definition at line 82 of file Registry.hpp.

Referenced by destroyEntity(), and ~Registry().

◆ _freeAddresses

std::priority_queue<Address, std::vector<Address>, std::greater<Address> > ecs::Registry::_freeAddresses = {}
private

Definition at line 80 of file Registry.hpp.

Referenced by _generateAddress(), and destroyEntity().

◆ _mutex

std::shared_mutex ecs::Registry::_mutex
mutableprivate

Definition at line 71 of file Registry.hpp.

Referenced by destroyEntity(), getEntitiesWithMask(), getSignature(), and newEntity().

◆ _nextAddress

Address ecs::Registry::_nextAddress
private

Definition at line 74 of file Registry.hpp.

Referenced by _generateAddress().

◆ _signatures

std::unordered_map<Address, Signature> ecs::Registry::_signatures
private

Definition at line 72 of file Registry.hpp.

Referenced by destroyEntity(), getEntitiesWithMask(), getSignature(), newEntity(), and ~Registry().


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