CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
EntityManager.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Forward.hpp"
11
12#include "Component.hpp"
13#include "ComponentStore.hpp"
14#include "EntityID.hpp"
15
17
25
26#include <bitset>
27
28namespace CeresEngine {
29
30 enum class EntityDirtyBit {
31 Name = 1u << 0u,
32 Parent = 1u << 1u,
33 Children = 1u << 2u,
34 Component = 1 << 3u,
35 };
36
39
45
48
50 template<typename C> friend class TComponentType;
51
52 private:
56
60
63
65 struct EntityMetadata;
66
69
72
75
76 private:
78 World* mWorld = nullptr;
79
83
87
88 static const Entity nullEntity;
89
90 public:
94
98
102
105
108
111
112 public:
115 void update();
116
117 public: // Entity management
120 [[nodiscard]] EntityID allocate(const Entity& parent = nullEntity, String name = {});
121
122 // /// Allocates a new entity ID.
123 // /// \return The allocated entity ID
124 // [[nodiscard]] CE_SCRIPT_EXPORT()
125 // EntityID allocate(String name) { return allocate(nullEntity, std::move(name)); }
126
131
134 [[nodiscard]] Entity create(const Entity& parent = nullEntity, String name = {});
135
136 // /// Creates a new entity
137 // /// \return The created entity
138 // [[nodiscard]] CE_SCRIPT_EXPORT()
139 // Entity create(String name) { return create(nullEntity, std::move(name)); }
140
145
150 Entity get(EntityID entityID);
151
157
161 void destroy(EntityID entityID);
162
167
172 bool valid(EntityID entityID) noexcept;
173
176 void clear();
177
178 private: // Entity-Component interface
182
193 template<CComponent C, typename... Args>
194 inline C& createComponent(EntityID entityID, Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>);
195
204 AbstractComponent& createComponent(EntityID entityID, const ComponentType& type, const Box& initialValue = nullptr);
205
217 template<CComponent C, typename... Args>
218 inline C& setComponent(EntityID entityID, Args&&... args) noexcept(std::is_nothrow_constructible_v<C, Args...>);
219
226
232 void removeComponent(EntityID entityID, const ComponentType& type);
233
240 template<CComponent C> [[nodiscard]] inline C& getComponent(EntityID entityID) noexcept;
241
248 [[nodiscard]] AbstractComponent& getComponent(EntityID entityID, const ComponentType& type);
249
250 // TODO Write docs.
251 [[nodiscard]] Vector<AbstractComponent*> getComponents(EntityID entityID);
252
258 template<CComponent... Cs> [[nodiscard]] bool hasComponents(EntityID entityID) const noexcept;
259
264 [[nodiscard]] bool hasComponents(EntityID entityID, ComponentMask mask) const noexcept;
265
269 void clear(EntityID entityID);
270
275 [[nodiscard]] bool empty(EntityID entityID) noexcept;
276
277 public: // Entity Objects
286 template<typename E, typename... Args> [[nodiscard]] inline E create(Args&&... args);
287
300 template<typename E, typename Block, typename... Args> inline E createWith(Block&& block, Args&&... args);
301
302 private: // Entity Objects interface
309
315
323
324 private: // Parent & Children entities
328 [[nodiscard]] Entity getParent(EntityID entityID) const noexcept;
329
333 void setParent(EntityID entityID, Entity parent);
334
338 [[nodiscard]] const Vector<Entity>& getChildren(EntityID entityID) const noexcept;
339
340 private: // Naming
345
349 void setName(EntityID entityID, String name);
350
351 public: // Listeners
355
359
360 private:
361 void triggerComponentListeners(EntityID entityID, ComponentMask oldMask, ComponentMask newMask) noexcept;
362
363 public: // Querying
369 public:
371 using Predicate = P;
372
373 private:
376
377 public:
381
382 public:
386 Generator<const Entity> operator()(const EntityManager& entityManager) const noexcept;
387 };
388
394
402
413
419
420 public: // Component changes
421 private:
423 template<CComponent C, typename T> void markComponentFieldAsDirty(EntityID entityID, T C::*fieldPointer);
424
428 void markComponentFieldAsDirty(EntityID entityID, const ComponentType& componentType, const ClassProperty* property);
429
431 void markEntityAsDirty(EntityID entityID, EntityDirtyFlags flags);
432
433 public:
435 [[nodiscard]] World& getWorld() noexcept { return *mWorld; }
436
438 [[nodiscard]] const World& getWorld() const noexcept { return *mWorld; }
439
441 [[nodiscard]] CE_SCRIPT_EXPORT(property = "getter", name = "eventManager")
443
446
448 [[nodiscard]] CE_SCRIPT_EXPORT(property = "getter", name = "actionManager")
450
453
454 private: // Entity Metadata
455 [[nodiscard]] EntityMetadata& metadata(EntityID entityID);
456
457 [[nodiscard]] EntityMetadata* metadataIf(EntityID entityID) noexcept;
458
460
461 private:
467
473
479 [[nodiscard]] AbstractComponentStore& getComponentStore(ComponentID componentID) noexcept;
480 };
481
482} // namespace CeresEngine
483
484#include "EntityManager.inl"
#define CE_FLAGS_OPERATORS(Enum)
Defines global operators for a Flags<Enum, Storage> implementation.
Definition Flags.hpp:216
#define CE_SCRIPT_EXPORT(...)
The CE_SCRIPT_EXPORT macro marks a class or method as exportable and available in scripting environme...
Definition Macros.hpp:247
The component store class is responsible for managing and organizing component data storage in memory...
Definition ComponentStore.hpp:28
A value type that can hold any alongside it's type information.
Definition Box.hpp:40
Represents a reflected property from metadata defined by the class.
Definition Class.hpp:176
A object that manages a set of components masks.
Definition Component.hpp:238
A type-safe component store implementation.
Definition ComponentStore.hpp:135
A type that describes and provides type-erased operations on a component.
Definition Component.hpp:456
Definition ActionManager.hpp:26
Definition EventManager.hpp:24
The base entity class.
Definition Entity.hpp:41
Definition EntityID.hpp:76
The query executor is responsible for iterating over entities and filtering them based on a given pre...
Definition EntityManager.hpp:368
QueryExecutor(Predicate &&predicate)
Creates a new query executor instance.
P Predicate
The predicate type.
Definition EntityManager.hpp:371
Generator< const Entity > operator()(const EntityManager &entityManager) const noexcept
Executes the query.
Predicate mPredicate
The predicate to filter selected entities.
Definition EntityManager.hpp:375
Definition EntityManager.hpp:49
HashMap< ComponentMask, Vector< AbstractComponentListener * > > mComponentListeners
A set of listeners for component changes.
Definition EntityManager.hpp:71
ComponentMaskSet mComponentMasks
A vector of component masks for every known entity, indexed by their entity indexes.
Definition EntityManager.hpp:59
Vector< UPtr< AbstractComponentStore > > mComponentStores
A vector containing all component stores indexed by their component IDs.
Definition EntityManager.hpp:55
Entity create(const Entity &parent=nullEntity, String name={})
Creates a new entity.
Map< EntityIndex, EntityChangeSet > mDirtyEntities
A list of entities whose components have been recently dirtied.
Definition EntityManager.hpp:74
static const Entity nullEntity
Definition EntityManager.hpp:88
Generator< const Entity > each() const noexcept
Creates a generator that iterates over all entities in the entity manager.
EntityEventManager & mEventManager
The event manager that should receive notifications from the EntityManager.
Definition EntityManager.hpp:82
const World & getWorld() const noexcept
Definition EntityManager.hpp:438
Vector< EntityID > allocate(EntityIndex n, const Entity &parent=nullEntity)
Allocates n new entity IDs.
EntityIDAllocator mAllocator
The entity ID allocator.
Definition EntityManager.hpp:62
Vector< Entity > create(EntityIndex n, const Entity &parent=nullEntity)
Creates n new entities.
EntityActionManager * mActionManager
The event manager that should receive notifications from the EntityManager.
Definition EntityManager.hpp:86
Vector< EntityMetadata > mEntityMetadata
A vector containing metadata for an allocated entity.
Definition EntityManager.hpp:68
A type-safe entity type.
Definition Entity.hpp:538
Definition EntityQuery.hpp:16
A generator represents a coroutine type that produces a sequence of values of type T,...
Definition Generator.hpp:50
An implementation of the ComponentType interface that implements type-erased operations for component...
Definition Component.hpp:529
Definition World.hpp:18
Definition Component.hpp:117
Definition Application.hpp:19
decltype(auto) get(BezierPath::Element &element) noexcept
Decomposes a bezier path element.
Definition BezierPath.hpp:723
auto with(Executor executor)
Definition ExecutionContext.hpp:546
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::unordered_map< Key, T, Hash, KeyEqual, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > HashMap
HashMap is an associative container that contains key-value pairs with unique keys.
Definition Map.hpp:33
ComponentDirtyBit
Definition EntityManager.hpp:40
std::bitset< 128 > ComponentMask
A bitset that represents a components mask (i.e. a set of components)
Definition Component.hpp:35
auto parent(const Entity &parent)
Sets the entity parent.
Definition Helpers.hpp:52
constexpr auto predicate(Callable &&callable) noexcept
Helper function to create a new predicate from a lambda.
Definition Predicate.hpp:390
EntityDirtyBit
Definition EntityManager.hpp:30
void copy(const A &a, B &b, T &&t=T())
Copies values from one container to another.
Definition Iterator.hpp:564
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
unsigned int ComponentID
A numeric type that represents a component.
Definition Component.hpp:32
std::map< Key, T, Compare, ScopedAllocatorAdaptor< StdAllocator< Pair< const Key, T >, RawAllocator > > > Map
Map is a sorted associative container that contains key-value pairs with unique keys.
Definition Map.hpp:24
UInt32 EntityIndex
Definition EntityID.hpp:22
Definition Span.hpp:668
A abstract class that provides a trait that allows checking for component implementations.
Definition Component.hpp:39
An abstract type that represents a generic component listener.
Definition Component.hpp:390
Definition NamedType.hpp:237
Definition Forward.hpp:28
Components serve as the base for data storage for an entity.
Definition Component.hpp:68
Definition EntityID.hpp:25
Wrapper around an enum that allows simple use of bitwise logic operations.
Definition Flags.hpp:19
A type that predicate types must extend to allow automatic operator overloading.
Definition Predicate.hpp:19