Cogs.Core
EntityStore.h
1#pragma once
2
3#include "EntityDefinition.h"
4#include "ComponentFunctions.h"
5
6#include "Foundation/Collections/Pool.h"
7#include "Foundation/ComponentModel/Component.h"
8#include "Foundation/ComponentModel/Entity.h"
9#include "Foundation/Reflection/Type.h"
10
11#include <unordered_map>
12
13namespace Cogs
14{
15 namespace Core
16 {
17 class ContextBase;
18 class Context;
19
24 {
26 size_t templateId = 0;
30 uint32_t flags = 0;
31 };
32
34 enum class EntityFind {
35 Default = 1,
36 PartialNameMatch = 1 << 1,
37 NonRecursive = 1 << 2,
38 // Insert new flags here
39 MaxFlag = 1 << 7,
40 };
41 ENABLE_ENUM_FLAGS(EntityFind);
42
49 class COGSCORE_DLL_API EntityStore
50 {
51 public:
57 EntityStore(Context * context);
58
67 void clear();
68
77 void addEntityDefinition(const EntityDefinition & definition);
78
88 const EntityDefinition * getEntityDefinition(const StringView & name) const;
89 const EntityDefinition * getEntityDefinition(size_t id) const;
90
94 void getEntityDefinitions(std::vector<StringView>& names) const;
95
99 void getEntityDefinitionComponents(std::vector<StringView>& components, const StringView & name) const;
100
122 EntityPtr createEntity(const StringView & name, const StringView & type, bool storeOwnership = true);
123
134 void createEntities(size_t count, std::vector<EntityPtr> & entities);
135 void createEntities(size_t count, std::vector<EntityPtr> & entities, std::function<void(ComponentModel::Entity *)> destructor);
136
153 EntityPtr createChildEntity(const StringView & type, ComponentModel::Entity * parent, const StringView & name = StringView());
154
161 void removeChild(ComponentModel::Entity * parent, const ComponentModel::Entity * entity);
162
168 void removeChildren(ComponentModel::Entity * entity);
169
175 void setEntityParent(ComponentModel::Entity* parent, ComponentModel::Entity* child);
176
184 void destroyEntity(const EntityId id);
185
196 EntityPtr getEntity(const StringView & name, bool logIfNotFound = true) const;
197
217 EntityPtr findEntity(const StringView & name, const ComponentModel::Entity * root = nullptr, EntityFind findOptions = EntityFind::Default) const;
218
226 Cogs::ComponentModel::Entity* getEntityParent(const ComponentModel::Entity* entity) const;
227
241 EntityPtr getEntity(const EntityId entityId, bool logIfNotFound = true, bool onlyRegistered = false) const;
242
253 void renameEntity(ComponentModel::Entity* entityPtr, StringView name);
254
260 bool hasEntityOwnership(const EntityId entityId) const
261 {
262 return entities.find(entityId) != entities.end();
263 }
264
268 const std::unordered_map<EntityId, EntityPtr> & getEntities() const
269 {
270 return entities;
271 }
272
280 ComponentModel::Entity* getEntityPtr(const EntityId entityId);
281
287 std::string dumpHierarchy() const;
288
294 std::string dumpHierarchy(const ComponentModel::Entity* parent, const std::string& prefix = "") const;
295
309 void addSystem(const Reflection::TypeId typeId, ComponentCreator creator, ComponentDestroyer destroyer);
310
317 void addSystem(class ComponentSystemBase * system);
318
325 void addChild(ComponentModel::Entity * parent, const EntityPtr & entity);
326 void addChild(EntityId parent, EntityId child);
327
335 ComponentModel::Component * addComponent(ComponentModel::Entity * entity, const StringView & type);
336
337 template<typename ComponentType>
338 ComponentType * addComponent(ComponentModel::Entity * entity)
339 {
340 return static_cast<ComponentType *>(addComponent(entity, Cogs::Reflection::TypeDatabase::getType<ComponentType>().getTypeId()));
341 }
342
343 void removeComponent(ComponentModel::Entity * entity, ComponentModel::ComponentHandle handle);
344
345 const std::unordered_map<size_t, EntityDefinition> & getDefinitions() const
346 {
347 return entityDefinitions;
348 }
349
351 size_t getAllocatedEntityCount() const { return entityDataPool.size(); }
352
358 uint32_t getRevision() const { return revision; }
359
360
361 void getEntitiesWithComponent(std::vector<EntityId>& entities, Reflection::TypeId typeId);
362
363 Context * getContext() const { return context; }
364
365 private:
366 EntityId getNextEntityId();
367
368 EntityPtr findEntity(EntityId entityId, const ComponentModel::Entity* parent) const;
369
370 void createEntityComponents(EntityDefinition & entityDefinition, const EntityPtr & entity);
371 void destroyEntityComponents(ComponentModel::Entity * entity);
372
373 void compileEntityDefinition(EntityDefinition & entityDefinition);
374
375 void storeNamedEntity(const EntityPtr& entity, StringView name);
376 private:
377
378 std::unordered_map<size_t, EntityDefinition> entityDefinitions;
379
380 std::unordered_map<Reflection::TypeId, ComponentCreator> creators;
381 std::unordered_map<Reflection::TypeId, ComponentDestroyer> destroyers;
382 std::unordered_map<Reflection::TypeId, ComponentSystemBase*> systems;
383
385 Collections::Pool<EntityData> entityDataPool;
386 std::unordered_map<EntityId, EntityPtr> entities;
387 std::unordered_map<size_t, WeakEntityPtr> entitiesByName;
388
389 Context * context;
390 uint32_t revision = 1;
391 };
392 }
393}
Base class for Component instances.
Definition: Component.h:143
Container for components, providing composition of dynamic entities.
Definition: Entity.h:18
Base class for component systems.
Context base contains the parts of a Context that may be instantiated by static scene or model instan...
Definition: Context.h:60
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Stores top level entities for the engine.
Definition: EntityStore.h:50
bool hasEntityOwnership(const EntityId entityId) const
Check if the given entityId has global ownership in EntityStore.
Definition: EntityStore.h:260
size_t getAllocatedEntityCount() const
Return total number of entities allocated.
Definition: EntityStore.h:351
uint32_t getRevision() const
Returns a number that changes every time an entity is created or destroyed.
Definition: EntityStore.h:358
const std::unordered_map< EntityId, EntityPtr > & getEntities() const
Return map of entities with global ownership.
Definition: EntityStore.h:268
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
std::shared_ptr< ComponentModel::Entity > EntityPtr
Smart pointer for Entity access.
Definition: EntityPtr.h:12
EntityFind
Options to findEntity/getEntityFull.
Definition: EntityStore.h:34
@ NonRecursive
Do not search recursively.
@ MaxFlag
Do not go beyond this flag. Used as bounding value.
@ PartialNameMatch
Search for the first entity that partially contains the specified name.
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Pool used to store elements of ElementType.
Definition: Pool.h:17
Handle to a Component instance.
Definition: Component.h:67
Holds extra housekeeping data for an Entity instance.
Definition: EntityStore.h:24
ContextBase * entityContext
Pointer to the ContextBase the entity belongs to.
Definition: EntityStore.h:28
uint32_t flags
Entity behavior flags.
Definition: EntityStore.h:30
size_t templateId
Contains the hashed name of a template or entity definition (if any) used to create the instance.
Definition: EntityStore.h:26
Defines how to construct entities of a certain type by a list of components to instantiate and defaul...