jubilant-funicular
Classes | Public Member Functions | Private Attributes | List of all members
nta::ECS Class Reference

#include <ECS.h>

Collaboration diagram for nta::ECS:
Collaboration graph
[legend]

Classes

struct  ComponentInfo
 Info directly attached to a ComponentID. More...
 

Public Member Functions

 ECS (const ComponentRegistry &registry)
 
Entity gen_entity ()
 Generates a new Entity, returning its ID.
 
void gen_entities (std::size_t num, Entity *ids)
 Generates several entities, storing their IDs in ids.
 
bool delete_entity (Entity id)
 
bool delete_owner (ComponentID cmpn)
 
std::size_t num_entities () const
 Returns the number of entities in the system.
 
template<typename T >
std::size_t num_components () const
 Returns the number of components of the given type.
 
std::size_t num_components () const
 
template<typename T , typename... Args>
utils::Option< ComponentIDadd_component (Entity entity, Args &&... args)
 
template<typename T , typename... Args>
utils::Option< ComponentIDadd_sibling (ComponentID cmpn, Args &&... args)
 
bool delete_component (ComponentID cmpn)
 
template<typename T >
bool delete_component (Entity entity)
 
template<typename T >
bool has_component (Entity entity) const
 Returns true if the given Entity has a Component of type T.
 
template<typename T >
bool has_sibling (ComponentID cmpn) const
 
bool does_entity_exist (Entity entity) const
 Returns true if the given Entity exists.
 
utils::Option< Entityget_owner (ComponentID cmpn) const
 Returns the Entity associated to this Component.
 
template<typename T >
ComponentList< T > & get_component_list () const
 Returns a list of all components of the given type.
 
template<typename T >
utils::Option< T & > get_component (Entity entity) const
 Returns the Component of the given type associated to the given Entity.
 
utils::Option< Component & > get_component (ComponentID id) const
 Returns the Component with the given id.
 
template<typename T >
utils::Option< T & > get_sibling (ComponentID cmpn) const
 Returns the Component of the given type with the same owner as cmpn.
 
template<typename T >
void for_each (std::function< void(T &)> func) const
 Runs a function of each Component of a given type.
 
template<typename T , typename Event >
void enact_on (const Event &event, Entity entity) const
 Enacts the event on the Component of the given type owned by the given Entity.
 
template<typename T , typename Event , typename Event::enum_type e>
void enact_on (const Event &event, Entity entity) const
 
template<typename T , typename Event >
void enact_on (const Event &event, typename Event::enum_type e, Entity entity) const
 
template<typename T , typename Event >
void enact_on_sibling (const Event &event, ComponentID cmpn) const
 Enacts the event on the Component of the given type owned by the same entity.
 
template<typename T , typename Event , typename Event::enum_type e>
void enact_on_sibling (const Event &event, ComponentID cmpn) const
 
template<typename T , typename Event >
void enact_on_sibling (const Event &event, typename Event::enum_type e, ComponentID cmpn) const
 
template<typename T , typename Event >
void enact_on_all (const Event &event, typename Event::enum_type e) const
 Enacts the event on all the Components of the given type.
 
template<typename T , typename Event , typename Event::enum_type e>
void enact_on_all (const Event &event) const
 
void clear ()
 Removes all entities and components from this system.
 

Private Attributes

utils::TypeMap m_components
 
utils::SlotMap< ComponentInfom_component_info
 
utils::IDFactory< Entitym_entity_gen
 Responsible for creating unique IDs for the Entities.
 
utils::IDFactory< ComponentIDm_cmpn_gen
 Responsible for creating unique IDs for the Components.
 
const ComponentRegistry m_registry
 

Detailed Description

Container class representing a complete Entity-Component System

Manages creation of Entities and grouping of Components.

Assumes each entity can only have one Component of a given type.

The Event template parameter used in some functions should usually be an instantiation of EventTemplate. It is used for passing around events/signals/commands, i.e. it should be invokable on components

Event f;
Component c;
...
f(c);

If you don't need events, leave this parameter unspecified.

Definition at line 135 of file ECS.h.


Class Documentation

◆ nta::ECS::ComponentInfo

struct nta::ECS::ComponentInfo

Info directly attached to a ComponentID.

Definition at line 138 of file ECS.h.

Collaboration diagram for nta::ECS::ComponentInfo:
Class Members
Entity owner
size_t type

Member Function Documentation

◆ add_component()

template<typename T , typename... Args>
utils::Option< ComponentID > nta::ECS::add_component ( Entity  entity,
Args &&...  args 
)

Adds the given Component to the given Entity.

You pass in the entity and any arguments needed to construct a new object of type T

T should be a concrete type, not a pointer type

Returns the id of the created Component (or None on failure).

Definition at line 250 of file ECS.h.

◆ delete_component()

bool nta::ECS::delete_component ( ComponentID  cmpn)

Attempts to delete the given Component

Returns false on failure

Definition at line 39 of file ECS.cpp.

◆ delete_entity()

bool nta::ECS::delete_entity ( Entity  id)

Attempts to delete Entity with given id.

Returns true on success

Definition at line 16 of file ECS.cpp.

Member Data Documentation

◆ m_component_info

utils::SlotMap<ComponentInfo> nta::ECS::m_component_info
private

A map from ComponentID to relevant information about that Component

Since ComponentIDs are unique, I could get rid of this and just always loop over all records instead.

Definition at line 153 of file ECS.h.

Referenced by add_component(), clear(), delete_component(), delete_entity(), get_component(), and get_owner().

◆ m_components

utils::TypeMap nta::ECS::m_components
private

All the components in this system

Only stores types of the form ComponentList<T> where T inherits Component. Each vector has size m_entity_gen.get_last_id().

Definition at line 148 of file ECS.h.

Referenced by add_component(), clear(), delete_component(), delete_entity(), get_component(), get_component_list(), and has_component().

◆ m_registry

const ComponentRegistry nta::ECS::m_registry
private

The various Component types in use

This registry should not change after it's associated to an ECS

Definition at line 161 of file ECS.h.

Referenced by clear(), delete_component(), delete_entity(), and get_component().


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