28 template<FilterBase TargetBase,
class... TargetIncludes>
29 requires PolicyType::template ConvertibleTo<TargetBase, TargetIncludes...>
30 operator EcsInterface<TargetBase, TargetIncludes...>()
const noexcept
32 return EcsInterface<TargetBase, TargetIncludes...>(m_policy);
36 template<std::same_as<Entity> T>
37 requires PolicyType::template HasAccess<Entity, Transform, Tag, Hierarchy>
40 const auto handle = m_policy.template GetPool<Entity>().Add(info.layer, info.flags, info.userData);
41 if (info.parent.Valid())
43 auto& parentTransform = Get<Transform>(info.parent);
44 Emplace<Transform>(handle, info.position, info.rotation, info.scale, parentTransform.TransformationMatrix());
45 Get<Hierarchy>(info.parent).children.push_back(handle);
49 Emplace<Transform>(handle, info.position, info.rotation, info.scale);
52 Emplace<Hierarchy>(handle, info.parent, std::vector<Entity>{});
53 Emplace<Tag>(handle, std::move(info.tag));
58 template<Component T,
class... Args>
59 requires PolicyType::template HasAccess<T>
62 NC_ASSERT(Contains<Entity>(entity),
"Bad entity");
63 if constexpr (std::derived_from<T, FreeComponent>)
65 return m_policy.GetFreeComponentPool().template Emplace<T>(entity, std::forward<Args>(args)...);
69 return m_policy.template GetPool<T>().Emplace(entity, std::forward<Args>(args)...);
74 template<std::same_as<Entity> T>
75 requires PolicyType::template HasAccess<Entity, Transform>
78 return Contains<Entity>(entity) ? RemoveNode<true>(entity) :
false;
83 requires PolicyType::template HasAccess<T>
86 if (!Contains<Entity>(entity))
89 if constexpr (std::derived_from<T, FreeComponent>)
91 return m_policy.GetFreeComponentPool().template Remove<T>(entity);
95 return m_policy.template GetPool<T>().Remove(entity);
100 template<Component T>
101 requires PolicyType::template HasAccess<T>
104 if constexpr (std::derived_from<T, FreeComponent>)
106 return m_policy.GetFreeComponentPool().template Get<T>(entity);
110 return m_policy.template GetPool<T>().Get(entity);
115 template<Component T>
116 requires PolicyType::template HasAccess<T>
119 if constexpr (std::derived_from<T, FreeComponent>)
121 return m_policy.GetFreeComponentPool().template Get<T>(entity);
125 return m_policy.template GetPool<T>().Get(entity);
136 requires PolicyType::template HasAccess<Entity>
137 && PolicyType::template HasAccess<Tag>
139 const auto tags = GetAll<Tag>();
140 const auto pos = std::ranges::find(tags, tagValue, [](
const auto& tag) {
return tag.value; });
141 NC_ASSERT(pos != std::ranges::end(tags), fmt::format(
"No Entity found with Tag '{}'", tagValue));
142 return m_policy.template GetPool<Tag>().GetParent(&(*pos));
147 requires PolicyType::template HasAccess<T>
149 auto GetAll() -> std::span<T>
151 return std::span<T>{GetPool<T>()};
156 requires PolicyType::template HasAccess<T>
157 && (PooledComponent<T> || std::same_as<Entity, T>)
158 auto GetAll() const -> std::span<const T>
160 return std::span<const T>{GetPool<T>()};
165 requires PolicyType::template HasAccess<T>
166 && (PooledComponent<T> || std::same_as<Entity, T>)
167 auto GetPool() ->
decltype(
auto)
169 return m_policy.template GetPool<T>();
174 requires PolicyType::template HasAccess<T>
175 && (PooledComponent<T> || std::same_as<Entity, T>)
178 return m_policy.template GetPool<T>();
182 auto GetComponentPools()
183 requires PolicyType::template BaseContains<
FilterBase::All>
185 return m_policy.GetComponentPools();
190 requires PolicyType::template HasAccess<Hierarchy>
192 auto& hierarchy = Get<Hierarchy>(entity);
193 const auto oldParent = std::exchange(hierarchy.parent, parent);
194 if (oldParent.Valid())
196 std::erase(Get<Hierarchy>(oldParent).children, entity);
201 Get<Hierarchy>(parent).children.push_back(entity);
206 auto GetRoot(Entity entity) -> Entity
207 requires PolicyType::template HasAccess<Hierarchy>
209 const auto& hierarchy = Get<Hierarchy>(entity);
210 return !hierarchy.parent.Valid() ? entity : GetRoot(hierarchy.parent);
214 template<PooledComponent T>
215 requires PolicyType::template HasAccess<T>
216 auto GetParent(
const T* component)
const -> Entity
218 return m_policy.template GetPool<T>().GetParent(component);
222 template<RegistryType T>
223 requires PolicyType::template HasAccess<T>
224 auto Contains(Entity entity)
const ->
bool
226 if constexpr (std::derived_from<T, FreeComponent>)
228 return m_policy.GetFreeComponentPool().template Contains<T>(entity);
232 return m_policy.template GetPool<T>().Contains(entity);
239 template<
bool IsRoot>
242 auto& hierarchy = Get<Hierarchy>(entity);
245 if (hierarchy.parent.Valid())
247 std::erase(Get<Hierarchy>(hierarchy.parent).children, entity);
251 for (
auto child : hierarchy.children)
252 RemoveNode<false>(child);
254 m_policy.template GetPool<Entity>().Remove(entity);
263template<
class... Includes>
267template<
class... Includes>
FilterBase
Filter option for AccessPolicy providing coarse control over type access.
Definition: EcsFilter.h:16
Identifies an object in the registry.
Definition: Entity.h:18
A proxy for ComponentRegistry providing filtered access to data pools.
Definition: AccessPolicy.h:33
std::conditional_t< Base==FilterBase::All, detail::AllFilter, std::conditional_t< Base==FilterBase::Basic, detail::MatchFilter< Entity, Hierarchy, Tag, Transform, Includes... >, detail::MatchFilter< Includes... > > > FilterType
The policy's filter type.
Definition: AccessPolicy.h:43
Interface for higher-level entity and component operations with optional type access restriction.
Definition: Ecs.h:18
EcsInterface(AccessPolicy< Base, Includes... > policy) noexcept
Construct an EcsInterface instance.
Definition: Ecs.h:24
auto GetEntityByTag(std::string_view tagValue) -> Entity
Get the first Entity matching a Tag.
Definition: Ecs.h:135
::template HasAccess< T > auto Emplace(Entity entity, Args &&... args) -> T &
Emplace a component.
Definition: Ecs.h:60
auto GetComponentPools() auto RemoveNode(Entity entity) -> bool
Get a range of pointers to all ComponentPoolBase instances.
Definition: Ecs.h:240
::template HasAccess< Entity, Transform > auto Remove(Entity entity) -> bool
Remove an entity.
Definition: Ecs.h:76
::template HasAccess< T > auto Get(Entity entity) -> T &
Get a component.
Definition: Ecs.h:102
::template HasAccess< T > &&PooledComponent< T > std::same_as< Entity, T > auto GetPool() const -> decltype(auto)
Get the pool for a given type.
Definition: Ecs.h:176
::template HasAccess< T > auto Get(Entity entity) const -> const T &
Get a component.
Definition: Ecs.h:117
::template HasAccess< T > auto Remove(Entity entity) -> bool
Remove a component.
Definition: Ecs.h:84
::template HasAccess< Entity, Transform, Tag, Hierarchy > auto Emplace(EntityInfo info={}) -> Entity
Emplace an entity.
Definition: Ecs.h:38
Requirements for the Registry to recognize a pooled component.
Definition: Component.h:61
Initialization data for Entities.
Definition: Entity.h:111