CoriEngine
Loading...
Searching...
No Matches
EntityView.hpp
Go to the documentation of this file.
1#pragma once
2#include "Entity.hpp"
3
4namespace Cori {
5 namespace World {
10 template <typename... T>
11 struct Exclude {};
12
17 template <typename View>
19 public:
20 using UnderlyingIterator = typename View::iterator;
21
22 class Iterator {
23 public:
24 using iterator_category = std::forward_iterator_tag;
26 using difference_type = std::ptrdiff_t;
27 using pointer = const Entity*;
29
30 Iterator(entt::registry* registry, UnderlyingIterator it)
31 : m_Registry(registry), m_EnttIterator(it) {}
32
34 return Entity({*m_Registry, *m_EnttIterator});
35 }
36
38 ++m_EnttIterator;
39 return *this;
40 }
41
42 bool operator!=(const Iterator& other) const {
43 return m_EnttIterator != other.m_EnttIterator;
44 }
45
46 private:
47 entt::registry* m_Registry;
48 UnderlyingIterator m_EnttIterator;
49 };
50
51 StaticEntityView(View view, entt::registry& registry)
52 : m_View(view), m_Registry(&registry) {}
53
60 template<typename... T>
61 [[nodiscard]] decltype(auto) Get(Entity entity) {
62 return m_View.template get<T...>(entity.GetRawEntity());
63 }
64
71 template<typename T>
72 [[nodiscard]] size_t Size() {
73 return m_View.template size<T>();
74 }
75
81 [[nodiscard]] size_t SizeHint() const {
82 return m_View.size_hint();
83 }
84
90 [[nodiscard]] bool Contains(const Entity entity) const {
91 return m_View.contains(entity.GetRawEntity());
92 }
93
94 auto begin() { return Iterator(m_Registry, m_View.begin()); }
95 auto end() { return Iterator(m_Registry, m_View.end()); }
96
97 private:
98 View m_View;
99 entt::registry* m_Registry;
100 };
101
108 public:
109
114 explicit DynamicEntityView(entt::registry& registry)
115 : m_Registry(&registry) {}
116
121 template <typename... T>
123 (m_View.iterate(m_Registry->storage<T>()), ...);
124 return *this;
125 }
126
131 template <typename... T>
133 (m_View.iterate(m_Registry->storage<T>()), ...);
134 return std::move(*this);
135 }
136
141 template <typename... T>
143 (m_View.exclude(m_Registry->storage<T>()), ...);
144 return *this;
145 }
146
151 template <typename... T>
153 (m_View.exclude(m_Registry->storage<T>()), ...);
154 return std::move(*this);
155 }
156
161 [[nodiscard]] size_t SizeHint() const {
162 return m_View.size_hint();
163 }
164
170 [[nodiscard]] bool Contains(const Entity entity) const {
171 return m_View.contains(entity.GetRawEntity());
172 }
173
177 void Clear() {
178 m_View.clear();
179 }
180
181 using UnderlyingIterator = entt::runtime_view::iterator;
182 class Iterator {
183 public:
184 using iterator_category = std::forward_iterator_tag;
186 using difference_type = std::ptrdiff_t;
187 using pointer = const Entity*;
189
190 Iterator(entt::registry* registry, UnderlyingIterator it)
191 : m_Registry(registry), m_EnttIterator(it) {}
192
194 return Entity({*m_Registry, *m_EnttIterator});
195 }
196
198 ++m_EnttIterator;
199 return *this;
200 }
201
202 bool operator!=(const Iterator& other) const {
203 return m_EnttIterator != other.m_EnttIterator;
204 }
205
206 private:
207 entt::registry* m_Registry;
208 UnderlyingIterator m_EnttIterator;
209 };
210
211 auto begin() { return Iterator(m_Registry, m_View.begin()); }
212 auto end() { return Iterator(m_Registry, m_View.end()); }
213
214 private:
215 entt::registry* m_Registry;
216 entt::runtime_view m_View;
217 };
218 }
219}
bool operator!=(const Iterator &other) const
Iterator(entt::registry *registry, UnderlyingIterator it)
std::forward_iterator_tag iterator_category
void Clear()
Completely clear the view.
size_t SizeHint() const
Checks the estimate amount of entities in the view.
bool Contains(const Entity entity) const
Checks if view contains a specific entity.
DynamicEntityView & With() &
Adds one or more component types to the view's filter.
DynamicEntityView && Without() &&
Excludes one or more component types from the view's filter.
DynamicEntityView && With() &&
Adds one or more component types to the view's filter.
DynamicEntityView & Without() &
Excludes one or more component types from the view's filter.
entt::runtime_view::iterator UnderlyingIterator
DynamicEntityView(entt::registry &registry)
Constructs a runtime view wrapper.
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
entt::entity GetRawEntity() const
Gets a raw entt::entity if you need to interact with entt directly.
Definition Entity.hpp:280
std::forward_iterator_tag iterator_category
Iterator(entt::registry *registry, UnderlyingIterator it)
bool operator!=(const Iterator &other) const
size_t SizeHint() const
Checks the estimate amount of entities in the view.
bool Contains(const Entity entity) const
Checks if view contains a specific entity.
size_t Size()
Checks how many entities in a view have a specific component.
StaticEntityView(View view, entt::registry &registry)
decltype(auto) Get(Entity entity)
Retries the components from an entity, both the components and the entity should be in a view,...
typename View::iterator UnderlyingIterator
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
Helper to exclude certain components from a static view.