R-Type  1.0.1.0
Rewrite of the R-Type game with networking ability
Loading...
Searching...
No Matches
View.hpp
1/*
2** EPITECH PROJECT, 2023
3** View.hpp
4** File description:
5** View.hpp
6*/
7
8#ifndef URANUS_VIEW_HPP
9#define URANUS_VIEW_HPP
10
11#include "Registry.hpp"
12
16namespace uranus::ecs {
21 template<typename... Components>
22 class View {
23 public:
24 using Tuple = std::tuple<size_t, Components &...>;
30 explicit View(ecs::Registry &registry) : _registry(registry) {}
31
35 class Iterator {
36 public:
41 explicit Iterator(ecs::Registry &registry) : _registry(registry)
42 {
43 idx = 0;
44 skipInvalidEntities<Components...>();
45 }
46
52 {
53 idx++;
54 skipInvalidEntities<Components...>();
55 return *this;
56 }
57
62 Tuple operator->() { return Tuple(idx, (*_registry.getComponent<Components>(idx))...); }
63
68 Tuple operator*() { return Tuple(idx, (*_registry.getComponent<Components>(idx))...); }
69
75 bool operator==(const Iterator &other) const { return idx == other.idx; }
76
82 bool operator!=(const Iterator &other) const { return !(other == *this); /* NOLINT */ }
83
84 std::size_t idx;
85
86 private:
94 template<typename Component, typename... Others>
96 {
97 while (idx < _registry.getEntityCounter()) {
98 bool hasAllComponents = true;
99 auto &component = _registry.getComponent<Component>(idx);
100 if (component == nullptr) {
101 hasAllComponents = false;
102 }
103 if (hasAllComponents) {
104 if constexpr (sizeof...(Others) > 0) {
105 skipInvalidEntities<Others...>();
106 }
107 }
108 if (hasAllComponents) return;
109 if (idx == _registry.getEntityCounter()) break;
110 idx++;
111 }
112 }
113 };
114
120
126 {
128 it.idx = _registry.getEntityCounter();
129 return it;
130 }
131
132 private:
134 };
135} // namespace uranus::ecs
136
137#endif // URANUS_VIEW_HPP
Registry class, used to store and manage entities and their components.
Definition: Registry.hpp:26
size_t getEntityCounter() const
Get the counter used to generate new entity ids.
Definition: Registry.hpp:335
SparseArray< Component >::ReferenceType getComponent(const Entity &e)
Get a component from an entity.
Definition: Registry.hpp:214
Iterator class for the view.
Definition: View.hpp:35
Tuple operator->()
Postfix increment operator.
Definition: View.hpp:62
bool operator!=(const Iterator &other) const
Inequality operator.
Definition: View.hpp:82
void skipInvalidEntities()
Skip all the entities that don't have all the components.
Definition: View.hpp:95
Iterator(ecs::Registry &registry)
Construct a new Iterator object.
Definition: View.hpp:41
Tuple operator*()
Dereference operator.
Definition: View.hpp:68
bool operator==(const Iterator &other) const
Equality operator.
Definition: View.hpp:75
ecs::Registry _registry
The registry to iterate over.
Definition: View.hpp:87
Iterator & operator++()
Prefix increment operator.
Definition: View.hpp:51
A view is a way to iterate over a set of entities that have a specific set of components.
Definition: View.hpp:22
Iterator end()
Get the end iterator.
Definition: View.hpp:125
View(ecs::Registry &registry)
Construct a new View object.
Definition: View.hpp:30
std::tuple< size_t, Components &... > Tuple
The tuple type that will be returned by the iterators.
Definition: View.hpp:24
Iterator begin()
Get the begin iterator.
Definition: View.hpp:119
Registry _registry
The registry to iterate over.
Definition: View.hpp:133
Namespace containing all the ECS related classes.
Definition: Components.hpp:21