1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef CORE_ECS_ENTITY_MANAGER_H 17 #define CORE_ECS_ENTITY_MANAGER_H 18 19 #include <cstddef> 20 #include <cstdint> 21 22 #include <base/containers/generic_iterator.h> 23 #include <base/containers/vector.h> 24 #include <base/namespace.h> 25 #include <core/ecs/entity.h> 26 #include <core/ecs/entity_reference.h> 27 #include <core/ecs/intf_entity_manager.h> 28 #include <core/namespace.h> 29 30 BASE_BEGIN_NAMESPACE() 31 template<class T1, class T2> 32 struct pair; 33 BASE_END_NAMESPACE() 34 35 CORE_BEGIN_NAMESPACE() class EntityManager final : public IEntityManager { 36 public: 37 EntityManager(); 38 explicit EntityManager(const size_t entityCount); 39 ~EntityManager() override; 40 41 Entity Create() override; 42 EntityReference CreateReferenceCounted() override; 43 EntityReference GetReferenceCounted(Entity entity) override; 44 void Destroy(const Entity entity) override; 45 void DestroyAllEntities() override; 46 47 bool IsAlive(const Entity entity) const override; 48 49 uint32_t GetGenerationCounter() const override; 50 51 Iterator::Ptr Begin(IteratorType type) const override; 52 Iterator::Ptr End(IteratorType type) const override; 53 54 void SetActive(const Entity entity, bool state) override; 55 56 BASE_NS::vector<Entity> GetRemovedEntities(); 57 BASE_NS::vector<BASE_NS::pair<Entity, EventType>> GetEvents(); 58 59 // Marks all entities with zero refcnt as DEAD. 60 void UpdateDeadEntities(); 61 62 private: 63 struct EntityState { 64 enum class State : uint32_t { 65 /* Entity is ready for re-use. 66 */ 67 FREE = 0, 68 /* Entity is alive and active. 69 */ 70 ALIVE = 1, 71 /* Entity is alive and de-activated. 72 */ 73 INACTIVE = 2, 74 /* Entity is destroyed and waiting for GC 75 */ 76 DEAD = 3, 77 } state { 0 }; 78 uint32_t generation { 0 }; 79 IEntityReferenceCounter::Ptr counter; 80 }; 81 BASE_NS::vector<EntityState> entities_; 82 BASE_NS::vector<Entity> removedList_; 83 BASE_NS::vector<uint32_t> freeList_; 84 BASE_NS::vector<BASE_NS::pair<Entity, IEntityManager::EventType>> eventList_; 85 uint32_t generationCounter_ { 0 }; 86 87 class IteratorImpl final : public Iterator { 88 const EntityManager* owner_ { nullptr }; 89 uint32_t index_ { 0 }; 90 IteratorType type_; 91 92 public: 93 IteratorImpl(const EntityManager&, size_t, IteratorType type); 94 const class IEntityManager* GetOwner() const override; 95 bool Compare(const Iterator::Ptr&) const override; 96 bool Next() override; 97 Entity Get() const override; 98 Iterator::Ptr Clone() const override; 99 }; 100 Iterator::Ptr MakeIterator(uint32_t index, IteratorType type) const; 101 }; 102 CORE_END_NAMESPACE() 103 104 #endif // CORE_ECS_ENTITY_MANAGER_H 105