Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1
3#pragma once
6
7#include <concepts>
8#include <cstddef>
9#include <functional>
10#include <iterator>
11#include <tuple>
12#include <type_traits>
13
14namespace aegis2d::ecs {
15
17namespace detail {
18
19template <typename... Types>
20inline constexpr bool distinct_types = true;
21
22template <typename First, typename... Rest>
23inline constexpr bool distinct_types<First, Rest...> =
24 (!std::is_same_v<First, Rest> && ...) && distinct_types<Rest...>;
25
26} // namespace detail
28
49template <typename... Components>
50 requires(sizeof...(Components) > 0) && detail::distinct_types<Components...>
51class View {
52public:
56 explicit View(SparseSet<Components>&... pools) noexcept : pools_{std::ref(pools)...} {}
57
62 template <typename Function>
63 requires std::invocable<Function&, Entity, Components&...> ||
64 std::invocable<Function&, Components&...>
65 void each(Function function) {
66 const SparseSetBase& driver = smallest_pool();
67 for (std::size_t remaining = driver.size(); remaining > 0; --remaining) {
68 const std::size_t position = remaining - 1;
69 if (position >= driver.size()) {
70 continue;
71 }
72 const Entity entity =
73 *std::next(driver.entities().begin(), static_cast<std::ptrdiff_t>(position));
74 if (!contains(entity)) {
75 continue;
76 }
77 if constexpr (std::invocable<Function&, Entity, Components&...>) {
78 std::invoke(function, entity, get<Components>(entity)...);
79 } else {
80 std::invoke(function, get<Components>(entity)...);
81 }
82 }
83 }
84
88 [[nodiscard]] bool contains(Entity entity) const noexcept {
89 return (pool<Components>().contains(entity) && ...);
90 }
91
97 template <typename Component>
98 requires(std::is_same_v<Component, Components> || ...)
99 [[nodiscard]] Component& get(Entity entity) const {
100 return pool<Component>().get(entity);
101 }
102
105 [[nodiscard]] std::size_t size_hint() const noexcept { return smallest_pool().size(); }
106
107private:
108 template <typename Component>
109 [[nodiscard]] SparseSet<Component>& pool() const noexcept {
110 return std::get<std::reference_wrapper<SparseSet<Component>>>(pools_).get();
111 }
112
113 [[nodiscard]] const SparseSetBase& smallest_pool() const noexcept {
114 std::reference_wrapper<const SparseSetBase> smallest = std::get<0>(pools_).get();
115 (
116 [&] {
117 const SparseSetBase& candidate = pool<Components>();
118 if (candidate.size() < smallest.get().size()) {
119 smallest = candidate;
120 }
121 }(),
122 ...);
123 return smallest.get();
124 }
125
126 std::tuple<std::reference_wrapper<SparseSet<Components>>...> pools_;
127};
128
129} // namespace aegis2d::ecs
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).