Nui Engine
A game engine framework
Loading...
Searching...
No Matches
Entity.h
1#pragma once
2#include "Core/Engine/ECS/Component.h"
3
4namespace Nui::ECS
5{
9 class Entity
10 {
11 friend class Context;
12 public:
16 constexpr static U64 InvalidId = 0;
17
21 using ComponentMap = std::unordered_map<TypeIndex, std::unique_ptr<Internal::ComponentContainerBase>>;
22
23 public:
29 Entity(Context* context, U64 id) : m_context(context), m_id(id) {}
30
35
40 inline Context* GetContext() const noexcept { return m_context; }
41
46 inline U64 GetId() const noexcept { return m_id; }
47
52 inline bool IsPendingDestroy() const noexcept { return m_pendingDestroy; }
53
59 template<typename T>
60 bool Has() const
61 {
62 TypeIndex index = GetTypeIndex<T>();
63 return m_components.find(index) != m_components.end();
64 }
65
73 template<typename T, typename U, typename... Types>
74 bool Has() const
75 {
76 return Has<T>() && Has<U, Types...>();
77 }
78
86 template<typename T, typename... Args>
87 ComponentHandle<T> Add(Args&&... args);
88
94 template<typename T>
96
102 template<typename T>
103 bool Remove();
104
108 void RemoveAll();
109
116 template<typename... Types>
117 bool With(typename std::common_type<std::function<void(ComponentHandle<Types>...)>>::type func)
118 {
119 if (!Has<Types...>())
120 return false;
121
122 func(Get<Types>()...);
123
124 return true;
125 }
126
127 private:
131 ComponentMap m_components;
132
136 Context* m_context;
137
141 U64 m_id;
142
146 bool m_pendingDestroy{ false };
147 };
148
149 namespace Internal
150 {
155 template <typename... Types>
157 {
158 public:
166 EntityComponentIterator(Context* context, U64 index, bool isEnd, bool includePendingDestroy);
167
172 inline U64 GetIndex() const noexcept { return m_index; }
173
178 inline bool IncludePendingDestroy() const noexcept { return m_includePendingDestroy; }
179
184 inline Context* GetContext() const noexcept { return m_context; }
185
190 bool IsEnd() const noexcept;
191
196 Entity* GetEntity() const noexcept;
197
202 Entity* operator*() const noexcept
203 {
204 return GetEntity();
205 }
206
213 {
214 if (m_context != other.m_context)
215 return false;
216
217 if (m_isEnd)
218 return other.m_isEnd;
219
220 return m_index == other.m_index;
221 }
222
229 {
230 if (m_context != other.m_context)
231 return true;
232
233 if (m_isEnd)
234 return !other.m_isEnd;
235
236 return m_index != other.m_index;
237 }
238
244
245 private:
249 bool m_isEnd;
250
254 U64 m_index;
255
259 Context* m_context;
260
264 bool m_includePendingDestroy;
265 };
266
271 template<typename... Types>
273 {
274 public:
281
286 const EntityComponentIterator<Types...>& begin() const
287 {
288 return m_first;
289 }
290
295 const EntityComponentIterator<Types...>& end() const
296 {
297 return m_last;
298 }
299
300 private:
304 EntityComponentIterator<Types...> m_first;
305
309 EntityComponentIterator<Types...> m_last;
310 };
311
316 {
317 public:
325 EntityIterator(Context* context, U64 index, bool isEnd, bool includePendingDestroy);
326
331 bool IsEnd() const noexcept;
332
337 inline U64 GetIndex() const noexcept { return m_index; }
338
343 inline bool IncludePendingDestroy() const noexcept { return m_includePendingDestroy; }
344
349 inline Context* GetContext() const noexcept { return m_context; }
350
355 Entity* GetEntity() const noexcept;
356
361 Entity* operator*() const noexcept { return GetEntity(); }
362
368 bool operator==(const EntityIterator& other) const
369 {
370 if (m_context != other.m_context)
371 return false;
372
373 if (m_isEnd)
374 return other.m_isEnd;
375
376 return m_index == other.m_index;
377 }
378
384 bool operator!=(const EntityIterator& other) const
385 {
386 if (m_context != other.m_context)
387 return true;
388
389 if (m_isEnd)
390 return !other.m_isEnd;
391
392 return m_index != other.m_index;
393 }
394
400
401 private:
405 bool m_isEnd;
406
410 U64 m_index;
411
415 Context* m_context;
416
420 bool m_includePendingDestroy;
421 };
422
427 {
428 public:
434 EntityView(const EntityIterator& first, const EntityIterator& last)
435 : m_first(first), m_last(last)
436 {
437 if (m_first.GetEntity() == nullptr || m_first.GetEntity()->IsPendingDestroy() && !m_first.IncludePendingDestroy())
438 {
439 ++m_first;
440 }
441 }
442
447 const EntityIterator& begin() const
448 {
449 return m_first;
450 }
451
456 const EntityIterator& end() const
457 {
458 return m_last;
459 }
460
461 private:
465 EntityIterator m_first;
466
470 EntityIterator m_last;
471 };
472 }
473}
This class provides a handle to a component.
Definition Component.h:52
The ECS context class manages entities, systems, and events in the ECS framework.
Definition Context.h:17
Class to represent an entity in an ECS Context.
Definition Entity.h:10
bool Has() const
Whether the entity has components of the given types.
Definition Entity.h:74
Entity(Context *context, U64 id)
Constructs a new entity in the given ECS Context with the given id.
Definition Entity.h:29
ComponentHandle< T > Add(Args &&... args)
Adds a component to the entity.
Definition ECS.h:151
bool Remove()
Gets a component from the entity.
Definition ECS.h:196
bool With(typename std::common_type< std::function< void(ComponentHandle< Types >...)> >::type func)
Executes a function on a set of component handles if the entity has components of the specified types...
Definition Entity.h:117
~Entity()
Destroys the entity and all its components.
Definition Entity.h:34
std::unordered_map< TypeIndex, std::unique_ptr< Internal::ComponentContainerBase > > ComponentMap
Maps component type indices to unique pointers to component containers.
Definition Entity.h:21
static constexpr U64 InvalidId
Invalid entity ID.
Definition Entity.h:16
Context * GetContext() const noexcept
Gets the ECS context associated with the entity.
Definition Entity.h:40
U64 GetId() const noexcept
Gets the ID of the entity.
Definition Entity.h:46
bool IsPendingDestroy() const noexcept
Whether the entity has pending destruction.
Definition Entity.h:52
bool Has() const
Whether the entity has a component of the given type.
Definition Entity.h:60
void RemoveAll()
Removes all components from the entity.
Definition Entity.cpp:5
ComponentHandle< T > Get()
Gets a component from the entity.
Definition ECS.h:183
Iterator class to allow easy iteration over entities with components.
Definition Entity.h:157
U64 GetIndex() const noexcept
Gets the index of the current entity in the iteration.
Definition Entity.h:172
bool IsEnd() const noexcept
Checks if the iterator is at the end.
Definition ECS.h:25
Context * GetContext() const noexcept
Gets the ECS context associated with the iterator.
Definition Entity.h:184
EntityComponentIterator< Types... > & operator++()
Moves the iterator to the next entity and skips over entities based on input criteria.
Definition ECS.h:40
Entity * GetEntity() const noexcept
Gets the current entity held by the iterator.
Definition ECS.h:31
bool IncludePendingDestroy() const noexcept
Whether the iterator should include pending destruction entities.
Definition Entity.h:178
bool operator==(const EntityComponentIterator< Types... > &other) const
Checks if two iterators are equal.
Definition Entity.h:212
EntityComponentIterator(Context *context, U64 index, bool isEnd, bool includePendingDestroy)
Initializes the iterator with necessary information.
Definition ECS.h:12
bool operator!=(const EntityComponentIterator< Types... > &other) const
Checks if two iterators are not equal.
Definition Entity.h:228
Class to represent a view over a range of entities in an ECS Context.
Definition Entity.h:273
EntityComponentView(const EntityComponentIterator< Types... > &first, const EntityComponentIterator< Types... > &last)
Initializes the view with iterator range.
Definition ECS.h:73
const EntityComponentIterator< Types... > & begin() const
Gets the beginning of the view.
Definition Entity.h:286
const EntityComponentIterator< Types... > & end() const
Gets the end of the view.
Definition Entity.h:295
Iterator class to allow easy iteration over entities.
Definition Entity.h:316
bool IncludePendingDestroy() const noexcept
Whether the iterator should include pending destruction entities.
Definition Entity.h:343
Entity * GetEntity() const noexcept
Gets the current entity held by the iterator.
Definition ECS.h:113
Context * GetContext() const noexcept
Gets the ECS context associated with the iterator.
Definition Entity.h:349
bool operator==(const EntityIterator &other) const
Checks if two iterators are equal.
Definition Entity.h:368
bool IsEnd() const noexcept
Checks if the iterator is at the end.
Definition ECS.h:108
U64 GetIndex() const noexcept
Gets the index of the current entity in the iteration.
Definition Entity.h:337
bool operator!=(const EntityIterator &other) const
Checks if two iterators are not equal.
Definition Entity.h:384
EntityIterator(Context *context, U64 index, bool isEnd, bool includePendingDestroy)
Initializes the iterator with necessary information.
Definition ECS.h:96
EntityIterator & operator++()
Moves the iterator to the next entity and skips over entities based on input criteria.
Definition ECS.h:121
Class to represent a view over a range of entities in an ECS Context.
Definition Entity.h:427
const EntityIterator & begin() const
Gets the beginning of the view.
Definition Entity.h:447
const EntityIterator & end() const
Gets the end of the view.
Definition Entity.h:456
EntityView(const EntityIterator &first, const EntityIterator &last)
Initializes the view with iterator range.
Definition Entity.h:434