1 /* 2 * Copyright (c) 2022 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() 36 class EntityReferenceCounter; 37 class EntityManager final : public IEntityManager { 38 public: 39 EntityManager(); 40 explicit EntityManager(const size_t entityCount); 41 ~EntityManager() override; 42 43 Entity Create() override; 44 EntityReference CreateReferenceCounted() override; 45 EntityReference GetReferenceCounted(Entity entity) override; 46 void Destroy(const Entity entity) override; 47 void DestroyAllEntities() override; 48 49 bool IsAlive(const Entity entity) const override; 50 51 uint32_t GetGenerationCounter() const override; 52 53 Iterator::Ptr Begin(IteratorType type) const override; 54 Iterator::Ptr End(IteratorType type) const override; 55 56 void SetActive(const Entity entity, bool state) override; 57 58 BASE_NS::vector<Entity> GetRemovedEntities(); 59 BASE_NS::vector<BASE_NS::pair<Entity, EventType>> GetEvents(); 60 61 // Marks all entities with zero refcnt as DEAD. 62 void UpdateDeadEntities(); 63 64 private: 65 struct EntityState { 66 enum class State : uint32_t { 67 // Entity is ready for re-use. 68 FREE = 0, 69 // Entity is alive and active. 70 ALIVE = 1, 71 // Entity is alive and de-activated. 72 INACTIVE = 2, 73 // Entity is destroyed and waiting for GC 74 DEAD = 3, 75 } state { State::FREE }; 76 uint32_t generation { 0U }; 77 BASE_NS::refcnt_ptr<EntityReferenceCounter> counter; 78 }; 79 BASE_NS::vector<EntityState> entities_; 80 BASE_NS::vector<Entity> removedList_; 81 BASE_NS::vector<uint32_t> freeList_; 82 BASE_NS::vector<BASE_NS::pair<Entity, IEntityManager::EventType>> eventList_; 83 uint32_t generationCounter_ { 0 }; 84 85 class IteratorImpl final : public Iterator { 86 const EntityManager* owner_ { nullptr }; 87 uint32_t index_ { 0 }; 88 IteratorType type_; 89 90 public: 91 IteratorImpl(const EntityManager&, size_t, IteratorType type); 92 const class IEntityManager* GetOwner() const override; 93 bool Compare(const Iterator::Ptr&) const override; 94 bool Next() override; 95 Entity Get() const override; 96 Iterator::Ptr Clone() const override; 97 }; 98 Iterator::Ptr MakeIterator(uint32_t index, IteratorType type) const; 99 }; 100 CORE_END_NAMESPACE() 101 102 #endif // CORE_ECS_ENTITY_MANAGER_H 103