19template <
typename... Types>
20inline constexpr bool distinct_types =
true;
22template <
typename First,
typename... Rest>
23inline constexpr bool distinct_types<First, Rest...> =
24 (!std::is_same_v<First, Rest> && ...) && distinct_types<Rest...>;
49template <
typename... Components>
50 requires(
sizeof...(Components) > 0) && detail::distinct_types<Components...>
62 template <
typename Function>
63 requires std::invocable<Function&, Entity, Components&...> ||
64 std::invocable<Function&, Components&...>
65 void each(Function function) {
68 const std::size_t position =
remaining - 1;
69 if (position >= driver.
size()) {
73 *std::next(driver.
entities().begin(),
static_cast<std::ptrdiff_t
>(position));
77 if constexpr (std::invocable<Function&,
Entity, Components&...>) {
78 std::invoke(function, entity, get<Components>(entity)...);
80 std::invoke(function, get<Components>(entity)...);
89 return (pool<Components>().
contains(entity) && ...);
97 template <
typename Component>
98 requires(std::is_same_v<Component, Components> || ...)
100 return pool<Component>().get(entity);
105 [[nodiscard]] std::size_t
size_hint() const noexcept {
return smallest_pool().
size(); }
108 template <
typename Component>
110 return std::get<std::reference_wrapper<SparseSet<Component>>>(pools_).
get();
113 [[nodiscard]]
const SparseSetBase& smallest_pool() const noexcept {
114 std::reference_wrapper<const SparseSetBase> smallest = std::get<0>(pools_).get();
117 const SparseSetBase& candidate = pool<Components>();
118 if (candidate.size() < smallest.get().size()) {
119 smallest = candidate;
123 return smallest.get();
126 std::tuple<std::reference_wrapper<SparseSet<Components>>...> pools_;
Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.
Definition entity.hpp:23
Type-erased part of a sparse set: which entities are in the pool, and where.
Definition sparse_set.hpp:30
std::size_t size() const noexcept
Number of entities in the pool.
Definition sparse_set.hpp:55
std::span< const Entity > entities() const noexcept
Entities of the pool, packed, in the same order as the components.
Definition sparse_set.hpp:63
Pool of one component type, stored in a sparse set.
Definition sparse_set.hpp:117
Visits every entity that owns all of Components, with those components.
Definition view.hpp:51
void each(Function function)
Calls a function for every entity that owns all the components.
Definition view.hpp:65
std::size_t size_hint() const noexcept
Upper bound of the number of entities visited.
Definition view.hpp:105
Component & get(Entity entity) const
One component of an entity of the view.
Definition view.hpp:99
View(SparseSet< Components > &... pools) noexcept
Creates a view on the given pools.
Definition view.hpp:56
bool contains(Entity entity) const noexcept
Tells whether an entity owns all the components of the view.
Definition view.hpp:88
Generational entity handle: an index into the component pools plus a generation.
Entity-Component-System used by client and server.
Definition animation.hpp:13
float remaining(const PathFollower &follower) noexcept
Distance a follower still has to walk.
Definition path_following.hpp:91
Sparse-set component pool: components packed in a dense array, found in O(1).