EntityManager.ixx File
Central manager for entity lifecycle and component storage. More...
Included Headers
#include <memory>
#include <vector>
#include <cstddef>
#include <helios.ecs.concepts.Traits>
#include <helios.ecs.ComponentOpsRegistry>
#include <helios.ecs.types>
#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:
22using namespace helios::ecs::types;
24export namespace helios::ecs {
73 class EntityManager {
78 using EntityRegistry_type = TEntityRegistry;
79 using Handle_type = THandle;
80 using StrongId_type = Handle_type::StrongId_type;
87 EntityManager(const EntityManager&) = delete;
88 EntityManager& operator=(const EntityManager&) = delete;
90 EntityManager(EntityManager&&) noexcept = default;
91 EntityManager& operator=(EntityManager&&) noexcept = default;
102 explicit EntityManager(const size_t capacity = TCapacity)
103 : registry_(EntityRegistry_type{capacity}), capacity_(capacity)
116 explicit EntityManager(
117 EntityRegistry_type& registry,
131 [[nodiscard]] Handle_type create(StrongId_type strongId = StrongId_type{}) {
142 [[nodiscard]] bool isValid(const Handle_type handle) const noexcept {
143 return registry_.isValid(handle);
154 return registry_.isValid(handle(entityId));
168 [[nodiscard]] bool destroy(const Handle_type handle) {
170 if (!registry_.isValid(handle)) {
179 const auto typeId = ComponentTypeId_type{i};
181 if (const auto& ops = ComponentOpsRegistry_type::ops(typeId); ops.onRemove) {
186 components_[i]->remove(handle.entityId);
189 registry_.destroy(handle);
205 [[nodiscard]] T* get(const Handle_type handle) const {
206 if (!has<T>(handle)) {
210 const auto entityId = handle.entityId;
213 auto* sparseSet = static_cast<SparseSet<T>*>(components_[typeId].get());
215 return sparseSet->get(entityId);
226 [[nodiscard]] SparseSet<T>* getSparseSet() {
234 return static_cast<SparseSet<T>*>(components_[typeId].get());
245 [[nodiscard]] const SparseSet<T>* getSparseSet() const {
253 return static_cast<SparseSet<T>*>(components_[typeId].get());
267 [[nodiscard]] bool has(const Handle_type handle) const {
268 if (!registry_.isValid(handle)) {
275 return components_[typeId]->contains(handle.entityId);
289 [[nodiscard]] bool has(const Handle_type handle,
290 const ComponentTypeId_type typeId) const {
291 if (!registry_.isValid(handle)) {
295 const auto tvalue = typeId.value();
298 return components_[tvalue]->contains(handle.entityId);
312 void enable(const Handle_type handle, const ComponentTypeId_type typeId) const {
324 void disable(const Handle_type handle, const ComponentTypeId_type typeId) const {
335 void enable(const Handle_type handle, const ComponentTypeId_type typeId, const bool enable) const {
345 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
347 if (enable && ops.enable) {
349 } else if (!enable && ops.disable) {
372 T* emplace(const Handle_type handle, Args&& ...args) {
374 if (!registry_.isValid(handle)) {
378 const auto entityId = handle.entityId;
390 auto* sparseSet = static_cast<SparseSet<T>*>(components_[typeId].get());
396 return sparseSet->emplace(entityId, std::forward<Args>(args)...);
412 T* emplaceOrGet(const Handle_type handle, Args&& ...args) {
414 if (!registry_.isValid(handle)) {
420 if (!raw) {
421 return get<T>(handle);
424 return raw;
445 bool remove(const Handle_type& handle) {
447 if (!has<T>(handle)) {
456 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
462 return components_[typeId.value()]->remove(handle.entityId);
471 void forEachComponentTypeId(const Handle_type handle, TFunc&& func) const {
472 if (!registry_.isValid(handle)) {
477 if (components_[i] && components_[i]->contains(handle.entityId)) {
478 std::forward<TFunc>(func)(ComponentTypeId_type{i});
493 void clone(const Handle_type source, const Handle_type target) {
500 [&](const ComponentTypeId_type typeId) {
501 if (!has(target, typeId)) {
503 const auto& ops = ComponentOpsRegistry_type::ops(typeId);
504 const void* sourceCmp = raw(source, typeId);
523 [[nodiscard]] void* raw(const Handle_type handle, const ComponentTypeId_type typeId ) const {
538 [[nodiscard]] Handle_type handle(const EntityId entityId) const {
539 return Handle_type{entityId, registry_.version(entityId), registry_.strongId(entityId)};
553 EntityRegistry_type registry_;
Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.