Cogs.Core
|
Stores top level entities for the engine. More...
#include <EntityStore.h>
Public Member Functions | |
EntityStore (Context *context) | |
Construct the store with the given context instance. | |
void | clear () |
Clear out all top level entities owned by the store. | |
void | addEntityDefinition (const EntityDefinition &definition) |
Add the given entity definition to the store. | |
const EntityDefinition * | getEntityDefinition (const StringView &name) const |
Fetch the entity definition with the given name from the store. | |
const EntityDefinition * | getEntityDefinition (size_t id) const |
void | getEntityDefinitions (std::vector< StringView > &names) const |
Get names of currently known entity definitions. | |
void | getEntityDefinitionComponents (std::vector< StringView > &components, const StringView &name) const |
Get components of a known entity definition. | |
EntityPtr | createEntity (const StringView &name, const StringView &type, bool storeOwnership=true) |
Create a new Entity. | |
void | createEntities (size_t count, std::vector< EntityPtr > &entities) |
Allocates and initializes a collection of empty entities. | |
void | createEntities (size_t count, std::vector< EntityPtr > &entities, std::function< void(ComponentModel::Entity *)> destructor) |
EntityPtr | createChildEntity (const StringView &type, ComponentModel::Entity *parent, const StringView &name=StringView()) |
Create a new Entity, parenting it to the given parent. | |
void | removeChild (ComponentModel::Entity *parent, const ComponentModel::Entity *entity) |
Remove the parent-child relationship between parent and entity. | |
void | removeChildren (ComponentModel::Entity *entity) |
Removes all children from the given entity. | |
void | setEntityParent (ComponentModel::Entity *parent, ComponentModel::Entity *child) |
Move Root entity to a parent: Equal to addChild(parent, child) + destroyEntity(child) Move Child entity to root: Removes child from old parent. | |
void | destroyEntity (const EntityId id) |
Destroy the entity with the given id. | |
EntityPtr | getEntity (const StringView &name, bool logIfNotFound=true) const |
Retrieve a reference to the shared entity pointer to the Entity with the given name. | |
EntityPtr | findEntity (const StringView &name, const ComponentModel::Entity *root=nullptr, EntityFind findOptions=EntityFind::Default) const |
Finds an entity with the given name. | |
Cogs::ComponentModel::Entity * | getEntityParent (const ComponentModel::Entity *entity) const |
Gets the parent of the given entity. | |
EntityPtr | getEntity (const EntityId entityId, bool logIfNotFound=true, bool onlyRegistered=false) const |
Retrieve a reference to the shared entity pointer to the Entity with the given entityId. | |
void | renameEntity (ComponentModel::Entity *entityPtr, StringView name) |
Rename the given entity. | |
bool | hasEntityOwnership (const EntityId entityId) const |
Check if the given entityId has global ownership in EntityStore. | |
const std::unordered_map< EntityId, EntityPtr > & | getEntities () const |
Return map of entities with global ownership. | |
ComponentModel::Entity * | getEntityPtr (const EntityId entityId) |
Get a raw pointer to the entity with the given id. | |
std::string | dumpHierarchy () const |
Generates a debug dump of the entire entity hierarchy. | |
std::string | dumpHierarchy (const ComponentModel::Entity *parent, const std::string &prefix="") const |
Generates a debug dump of the entity hierarchy from the specified parent entity. | |
void | addSystem (const Reflection::TypeId typeId, ComponentCreator creator, ComponentDestroyer destroyer) |
Adds the given creator and destroyer functions for handling components of the given typeId. | |
void | addSystem (class ComponentSystemBase *system) |
Adds the given system, creating creator and destroyer functions for the component type handled by the system and registering those using addSystem(). | |
void | addChild (ComponentModel::Entity *parent, const EntityPtr &entity) |
Add a child to the given parent. | |
void | addChild (EntityId parent, EntityId child) |
ComponentModel::Component * | addComponent (ComponentModel::Entity *entity, Reflection::TypeId typeId) |
Add a component of the given type to the entity. | |
ComponentModel::Component * | addComponent (ComponentModel::Entity *entity, const StringView &type) |
template<typename ComponentType > | |
ComponentType * | addComponent (ComponentModel::Entity *entity) |
void | removeComponent (ComponentModel::Entity *entity, ComponentModel::ComponentHandle handle) |
const std::unordered_map< size_t, EntityDefinition > & | getDefinitions () const |
size_t | getAllocatedEntityCount () const |
Return total number of entities allocated. | |
uint32_t | getRevision () const |
Returns a number that changes every time an entity is created or destroyed. | |
void | getEntitiesWithComponent (std::vector< EntityId > &entities, Reflection::TypeId typeId) |
Context * | getContext () const |
Private Member Functions | |
EntityId | getNextEntityId () |
EntityPtr | findEntity (EntityId entityId, const ComponentModel::Entity *parent) const |
void | createEntityComponents (EntityDefinition &entityDefinition, const EntityPtr &entity) |
void | destroyEntityComponents (ComponentModel::Entity *entity) |
void | compileEntityDefinition (EntityDefinition &entityDefinition) |
void | storeNamedEntity (const EntityPtr &entity, StringView name) |
Private Attributes | |
std::unordered_map< size_t, EntityDefinition > | entityDefinitions |
std::unordered_map< Reflection::TypeId, ComponentCreator > | creators |
std::unordered_map< Reflection::TypeId, ComponentDestroyer > | destroyers |
std::unordered_map< Reflection::TypeId, ComponentSystemBase * > | systems |
Collections::Pool< ComponentModel::Entity > | entityPool |
Collections::Pool< EntityData > | entityDataPool |
std::unordered_map< EntityId, EntityPtr > | entities |
std::unordered_map< size_t, WeakEntityPtr > | entitiesByName |
Context * | context |
uint32_t | revision = 1 |
Stores top level entities for the engine.
The entity store stores all top level entities in a context instance and provides functionality for creating, destroying and connecting entities.
Definition at line 49 of file EntityStore.h.
Cogs::Core::EntityStore::EntityStore | ( | Context * | context | ) |
Construct the store with the given context instance.
context | Pointer to a valid context instance. |
Definition at line 30 of file EntityStore.cpp.
References Cogs::Core::createDefaultEntityDefinitions().
void Cogs::Core::EntityStore::addChild | ( | ComponentModel::Entity * | parent, |
const EntityPtr & | entity | ||
) |
Add a child to the given parent.
parent | Pointer to the parent entity. |
entity | Shared pointer to the child. THe parent will take shared ownership of the child entity. |
Definition at line 223 of file EntityStore.cpp.
References Cogs::Core::SceneComponent::children, Cogs::ComponentModel::Component::getComponent(), Cogs::ComponentModel::Entity::getComponent(), Cogs::ComponentModel::Entity::getId(), Cogs::ComponentModel::Entity::getName(), Cogs::Core::SceneComponent::parent, Cogs::Core::TransformComponent::parent, and Cogs::ComponentModel::Component::setChanged().
Referenced by Cogs::Core::OGC3DTilesSystem::addModelToScene(), and Cogs::Core::InstancedModelSystem::update().
void Cogs::Core::EntityStore::addChild | ( | EntityId | parent, |
EntityId | child | ||
) |
Definition at line 264 of file EntityStore.cpp.
|
inline |
Definition at line 338 of file EntityStore.h.
Cogs::ComponentModel::Component * Cogs::Core::EntityStore::addComponent | ( | ComponentModel::Entity * | entity, |
const StringView & | type | ||
) |
Definition at line 286 of file EntityStore.cpp.
Cogs::ComponentModel::Component * Cogs::Core::EntityStore::addComponent | ( | ComponentModel::Entity * | entity, |
Reflection::TypeId | typeId | ||
) |
Add a component of the given type to the entity.
entity | Pointer to an existing entity. |
typeId | Type id of the desired component type. |
Definition at line 269 of file EntityStore.cpp.
References Cogs::ComponentModel::Entity::addComponent(), and Cogs::ComponentModel::ComponentHandle::Empty().
Referenced by Cogs::Core::applyFieldValues().
void Cogs::Core::EntityStore::addEntityDefinition | ( | const EntityDefinition & | definition | ) |
Add the given entity definition to the store.
After adding a definition, entities with the same name as the definitions name can be created by calls to createEntity().
definition | Definition to use when users request entities of the type from the definitions name. |
Definition at line 59 of file EntityStore.cpp.
References Cogs::hash(), and Cogs::Core::EntityDefinition::name.
Referenced by Cogs::Core::createEntityDefinition(), Cogs::Core::CurtainView::CurtainViewExtension::initialize(), Cogs::Core::VectorField::VectorFieldExtension::initialize(), and Cogs::VideoExtension::VideoExtension::initialize().
void Cogs::Core::EntityStore::addSystem | ( | class ComponentSystemBase * | system | ) |
Adds the given system, creating creator and destroyer functions for the component type handled by the system and registering those using addSystem().
system | Pointer to a component system to add handling for. |
Definition at line 704 of file EntityStore.cpp.
References Cogs::Core::ComponentSystemBase::createComponent(), Cogs::Core::ComponentSystemBase::destroyComponent(), Cogs::Core::ComponentSystemBase::getComponentType(), and Cogs::Reflection::NoType.
void Cogs::Core::EntityStore::addSystem | ( | const Reflection::TypeId | typeId, |
ComponentCreator | creator, | ||
ComponentDestroyer | destroyer | ||
) |
Adds the given creator and destroyer functions for handling components of the given typeId.
The creator and destroyer functions are used when creating or destroying entities. The EntityDefinition used to create the entity contains a list of component types which will have their respective creators called.
Upon destruction, destroyers are called for all components in an Entity based on their types.
typeId | Type id for the type of Component these functions are used for. |
creator | Creator function used to instantiate components of the given type. |
destroyer | Destroyer function used to destruct components of the given type. |
Definition at line 698 of file EntityStore.cpp.
Referenced by Cogs::Core::Engine::registerSystem().
void Cogs::Core::EntityStore::clear | ( | ) |
Clear out all top level entities owned by the store.
This will in turn destroy all components held by top level entities which should lead to the deletion of all entities in the scene.
A check will be performed for components still alive after the execution of this method.
Definition at line 45 of file EntityStore.cpp.
|
private |
Definition at line 201 of file EntityStore.cpp.
Cogs::Core::EntityPtr Cogs::Core::EntityStore::createChildEntity | ( | const StringView & | type, |
ComponentModel::Entity * | parent, | ||
const StringView & | name = StringView() |
||
) |
Create a new Entity, parenting it to the given parent.
See createEntity() for details on Entity creation.
Parenting is performed by adding the newly created entity to the parents SceneComponent set of children. The TransformComponent of the child has its parent set to the TransformComponent of the parent.
type | String containing entity type name. |
parent | Pointer to a parent entity to add the child entity to. |
name | String containing an optional entity name. |
Definition at line 728 of file EntityStore.cpp.
Referenced by Cogs::Core::CreateEntityCommand::apply(), Cogs::Core::EchoSounder::UniformGridSystem::preUpdate(), Cogs::Core::VariableExtrusionSystem::update(), Cogs::Core::EchoSounder::SwathBottomSystem::update(), Cogs::Core::EchoSounder::SwathIsoSystem::update(), Cogs::Core::OceanSystem::update(), Cogs::Core::BasicOceanSystem::update(), and Cogs::Core::AdaptivePlanarGridSystem::update().
void Cogs::Core::EntityStore::createEntities | ( | size_t | count, |
std::vector< EntityPtr > & | entities | ||
) |
Allocates and initializes a collection of empty entities.
The entities have no global ownership stored, the caller must manage lifetime by handling EntityPtr instances locally.
count | Number of entities to allocate. |
entities | Collection to place owning pointers to the entities in. The collection will be cleared and resized to count. |
Definition at line 156 of file EntityStore.cpp.
References Cogs::ComponentModel::Entity::getUserData(), and Cogs::ComponentModel::Entity::setId().
Referenced by Cogs::Core::InstancedModelSystem::update().
Cogs::Core::EntityPtr Cogs::Core::EntityStore::createEntity | ( | const StringView & | name, |
const StringView & | type, | ||
bool | storeOwnership = true |
||
) |
Create a new Entity.
An entity is created using the given type name to lookup an entity definition to use as template for creation. If no EntityDefinition is found, the resulting entity will be empty.
If name is non-empty the entity will be given a name, and registered so that lookup using the name can be performed later. Registering two entities with the same name results in the second entity being impossible to retrieve by name at a later stage.
The parameter storeOwnership is used to determine if the entity store should treat the entity as a top level entity and keep an owning reference to the entity, thus preventing it from being deleted if the return value from this function is not kept alive.
name | String containing a name to give the entity, or an empty string. Non-empty strings can be used to look up the entity at a later time using the string. |
type | String containing an entity type name. This is used to lookup a corresponding entity definition. |
storeOwnership | If the entity should be considered top level and an owning reference kept by the store. |
Definition at line 104 of file EntityStore.cpp.
References Cogs::Core::applyFieldValues(), Cogs::ComponentModel::Entity::getUserData(), Cogs::hash(), Cogs::ComponentModel::Entity::setId(), and Cogs::StringView::size().
Referenced by Cogs::Core::OGC3DTilesSystem::addModelToScene(), Cogs::Core::CreateEntityCommand::apply(), and Cogs::Core::Scene::setup().
|
private |
Definition at line 184 of file EntityStore.cpp.
void Cogs::Core::EntityStore::destroyEntity | ( | const EntityId | id | ) |
Destroy the entity with the given id.
The Entity must be a top level entity owned by the entity store.
id | EntityId of the entity to destroy. |
Definition at line 407 of file EntityStore.cpp.
References Cogs::ComponentModel::Entity::getId(), Cogs::ComponentModel::Entity::getName(), Cogs::hash(), and Cogs::StringView::size().
Referenced by Cogs::Core::OGC3DTilesSystem::destroyComponent(), Cogs::Core::OGC3DTilesSystem::update(), and Cogs::Core::BasicOceanSystem::update().
|
private |
Definition at line 429 of file EntityStore.cpp.
std::string Cogs::Core::EntityStore::dumpHierarchy | ( | ) | const |
Generates a debug dump of the entire entity hierarchy.
Definition at line 637 of file EntityStore.cpp.
std::string Cogs::Core::EntityStore::dumpHierarchy | ( | const ComponentModel::Entity * | parent, |
const std::string & | prefix = "" |
||
) | const |
Generates a debug dump of the entity hierarchy from the specified parent entity.
Definition at line 647 of file EntityStore.cpp.
References Cogs::Core::SceneComponent::children, Cogs::ComponentModel::Entity::getComponent(), Cogs::ComponentModel::Entity::getComponents(), Cogs::Reflection::Name::getName(), Cogs::Reflection::Type::getName(), Cogs::ComponentModel::Entity::getName(), Cogs::Reflection::TypeDatabase::getType(), and Cogs::ComponentModel::ComponentCollectionBase::size().
Cogs::Core::EntityPtr Cogs::Core::EntityStore::findEntity | ( | const StringView & | name, |
const ComponentModel::Entity * | root = nullptr , |
||
EntityFind | findOptions = EntityFind::Default |
||
) | const |
Finds an entity with the given name.
Searches all entities in the context, using depth-first traversal of the trees formed by all root level entities in the context.
Optionally takes a root entity, constraining search to the trees formed by its descendants. The root is not included in the search.
Optionally takes a flag, determining whether the exact name string has to match or can be a substring of the name of the entity being compared against.
name | String name of the entity to find. |
root | Optional pointer to a root entity, limiting the search scope to the tree formed by its descendants. |
findOptions | Search options, defaults to EntityFind::Default. |
Definition at line 497 of file EntityStore.cpp.
References Cogs::Core::SceneComponent::children, Cogs::ComponentModel::Entity::getComponent(), Cogs::Core::NonRecursive, and Cogs::Core::PartialNameMatch.
Referenced by Cogs::Core::OGC3DTilesSystem::addModelToScene(), and Cogs::Core::applyFieldValues().
|
private |
Definition at line 570 of file EntityStore.cpp.
|
inline |
Return total number of entities allocated.
Definition at line 351 of file EntityStore.h.
|
inline |
Definition at line 363 of file EntityStore.h.
|
inline |
Definition at line 345 of file EntityStore.h.
|
inline |
Return map of entities with global ownership.
For debugging.
Definition at line 268 of file EntityStore.h.
Referenced by Cogs::Core::ExportCommand::apply(), Cogs::Core::ExportGltfCommand::apply(), Cogs::Core::Editor::hideUnselected(), Cogs::Core::Editor::invertVisibility(), and Cogs::Core::Editor::showAll().
void Cogs::Core::EntityStore::getEntitiesWithComponent | ( | std::vector< EntityId > & | entities, |
Reflection::TypeId | typeId | ||
) |
Definition at line 737 of file EntityStore.cpp.
Cogs::Core::EntityPtr Cogs::Core::EntityStore::getEntity | ( | const EntityId | entityId, |
bool | logIfNotFound = true , |
||
bool | onlyRegistered = false |
||
) | const |
Retrieve a reference to the shared entity pointer to the Entity with the given entityId.
This method will only find entities created by user. Use
Trying to get an entity which does not exist in the store is an error.
entityId | EntityId of the entity to look up. |
onlyRegistered | true if only searching registered entities in 'entities' map. Use getEntityPtr to find any entity if EntityPtr not required. |
Definition at line 449 of file EntityStore.cpp.
Cogs::Core::EntityPtr Cogs::Core::EntityStore::getEntity | ( | const StringView & | name, |
bool | logIfNotFound = true |
||
) | const |
Retrieve a reference to the shared entity pointer to the Entity with the given name.
Trying to get an entity which does not exist in the store is an error.
name | String name of the Entity to look up. |
logIfNotFound | true if method shall log if not found (default=true) |
Definition at line 472 of file EntityStore.cpp.
References Cogs::hash().
Referenced by Cogs::Core::PackMeshCommand::apply(), Cogs::Core::OGC3DTilesSystem::destroyComponent(), Cogs::Core::OGC3DTilesSystem::pruneTileCache(), Cogs::Core::Scene::setup(), Cogs::Core::VariableExtrusionSystem::update(), Cogs::Core::GhostSystem::update(), Cogs::Core::RigidBodySystem::update(), Cogs::Core::TriggerSystem::update(), and Cogs::Core::EchoSounder::SwathBottomSystem::update().
const Cogs::Core::EntityDefinition * Cogs::Core::EntityStore::getEntityDefinition | ( | const StringView & | name | ) | const |
Fetch the entity definition with the given name from the store.
If no such named definition is available, returns nullptr.
name | Name of the entity definition to retrieve. |
Definition at line 92 of file EntityStore.cpp.
References Cogs::hash().
Referenced by Cogs::Core::writeEntity().
const Cogs::Core::EntityDefinition * Cogs::Core::EntityStore::getEntityDefinition | ( | size_t | id | ) | const |
Definition at line 97 of file EntityStore.cpp.
void Cogs::Core::EntityStore::getEntityDefinitionComponents | ( | std::vector< StringView > & | components, |
const StringView & | name | ||
) | const |
Get components of a known entity definition.
Definition at line 80 of file EntityStore.cpp.
void Cogs::Core::EntityStore::getEntityDefinitions | ( | std::vector< StringView > & | names | ) | const |
Get names of currently known entity definitions.
Definition at line 71 of file EntityStore.cpp.
Cogs::ComponentModel::Entity * Cogs::Core::EntityStore::getEntityParent | ( | const ComponentModel::Entity * | entity | ) | const |
Gets the parent of the given entity.
Can only find parant for entities with TransformComponent, but all(?) entities with children (SceneComponent) also have xform.
entity | Entity to find parent of. |
Definition at line 591 of file EntityStore.cpp.
References Cogs::ComponentModel::Entity::getComponent(), Cogs::Core::TransformComponent::parent, and Cogs::ComponentModel::ComponentHandle::resolve().
Referenced by Cogs::Core::Editor::isUserRootEntity().
Cogs::ComponentModel::Entity * Cogs::Core::EntityStore::getEntityPtr | ( | const EntityId | entityId | ) |
Get a raw pointer to the entity with the given id.
entityId | EntityId of the entity to get a pointer to. |
Definition at line 681 of file EntityStore.cpp.
References Cogs::ComponentModel::Entity::getId().
Referenced by Cogs::Core::ExportCommand::apply(), Cogs::Core::ExportGltfCommand::apply(), Cogs::Core::SetFieldCommand< T >::apply(), Cogs::Core::EditorState::eraseInvalidSelected(), Cogs::Core::EditorState::getSelected(), Cogs::Core::Editor::hideUnselected(), Cogs::Core::Editor::selectFurthestAway(), Cogs::Core::Editor::showHide(), and Cogs::Core::EditorState::updateSelectedInfo().
|
private |
Definition at line 440 of file EntityStore.cpp.
|
inline |
Returns a number that changes every time an entity is created or destroyed.
Intent is to be used for detecting when cached scene structure needs to be updated.
Definition at line 358 of file EntityStore.h.
|
inline |
Check if the given entityId has global ownership in EntityStore.
entityPtr | Pointer to the entity to rename. |
entityId | EntityId of the entity. |
Definition at line 260 of file EntityStore.h.
Referenced by Cogs::Core::writeEntity().
void Cogs::Core::EntityStore::removeChild | ( | ComponentModel::Entity * | parent, |
const ComponentModel::Entity * | entity | ||
) |
Remove the parent-child relationship between parent and entity.
parent | A pointer to the current parent Entity of the given child entity. |
entity | A pointer to the child entity to remove from the parents list of children. |
Definition at line 307 of file EntityStore.cpp.
References Cogs::ComponentModel::ComponentHandle::Empty(), Cogs::ComponentModel::Entity::getComponent(), and Cogs::Core::TransformComponent::parent.
Referenced by Cogs::Core::EchoSounder::UniformGridSystem::preUpdate(), and Cogs::Core::OGC3DTilesSystem::update().
void Cogs::Core::EntityStore::removeChildren | ( | ComponentModel::Entity * | entity | ) |
Removes all children from the given entity.
entity | Pointer to a valid entity to remove all children from. |
Definition at line 344 of file EntityStore.cpp.
References Cogs::ComponentModel::ComponentHandle::Empty(), Cogs::ComponentModel::Component::getComponent(), Cogs::ComponentModel::Entity::getComponent(), Cogs::Core::SceneComponent::parent, and Cogs::Core::TransformComponent::parent.
Referenced by Cogs::Core::AssetSystem::update().
void Cogs::Core::EntityStore::removeComponent | ( | ComponentModel::Entity * | entity, |
ComponentModel::ComponentHandle | handle | ||
) |
Definition at line 291 of file EntityStore.cpp.
void Cogs::Core::EntityStore::renameEntity | ( | ComponentModel::Entity * | entityPtr, |
StringView | name | ||
) |
Rename the given entity.
The entity may have global ownership in EntityStore or be created in client collection using
Clients should call renameEntity instead of Entity::setName to ensure name lookup consistency.
entityPtr | Pointer to the entity to rename. |
name | New entity name. |
Definition at line 606 of file EntityStore.cpp.
References Cogs::StringView::empty(), Cogs::ComponentModel::Entity::getId(), Cogs::ComponentModel::Entity::getName(), Cogs::hash(), and Cogs::ComponentModel::Entity::setName().
void Cogs::Core::EntityStore::setEntityParent | ( | ComponentModel::Entity * | parent, |
ComponentModel::Entity * | child | ||
) |
Move Root entity to a parent: Equal to addChild(parent, child) + destroyEntity(child) Move Child entity to root: Removes child from old parent.
Adds child to global entities (+name lookup if unique). Move Child entity to other parent: Removes child from old parent. Adds child to new parent.
Definition at line 377 of file EntityStore.cpp.
References Cogs::ComponentModel::Entity::getId(), and Cogs::ComponentModel::Entity::getName().
|
private |
Definition at line 745 of file EntityStore.cpp.
|
private |
Definition at line 389 of file EntityStore.h.
|
private |
Definition at line 380 of file EntityStore.h.
|
private |
Definition at line 381 of file EntityStore.h.
|
private |
Definition at line 386 of file EntityStore.h.
|
private |
Definition at line 387 of file EntityStore.h.
|
private |
Definition at line 385 of file EntityStore.h.
|
private |
Definition at line 378 of file EntityStore.h.
|
private |
Definition at line 384 of file EntityStore.h.
|
private |
Definition at line 390 of file EntityStore.h.
|
private |
Definition at line 382 of file EntityStore.h.