TazGraph Project v0.1.0
Loading...
Searching...
No Matches
GECSManager.h
1#pragma once
2
3#include "GECS.h"
4#include "../../Grid/Grid.h"
5
6#include "../../Threader/Threader.h"
7#include <regex>
8#include <filesystem>
9
10namespace fs = std::filesystem;
11
12
14{
15private:
16 Threader* _threader = nullptr;
17 int lastEntityId = 0;
18 int negativeEntityId = -1;
19 std::vector<std::unique_ptr<Entity>> entities;
20
21 std::array<std::vector<EmptyEntity*>, maxGroups> groupedEmptyEntities;
22 std::array<std::vector<NodeEntity*>, maxGroups> groupedNodeEntities;
23 std::array<std::vector<LinkEntity*>, maxGroups> groupedLinkEntities;
24
25 std::vector<EmptyEntity*> visible_emptyEntities;
26 std::vector<NodeEntity*> visible_nodes;
27 std::vector<LinkEntity*> visible_links;
28
29 std::array<std::vector<EmptyEntity*>, maxGroups> visible_groupedEmptyEntities;
30 std::array<std::vector<NodeEntity*>, maxGroups> visible_groupedNodeEntities;
31 std::array<std::vector<LinkEntity*>, maxGroups> visible_groupedLinkEntities;
32
33 bool _update_active_entities = false;
34public:
35
36 std::vector<NodeEntity*> movedNodes;
37 std::mutex movedNodesMutex;
38
39 bool arrowheadsEnabled = false;
40 bool last_arrowheadsEnabled = false;
41
42 std::unordered_map<std::string, std::vector<std::string>> componentNames;
43
44 std::unique_ptr<Grid> grid;
45
46 Manager() {}
47
48 ~Manager() { _threader = nullptr; }
49
50 void setThreader(Threader& mthreader) {
51 _threader = &mthreader;
52 }
53
54 void update(float deltaTime = 1.0f)
55 {
57
58 if (_threader && !_threader->t_queue.shuttingDown) {
59
61
62 //_threader->parallel(visible_emptyEntities.size(), [&](int start, int end) {
63 // for (int i = start; i < end; i++) {
64 // if (visible_emptyEntities[i] && visible_emptyEntities[i]->isActive()) {
65 // visible_emptyEntities[i]->cellUpdate();
66 // }
67 // }
68 // });
69
70 //_threader->parallel(visible_nodes.size(), [&](int start, int end) {
71 // for (int i = start; i < end; i++) {
72 // if (visible_nodes[i] && visible_nodes[i]->isActive()) {
73 // visible_nodes[i]->cellUpdate();
74 // }
75 // }
76 // });
77
79 //? THIS MAY CAUSE ERRORS, IF REMOVE LINK FROM CELL AND OTHER LINK THAT HAS THAT CELL IN SEARCH
80 //? WILL PUMP IN AN EMPTY ELEMENT OR THE SIZE WILL BE SMALLER FOR THAT LINK TO FIND ELEMENT
81 for (auto& e : movedNodes) {
82 for (auto& link : e->getInLinks()) {
83 link->cellUpdate();
84 }
85 for (auto& link : e->getOutLinks()) {
86 link->cellUpdate();
87 }
88 }
89
90 _threader->parallel(movedNodes.size(), [&](int start, int end) {
91 for (int i = start; i < end; i++) {
92 for (auto& link : movedNodes[i]->getInLinks()) {
93 link->updateLinkToPorts();
94 }
95 }
96 });
97
98 _threader->parallel(movedNodes.size(), [&](int start, int end) {
99 for (int i = start; i < end; i++) {
100 for (auto& link : movedNodes[i]->getOutLinks()) {
101 link->updateLinkToPorts();
102 }
103 }
104 });
105
106 movedNodes.clear();
107
109 _threader->parallel(visible_emptyEntities.size(), [&](int start, int end) {
110 for (int i = start; i < end; i++) {
111 if (visible_emptyEntities[i] && visible_emptyEntities[i]->isActive()) {
112 visible_emptyEntities[i]->update(deltaTime);
113 }
114 }
115 });
116
117
118 _threader->parallel(visible_nodes.size(), [&](int start, int end) {
119 for (int i = start; i < end; i++) {
120 if (visible_nodes[i] && visible_nodes[i]->isActive()) {
121 visible_nodes[i]->update(deltaTime);
122 }
123 }
124
125 });
126
127
128 _threader->parallel(visible_links.size(), [&](int start, int end) {
129
130 for (int i = start; i < end; i++) {
131 if (visible_links[i] && visible_links[i]->isActive()) {
132 visible_links[i]->update(deltaTime);
133 }
134 }
135 });
136
137 }
138
140 else {
141
143
144 for (auto& e : movedNodes) {
145 for (auto& link : e->getInLinks()) {
146 link->cellUpdate();
147 }
148 for (auto& link : e->getOutLinks()) {
149 link->cellUpdate();
150 }
151 }
152
153 for (auto& e : movedNodes) {
154 for (auto& link : e->getInLinks()) {
155 link->updateLinkToPorts();
156 }
157 }
158
159 for (auto& e : movedNodes) {
160 for (auto& link : e->getOutLinks()) {
161 link->updateLinkToPorts();
162 }
163 }
164
165 movedNodes.clear();
166
167 for (auto& e : visible_emptyEntities) {
168 if (!e || !e->isActive()) continue;
169
170 e->update(deltaTime);
171 }
172
173 if (arrowheadsEnabled) {
174 for (auto& e : visible_nodes) {
175 if (!e || !e->isActive()) continue;
176
177 e->update(deltaTime);
178
179 }
180 }
181
182
183 for (auto& e : visible_links) {
184 if (!e || !e->isActive()) continue;
185
186 e->update(deltaTime);
187 }
188 }
189 }
190
191 // update fully will update all nodes and links in the world
192 void updateFully(float deltaTime = 1.0f)
193 {
194 // the links are updating once since after first update we check wether the nodes are aligned with the ownerCells
195 for (auto& e : entities) {
196 if (!e || !e->isActive()) continue;
197
198 e->update(deltaTime);
199 }
200 }
201
202 void refresh(ICamera* camera = nullptr)
203 {
204
205 if (grid && (camera->hasChanged() || grid->gridLevelChanged())) {
206 bool interceptedCellsChanged = grid->setIntersectedCameraCells(*camera);
207
208 if (interceptedCellsChanged) {
209 aboutTo_updateActiveEntities();
210 }
211 camera->refreshCamera();
212 }
213
214 if (_update_active_entities) {
215 _update_active_entities = false;
216
217 updateActiveEntities();
218 updateVisibleEntities();
219 }
220
221 }
222
223 void aboutTo_updateActiveEntities() {
224 _update_active_entities = true;
225 }
226
227 void updateActiveEntities();
228
229 void updateVisibleEntities();
230
231 void AddToGroup(EmptyEntity* mEntity, Group mGroup)
232 {
233 groupedEmptyEntities[mGroup].emplace_back(mEntity);
234 }
235
236 void AddToGroup(NodeEntity* mEntity, Group mGroup)
237 {
238 groupedNodeEntities[mGroup].emplace_back(mEntity);
239 }
240
241 void AddLinkToGroup(LinkEntity* mEntity, Group mGroup)
242 {
243 groupedLinkEntities[mGroup].emplace_back(mEntity);
244 }
245
246 const std::vector<std::unique_ptr<Entity>>& getEntities() const {
247 return entities;
248 }
249
250 template <typename T>
251 std::vector<T*> getVisible() {
252 if constexpr (std::is_same_v<T, EmptyEntity>) {
253 return visible_emptyEntities;
254 }
255 else if constexpr (std::is_same_v<T, NodeEntity>) {
256 return visible_nodes;
257 }
258 else if constexpr (std::is_same_v<T, LinkEntity>) {
259 return visible_links;
260 }
261 else {
262 static_assert(sizeof(T) == 0, "Unsupported entity type.");
263 }
264 }
265
266 template <typename T>
267 std::vector<T*>& getVisibleGroup(Group mGroup) {
268 if constexpr (std::is_same_v<T, EmptyEntity>) {
269 return visible_groupedEmptyEntities[mGroup];
270 }
271 else if constexpr (std::is_same_v<T, NodeEntity>) {
272 return visible_groupedNodeEntities[mGroup];
273 }
274 else if constexpr (std::is_same_v<T, LinkEntity>) {
275 return visible_groupedLinkEntities[mGroup];
276 }
277 else {
278 static_assert(sizeof(T) == 0, "Unsupported entity type.");
279 }
280 }
281
282 template <typename T>
283 std::vector<T*>& getGroup(Group mGroup) {
284 if constexpr (std::is_same_v<T, EmptyEntity>) {
285 return groupedEmptyEntities[mGroup];
286 }
287 else if constexpr (std::is_same_v<T, NodeEntity>) {
288 return groupedNodeEntities[mGroup];
289 }
290 else if constexpr (std::is_same_v<T, LinkEntity>) {
291 return groupedLinkEntities[mGroup];
292 }
293 else {
294 static_assert(sizeof(T) == 0, "Unsupported entity type.");
295 }
296 }
297
298 template <typename T, typename... TArgs>
299 T& addEntityNoId(TArgs&&... mArgs)
300 {
301 T* e(new T(*this, std::forward<TArgs>(mArgs)...));
302 e->setId(negativeEntityId--);
303 std::unique_ptr<T> uPtr{ e };
304 entities.emplace_back(std::move(uPtr));
305
306 return *e;
307 }
308
309 template <typename T, typename... TArgs>
310 T& addEntity(TArgs&&... mArgs)
311 {
312 T* e(new T(*this, std::forward<TArgs>(mArgs)...));
313 e->setId(lastEntityId++);
314 std::unique_ptr<T> uPtr{ e };
315 entities.emplace_back(std::move(uPtr));
316
317 return *e;
318 }
319
320 void resetEntityId() {
321 lastEntityId = 0;
322 }
323
324 Entity* getEntityFromId(unsigned int mId) {
325 for (auto& entity : entities) {
326 if (entity->getId() == mId && entity->isActive()) {
327 return &*entity;
328 }
329 }
330 return nullptr;
331 }
332
333 void clearAllEntities() {
334 for (auto& group : groupedNodeEntities) {
335 group.clear();
336 }
337 for (auto& group : groupedLinkEntities) {
338 group.clear();
339 }
340 entities.clear();
341 }
342
343 void removeAllEntites() {
344 for (std::size_t group = Manager::groupBackgroundLayer; group != Manager::buttonLabels; group++) {
345 removeAllEntitiesFromGroup(group);
346 removeAllEntitiesFromLinkGroup(group);
347 }
348 }
349
350 void removeAllEntitiesFromGroup(Group mGroup) {
351 auto& entitiesInGroup = groupedNodeEntities[mGroup];
352
353 for (Entity* entity : entitiesInGroup) {
354 entity->destroy();
355 }
356 }
357 void removeAllEntitiesFromLinkGroup(Group mGroup) {
358 auto& entitiesInGroup = groupedLinkEntities[mGroup];
359
360 for (Entity* entity : entitiesInGroup) {
361 entity->destroy();
362 }
363 }
364
365 std::vector<Entity*> adjacentEntities(Entity* mainEntity, Group group) {
366 std::vector<Entity*> nearbyEntities;
367
368 auto adjacentCells = grid->getAdjacentCells(*mainEntity, grid->getGridLevel());
369
370 for (Cell* adjCell : adjacentCells) {
371 for (auto& neighbor : adjCell->nodes) {
372 if (neighbor->hasGroup(group) && (neighbor != mainEntity) ) {
373 nearbyEntities.push_back(neighbor);
374 }
375 }
376 }
377
378 return nearbyEntities;
379 }
380
381 enum groupLabels : std::size_t //todo should add groups at end for some reason
382 {
383 //back
384 groupBackgroundLayer,
385 panelBackground,
386
387 //action
388 groupLinks_0,
389 groupGroupLinks_0,
390 groupGroupLinks_1,
391
392 groupArrowHeads_0,
393
394 groupNodes_0,
395 groupGroupNodes_0,
396 groupGroupNodes_1,
397 groupColliders,
398
399 groupEmpties,
400 groupSphereEmpties,
401
402 groupRenderSprites,
403
404 //fore
405 buttonLabels,
406 };
407
408 const std::unordered_map<Group, std::string> groupNames = {
409 {groupBackgroundLayer, "groupBackgroundLayer" },
410 {panelBackground, "panelBackground"},
411
412 //action
413 { groupLinks_0,"groupLinks_0" },
414 {groupGroupLinks_0, "groupGroupLinks_0"},
415 {groupGroupLinks_1, "groupGroupLinks_1"},
416
417 {groupArrowHeads_0, "groupArrowHeads_0"},
418
419 { groupNodes_0,"groupNodes_0" },
420 { groupGroupNodes_0, "groupGroupNodes_0"},
421 { groupGroupNodes_1, "groupGroupNodes_1"},
422
423 { groupEmpties,"groupEmpties" },
424 { groupSphereEmpties,"groupSphereEmpties" },
425
426 { groupColliders,"groupColliders" },
427 { groupRenderSprites,"groupRenderSprites" },
428
429
430 //fore
431 { buttonLabels,"buttonLabels" },
432 };
433
434 std::string getGroupName(Group mGroup) const;
435
436 void scanComponentNames(const std::string& folderPath);
437
438 void setComponentNames();
439};
Definition GECSEntity.h:7
Definition GECS.h:140
Definition ICamera.h:9
Definition GECSEntity.h:90
Definition GECSManager.h:14
void update(float deltaTime=1.0f)
Definition GECSManager.h:54
Definition GECSEntity.h:43
Definition CellEntity.h:5
Definition Threader.h:84