VOX
A little voxel engine
Loading...
Searching...
No Matches
View.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <unordered_map>
4#include <array>
5#include <limits>
6
7#include "ecs_FWD.hpp"
8
9#include "Manager.hpp"
10#include "ComponentStorage.hpp"
11#include "SparseSet.hpp"
12
13namespace ecs
14{
15 template <ValidEntity EntityType, size_t pool_size>
17 {
18 public:
21 typedef std::shared_ptr<entitySet> SparseSetPtr;
22 typedef std::array<SparseSetPtr, pool_size> SparseSetArray;
23
25 : m_sparseSets{sparseSets}, m_leadSet(leadSet), m_lead_iterator(leadIter)
26 {
27 seek_next();
28 }
29
31 ViewIterator(const ViewIterator & other) = default;
32 ViewIterator(ViewIterator && other) = default;
33 ViewIterator & operator=(const ViewIterator & other) = default;
34 ViewIterator & operator=(ViewIterator && other) = default;
35
37 {
38 m_lead_iterator++;
39 seek_next();
40 return *this;
41 }
42
44 {
45 ViewIterator temp = *this;
46 ++(*this);
47 return temp;
48 }
49
51 {
52 --m_lead_iterator;
53 seek_prev();
54 return *this;
55 }
56
58 {
59 ViewIterator temp = *this;
60 --(*this);
61 return temp;
62 }
63
64 bool operator==(const ViewIterator & other) const
65 {
66 return (m_lead_iterator == other.m_lead_iterator);
67 }
68
69 bool operator!=(const ViewIterator & other) const
70 {
71 return (!(*this == other));
72 }
73
74 EntityType operator*()
75 {
76 return *m_lead_iterator;
77 }
78
79 private:
80
81 bool all_of(EntityType entity)
82 {
83 for (auto & set : m_sparseSets)
84 {
85 if (!set->contains(entity))
86 return false;
87 }
88 return true;
89 }
90
91 void seek_next()
92 {
93 while(m_lead_iterator != m_leadSet->end() && !all_of(*m_lead_iterator))
94 ++m_lead_iterator;
95 }
96
97 void seek_prev()
98 {
99 while (m_lead_iterator != m_leadSet->begin() && !all_of(*m_lead_iterator))
100 --m_lead_iterator;
101 }
102
103 SparseSetArray m_sparseSets;
104 SparseSetPtr m_leadSet;
105 entityIterator m_lead_iterator;
106 };
107
108 template <ValidEntity EntityType, typename... ComponentTypes>
109 class View
110 {
111 public:
113 typedef IndexOf<ComponentTypes...> index;
114 typedef std::array<std::shared_ptr<entitySet>, sizeof... (ComponentTypes)> entitySetArray;
115 typedef ViewIterator<EntityType, sizeof... (ComponentTypes)> iterator;
116
118 {
119 //mmmmm spicy
120 auto func = [&]<typename ComponentType>()
121 {
123 std::shared_ptr<ComponentSet> set = storage.template getSetPtr<ComponentType>();
124
125 m_sparseSets[index::template get<ComponentType>()] = set;
126 };
127 (func.template operator()<ComponentTypes>(), ...);
128
129 set_lead();
130 }
131
132 View(){};
133 ~View(){};
134 View(const View & other)
135 {
136 m_sparseSets = other.m_sparseSets;
137 set_lead();
138 }
139 View(View && other)
140 {
141 m_sparseSets = std::move(other.m_sparseSets);
142 other.m_lead = 0;
143 set_lead();
144 }
145 View & operator=(const View & other)
146 {
147 m_sparseSets = other.m_sparseSets;
148 set_lead();
149 return *this;
150 }
151 View & operator=(View && other)
152 {
153 m_sparseSets = std::move(other.m_sparseSets);
154 other.m_lead = 0;
155 set_lead();
156 return *this;
157 }
158
159 //begin and end
161 {
162 //give the iterator a lead set and the rest of the sets
163 return iterator(m_sparseSets, m_sparseSets[m_lead], m_sparseSets[m_lead]->begin());
164 }
165
167 {
168 return iterator(m_sparseSets, m_sparseSets[m_lead], m_sparseSets[m_lead]->end());
169 }
170 private:
171 entitySetArray m_sparseSets;
172 size_t m_lead = 0;
173
174 void set_lead()
175 {
176 size_t min = std::numeric_limits<size_t>::max();
177 for (size_t i = 0; i < m_sparseSets.size(); i++)
178 {
179 auto set = m_sparseSets[i];
180 if (set->size() < min)
181 {
182 min = set->size();
183 m_lead = i;
184 }
185 }
186 }
187 };
188}
Definition: ComponentStorage.hpp:12
This class is the main class of the ECS system you can use it to create entitites,...
Definition: Manager.hpp:34
Definition: SparseSet.hpp:20
dense_type::iterator iterator
Definition: SparseSet.hpp:26
Definition: View.hpp:17
entitySet::iterator entityIterator
Definition: View.hpp:20
ViewIterator & operator=(ViewIterator &&other)=default
~ViewIterator()
Definition: View.hpp:30
bool operator==(const ViewIterator &other) const
Definition: View.hpp:64
ViewIterator & operator--()
Definition: View.hpp:50
ViewIterator operator++(int)
Definition: View.hpp:43
ViewIterator operator--(int)
Definition: View.hpp:57
ViewIterator(const ViewIterator &other)=default
bool operator!=(const ViewIterator &other) const
Definition: View.hpp:69
SparseSet< EntityType > entitySet
Definition: View.hpp:19
EntityType operator*()
Definition: View.hpp:74
ViewIterator(ViewIterator &&other)=default
ViewIterator & operator=(const ViewIterator &other)=default
ViewIterator(SparseSetArray &sparseSets, SparseSetPtr leadSet, entityIterator leadIter=entityIterator())
Definition: View.hpp:24
std::shared_ptr< entitySet > SparseSetPtr
Definition: View.hpp:21
std::array< SparseSetPtr, pool_size > SparseSetArray
Definition: View.hpp:22
ViewIterator & operator++()
Definition: View.hpp:36
Definition: View.hpp:110
~View()
Definition: View.hpp:133
View(Manager< EntityType > &storage)
Definition: View.hpp:117
View(View &&other)
Definition: View.hpp:139
View & operator=(View &&other)
Definition: View.hpp:151
IndexOf< ComponentTypes... > index
Definition: View.hpp:113
View(const View &other)
Definition: View.hpp:134
View()
Definition: View.hpp:132
View & operator=(const View &other)
Definition: View.hpp:145
iterator begin()
Definition: View.hpp:160
ViewIterator< EntityType, sizeof...(ComponentTypes)> iterator
Definition: View.hpp:115
std::array< std::shared_ptr< entitySet >, sizeof...(ComponentTypes)> entitySetArray
Definition: View.hpp:114
iterator end()
Definition: View.hpp:166
SparseSet< EntityType > entitySet
Definition: View.hpp:112
Definition: ComponentStorage.hpp:9
uint32_t entity
Definition: ecs_CONSTANTS.hpp:10
Definition: ecs_utils.hpp:18