SDEngine
Game Engine
Loading...
Searching...
No Matches
EntityManager.hpp
Go to the documentation of this file.
1// TODO(docs): Add file-level Doxygen header
2// - @file EntityManager.hpp
3// - @brief Entity-Component System core manager
4// - Entity creation, destruction, and component management
5// - View system for component queries
6#pragma once
7#include <bitset>
8#include <memory>
9#include <vector>
10
11#include "ComponentFactory.hpp"
12#include "Entity.hpp"
13#include "SD/core/logging.hpp"
14#include "SparseEntitySet.hpp"
16
17
18namespace sd {
19class EntityManager;
20
21// TODO(docs): Document ViewImpl class
22// - Purpose: Iterator for entities with specific components
23// - Iteration pattern and validity checking
24// - begin()/end() semantics
25// - Example: Iterating over entities with Transform and Velocity
26template<typename... Components>
27class ViewImpl {
29 const std::vector<Entity>* m_smallest_pool = nullptr;
30
31public:
32 explicit ViewImpl(EntityManager& manager);
33 struct Iterator {
35 const std::vector<Entity>* entities;
37
38 using iterator_category = std::forward_iterator_tag;
39 using value_type = std::tuple<Entity, Components&...>;
40 using difference_type = std::ptrdiff_t;
41 using pointer = void;
43
44 Iterator(EntityManager& em, const std::vector<Entity>* dense_entities, usize idx);
46
47 bool operator==(const Iterator& other) const {
48 return index == other.index && entities == other.entities;
49 }
50
51 bool operator!=(const Iterator& other) const { return !(*this == other); }
52
53 std::tuple<Entity, Components&...> operator*() const;
54
55 private:
56 void next();
57
58 [[nodiscard]] bool is_valid() const;
59 };
60
61 Iterator begin();
62
63 Iterator end();
64
65
66private:
67 template<typename Component>
69};
70
71
72// TODO: make multi-threaded safe.
84public:
85 Entity create();
86
87 template<typename T, typename... Args>
89
90 template<typename T>
92
99 template<typename T>
101 template<typename T>
102 const T& get_component(Entity e) const;
103 void serialize(Serializer& s) const override;
104 void deserialize(Serializer& s) override;
105 template<typename T>
107
108 void destroy(Entity e);
109 [[nodiscard]] std::vector<ComponentDebugInfo> get_all_component_info(Entity e) const;
110
111 [[nodiscard]] bool is_alive(Entity e) const;
112 [[nodiscard]] int get_entity_count() const { return m_entity_masks.size(); }
114 int alive = 0;
116 if (is_alive(e))
117 ++alive;
118 }
119 return alive;
120 }
121
122
123 template<typename T>
124 struct UnpackGroup {
125 static constexpr bool is_group = false;
126 };
127 template<typename... Ts>
128 struct UnpackGroup<std::tuple<Ts...>> {
129 using type = ViewImpl<Ts...>;
130 static constexpr bool is_group = true;
131 };
132 template<typename... Ts>
134 using type = ViewImpl<Ts...>;
135 static constexpr bool is_group = true;
136 };
137
138 template<typename... Args>
139 auto view();
140
141 template<typename T>
143
144 template<typename... Components>
146
147 template<typename... Components>
148 std::tuple<const Components&...> get_component_group(Entity e) const;
149
150 template<typename T>
152
153 template<typename T>
155
156
157private:
159 std::vector<u32> m_generations;
160 std::vector<u32> m_free_list;
161
162 std::vector<std::unique_ptr<SparseEntitySetBase>> m_component_pools;
163 // NOTE: Hard capped at 256 for now, shouldn't really be a problem. Change in future if needed
164 using ComponentMask = std::bitset<256>;
166
167 // INVARIANTS:
168 // 1. For alive entity e: m_generations[e.index] == e.generation
169 // 2. mFreeList contains only indices of destroyed entities (not in use)
170 // 3. m_entity_masks has entry for every index in m_generations
171 // 4. Component mask bit is set iff component exists in corresponding pool
172
173#ifndef NDEBUG
174 void ValidateInvariants() const {
176 for (u32 idx : m_free_list) {
177 assert(idx < m_generations.size());
178 }
179 }
180#endif
181
183};
184
185#include "impl/EntityManager.inl"
186} // namespace sd
Definition EntityManager.hpp:83
int get_entity_count() const
Definition EntityManager.hpp:112
std::vector< u32 > m_free_list
Definition EntityManager.hpp:160
bool has_component_pool()
int get_alive_entity_count() const
Definition EntityManager.hpp:113
bool is_alive(Entity e) const
Definition EntityManager.inl:242
Entity create()
Definition EntityManager.inl:192
void serialize(Serializer &s) const override
Definition EntityManager.inl:251
std::tuple< const Components &... > get_component_group(Entity e) const
std::vector< u32 > m_generations
Definition EntityManager.hpp:159
std::bitset< 256 > ComponentMask
Definition EntityManager.hpp:164
const T & get_component(Entity e) const
void ValidateInvariants() const
Definition EntityManager.hpp:174
bool has_component(Entity e) const
u32 pop_free_list()
Definition EntityManager.inl:245
std::tuple< Components &... > get_component_group(Entity e)
T * add_component(Entity e, Args &&... args)
bool try_remove_component(Entity e)
void destroy(Entity e)
Definition EntityManager.inl:206
T * try_get_component(Entity e)
SparseEntitySet< T > * get_component_pool()
void deserialize(Serializer &s) override
Definition EntityManager.inl:306
SparseEntitySet< ComponentMask > m_entity_masks
Definition EntityManager.hpp:165
T & get_component(Entity e)
See TryGetComponent for safe pointer version.
std::vector< std::unique_ptr< SparseEntitySetBase > > m_component_pools
Definition EntityManager.hpp:162
std::vector< ComponentDebugInfo > get_all_component_info(Entity e) const
Definition EntityManager.inl:228
Definition RuntimeStateManager.hpp:24
Definition serialization.hpp:16
Definition serialization.hpp:36
Definition SparseEntitySet.hpp:42
const std::vector< Entity > & get_dense_entities() const
Definition SparseEntitySet.hpp:90
usize size() const
Definition SparseEntitySet.hpp:92
Definition EntityManager.hpp:27
Iterator begin()
Definition EntityManager.inl:43
void check_size(usize &minSize)
const std::vector< Entity > * m_smallest_pool
Definition EntityManager.hpp:29
Iterator end()
Definition EntityManager.inl:52
EntityManager & m_manager
Definition EntityManager.hpp:28
Definition Application.hpp:28
Definition component_registration.hpp:44
Definition EntityManager.hpp:124
static constexpr bool is_group
Definition EntityManager.hpp:125
Definition Entity.hpp:18
Definition EntityManager.hpp:33
bool is_valid() const
Definition EntityManager.inl:36
bool operator==(const Iterator &other) const
Definition EntityManager.hpp:47
EntityManager & manager
Definition EntityManager.hpp:34
std::forward_iterator_tag iterator_category
Definition EntityManager.hpp:38
std::tuple< Entity, Components &... > value_type
Definition EntityManager.hpp:39
void next()
Definition EntityManager.inl:28
value_type reference
Definition EntityManager.hpp:42
Iterator & operator++()
Definition EntityManager.inl:17
usize index
Definition EntityManager.hpp:36
const std::vector< Entity > * entities
Definition EntityManager.hpp:35
std::ptrdiff_t difference_type
Definition EntityManager.hpp:40
bool operator!=(const Iterator &other) const
Definition EntityManager.hpp:51
void pointer
Definition EntityManager.hpp:41
std::tuple< Entity, Components &... > operator*() const
Definition EntityManager.inl:22
std::uint32_t u32
Definition types.hpp:15
constexpr T g_type_max
Definition types.hpp:21
std::size_t usize
Definition types.hpp:18