EntityManager.ixx File
Central manager for entity lifecycle and component storage. More...
Included Headers
#include <memory>
#include <vector>
#include <cstddef>
#include <cassert>
#include "helios-ecs-config.h"
#include <helios.ecs.types>
#include <helios.ecs.components>
#include <helios.ecs.ComponentOpsRegistry>
#include <helios.ecs.concepts.Traits>
#include <helios.ecs.EntityRegistry>
#include <helios.ecs.SparseSet>
Namespaces Index
| namespace | helios |
| namespace | ecs |
Classes Index
| class | EntityManager<THandle, TEntityRegistry, TCapacity> |
|
Manages entities and their associated components. More... | |
Description
Central manager for entity lifecycle and component storage.
File Listing
The file content with the documentation metadata removed is:
12#include "helios-ecs-config.h"
26using namespace helios::ecs::types;
27using namespace helios::ecs::components;
28using namespace helios::ecs::concepts;
29export namespace helios::ecs {
78 class EntityManager {
84 using EntityRegistry_type = TEntityRegistry;
86 using Handle_type = THandle;
88 using StrongId_type = Handle_type::StrongId_type;
97 EntityManager(const EntityManager&) = delete;
98 EntityManager& operator=(const EntityManager&) = delete;
103 EntityManager(EntityManager&&) noexcept = default;
108 EntityManager& operator=(EntityManager&&) noexcept = default;
120 explicit EntityManager(const size_t capacity = TCapacity)
121 : registry_(EntityRegistry_type{capacity}), capacity_(capacity) {
134 [[nodiscard]] Handle_type create(StrongId_type strongId = StrongId_type{}) {
145 [[nodiscard]] bool isValid(const Handle_type handle) const noexcept {
146 return registry_.isValid(handle);
157 return registry_.isValid(handle(entityId));
171 [[nodiscard]] bool destroy(const Handle_type handle) {
173 if (!registry_.isValid(handle)) {
182 const auto typeId = ComponentTypeId_type{i};
184 if (const auto& ops = ComponentOpsRegistry_type::ops(typeId); ops.onRemove) {
189 components_[i]->remove(handle.entityId);
192 registry_.destroy(handle);
208 [[nodiscard]] T* get(const Handle_type handle) const {
209 if (!has<T>(handle)) {
213 const auto entityId = handle.entityId;
216 auto* sparseSet = static_cast<SparseSet<T>*>(components_[typeId].get());
229 [[nodiscard]] SparseSet<T>* sparseSet() {
237 return static_cast<SparseSet<T>*>(components_[typeId].get());
248 [[nodiscard]] const SparseSet<T>* sparseSet() const {
256 return static_cast<SparseSet<T>*>(components_[typeId].get());
270 [[nodiscard]] bool has(const Handle_type handle) const {
271 if (!registry_.isValid(handle)) {
278 return components_[typeId]->contains(handle.entityId);
292 [[nodiscard]] bool has(const Handle_type handle,
293 const ComponentTypeId_type typeId) const {
294 if (!registry_.isValid(handle)) {
298 const auto tvalue = typeId.value();
301 return components_[tvalue]->contains(handle.entityId);
317 [[nodiscard]] SparseSet<TComponent>* ensureSparseSet() {
329 return static_cast<SparseSet<TComponent>*>(components_[typeId].get());
350 T* emplace(const Handle_type handle, Args&& ...args) {
352 if (!registry_.isValid(handle)) {
356 const auto entityId = handle.entityId;
360 auto* sparseSet = ensureSparseSet<T>();
362 if (sparseSet->contains(entityId)) {
366 return sparseSet->emplace(entityId, std::forward<Args>(args)...);
382 T* emplaceOrGet(const Handle_type handle, Args&& ...args) {
384 if (!registry_.isValid(handle)) {
390 if (!raw) {
391 return get<T>(handle);
394 return raw;
415 [[nodiscard]] bool remove(const Handle_type& handle) {
417 if (!has<T>(handle)) {
426 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
432 return components_[typeId.value()]->remove(handle.entityId);
443 [[nodiscard]] bool managesDirty() {
458 void trackDirty() {
496 void clearAllDirtySets() {
513 void clearDirtySet() {
534 void forEachComponentTypeId(const Handle_type handle, TFunc&& func) const {
535 if (!registry_.isValid(handle)) {
540 if (components_[i] && components_[i]->contains(handle.entityId)) {
541 std::forward<TFunc>(func)(ComponentTypeId_type{i});
556 void clone(const Handle_type source, const Handle_type target) {
563 [&](const ComponentTypeId_type typeId) {
564 if (!has(target, typeId)) {
566 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
567 const void* sourceCmp = raw(source, typeId);
586 [[nodiscard]] void* raw(const Handle_type handle, const ComponentTypeId_type typeId ) const {
601 [[nodiscard]] Handle_type handle(const EntityId entityId) const {
602 return Handle_type{entityId, registry_.version(entityId), registry_.strongId(entityId)};
621 EntityRegistry_type registry_;
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.