1 /*
2 * Copyright (C) 2023 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 <base/containers/unordered_map.h>
20 #include <core/ecs/intf_entity_manager.h>
21 #include <core/namespace.h>
22
CORE_BEGIN_NAMESPACE()23 CORE_BEGIN_NAMESPACE()
24 class EntityManager final : public IEntityManager {
25 public:
26 EntityManager();
27 explicit EntityManager(const size_t entityCount);
28 ~EntityManager() override;
29
30 Entity Create() override;
31 EntityReference CreateReferenceCounted() override;
32 EntityReference GetReferenceCounted(Entity entity) override;
33 void Destroy(const Entity entity) override;
34 void DestroyAllEntities() override;
35
36 bool IsAlive(const Entity entity) const override;
37 BASE_NS::vector<Entity> GetAddedEntities() override;
38 BASE_NS::vector<Entity> GetRemovedEntities() override;
39 BASE_NS::vector<BASE_NS::pair<Entity, EventType>> GetEvents() override;
40
41 uint32_t GetGenerationCounter() const override;
42
43 Iterator::Ptr Begin(IteratorType type) const override;
44 Iterator::Ptr End(IteratorType type) const override;
45
46 void SetActive(const Entity entity, bool state) override;
47
48 // Marks all entities with zero refcnt as DEAD.
49 void UpdateDeadEntities();
50
51 private:
52 struct EntityState {
53 enum class State : uint32_t {
54 /* Entity is ready for re-use.
55 */
56 FREE = 0,
57 /* Entity is alive and active.
58 */
59 ALIVE = 1,
60 /* Entity is alive and de-activated.
61 */
62 INACTIVE = 2,
63 /* Entity is destroyed and waiting for GC
64 */
65 DEAD = 3,
66 } state { 0 };
67 uint32_t generation { 0 };
68 IEntityReferenceCounter::Ptr counter;
69 };
70 BASE_NS::vector<EntityState> entities_;
71 BASE_NS::vector<Entity> addedList_;
72 BASE_NS::vector<Entity> removedList_;
73 BASE_NS::vector<BASE_NS::pair<Entity, IEntityManager::EventType>> eventList_;
74 uint32_t generationCounter_ { 0 };
75
76 class IteratorImpl : public Iterator {
77 const EntityManager* owner_ { nullptr };
78 uint32_t index_ { 0 };
79 IteratorType type_;
80
81 public:
82 IteratorImpl(const EntityManager&, size_t, IteratorType type);
83 const class IEntityManager* GetOwner() const override;
84 bool Compare(const Iterator::Ptr&) const override;
85 bool Next() override;
86 Entity Get() const override;
87 Iterator::Ptr Clone() const override;
88 };
89 Iterator::Ptr MakeIterator(uint32_t index, IteratorType type) const;
90 };
91 CORE_END_NAMESPACE()
92
93 #endif // CORE_ECS_ENTITY_MANAGER_H
94