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
26
27 std::array<std::vector<EmptyEntity*>, maxGroups> visible_groupedEmptyEntities;
28 std::array<std::vector<NodeEntity*>, maxGroups> visible_groupedNodeEntities;
29 std::array<std::vector<LinkEntity*>, maxGroups> visible_groupedLinkEntities;
30
31 bool _update_active_entities = false;
32public:
33
34 std::vector<NodeEntity*> movedNodes;
35 std::mutex movedNodesMutex;
36
37 bool arrowheadsEnabled = false;
38 bool last_arrowheadsEnabled = false;
39
40 bool updateInnerPathLinks = 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 updateInnerPathLinks = true;
83 for (auto& link : e->getInLinks()) {
84 link->cellUpdate();
85 }
86 for (auto& link : e->getOutLinks()) {
87 link->cellUpdate();
88 }
89 }
90
91 _threader->parallel(movedNodes.size(), [&](int start, int end) {
92 for (int i = start; i < end; i++) {
93 for (auto& link : movedNodes[i]->getInLinks()) {
94 link->updateConnectedPorts();
95 }
96 }
97 });
98
99 _threader->parallel(movedNodes.size(), [&](int start, int end) {
100 for (int i = start; i < end; i++) {
101 for (auto& link : movedNodes[i]->getOutLinks()) {
102 link->updateConnectedPorts();
103 }
104 }
105 });
106
107 movedNodes.clear();
108
110 _threader->parallel(grid->visible_emptyEntities.size(), [&](int start, int end) {
111 for (int i = start; i < end; i++) {
112 if (grid->visible_emptyEntities[i] && grid->visible_emptyEntities[i]->isActive()) {
113 grid->visible_emptyEntities[i]->update(deltaTime);
114 }
115 }
116 });
117
118
119 _threader->parallel(grid->visible_nodes.size(), [&](int start, int end) {
120 for (int i = start; i < end; i++) {
121 if (grid->visible_nodes[i] && grid->visible_nodes[i]->isActive()) {
122 grid->visible_nodes[i]->update(deltaTime);
123 }
124 }
125
126 });
127
128
129 _threader->parallel(grid->visible_links.size(), [&](int start, int end) {
130
131 for (int i = start; i < end; i++) {
132 if (grid->visible_links[i] && grid->visible_links[i]->isActive()) {
133 grid->visible_links[i]->update(deltaTime);
134 }
135 }
136 });
137
138 }
139
141 else {
142
144
145 for (auto& e : movedNodes) {
146 for (auto& link : e->getInLinks()) {
147 link->cellUpdate();
148 }
149 for (auto& link : e->getOutLinks()) {
150 link->cellUpdate();
151 }
152 }
153
154 for (auto& e : movedNodes) {
155 for (auto& link : e->getInLinks()) {
156 link->updateConnectedPorts();
157 }
158 }
159
160 for (auto& e : movedNodes) {
161 for (auto& link : e->getOutLinks()) {
162 link->updateConnectedPorts();
163 }
164 }
165
166 movedNodes.clear();
167
168 for (auto& e : grid->visible_emptyEntities) {
169 if (!e || !e->isActive()) continue;
170
171 e->update(deltaTime);
172 }
173
174 if (arrowheadsEnabled) {
175 for (auto& e : grid->visible_nodes) {
176 if (!e || !e->isActive()) continue;
177
178 e->update(deltaTime);
179
180 }
181 }
182
183
184 for (auto& e : grid->visible_links) {
185 if (!e || !e->isActive()) continue;
186
187 e->update(deltaTime);
188 }
189 }
190 }
191
192 // update fully will update all nodes and links in the world
193 void updateFully(float deltaTime = 1.0f)
194 {
195 // the links are updating once since after first update we check wether the nodes are aligned with the ownerCells
196 for (auto& e : entities) {
197 if (!e || !e->isActive()) continue;
198
199 e->update(deltaTime);
200 }
201 }
202
203 void refresh(ICamera* camera = nullptr)
204 {
205
206 if (grid && (camera->hasChanged() || grid->gridLevelChanged())) {
207 bool interceptedCellsChanged = grid->setIntersectedCameraCells(*camera);
208
209 if (interceptedCellsChanged) {
210 aboutTo_updateActiveEntities();
211 }
212 camera->refreshCamera();
213 }
214
215 if (_update_active_entities) {
216 _update_active_entities = false;
217
218 updateActiveEntities();
219 updateVisibleEntities();
220 }
221
222 }
223
224 void aboutTo_updateActiveEntities() {
225 _update_active_entities = true;
226 }
227
228 void updateActiveEntities();
229
230 void updateVisibleEntities();
231
232 void AddToGroup(EmptyEntity* mEntity, Group mGroup)
233 {
234 groupedEmptyEntities[mGroup].emplace_back(mEntity);
235 }
236
237 void AddToGroup(NodeEntity* mEntity, Group mGroup)
238 {
239 groupedNodeEntities[mGroup].emplace_back(mEntity);
240 }
241
242 void AddLinkToGroup(LinkEntity* mEntity, Group mGroup)
243 {
244 groupedLinkEntities[mGroup].emplace_back(mEntity);
245 }
246
247 const std::vector<std::unique_ptr<Entity>>& getEntities() const {
248 return entities;
249 }
250
251 template <typename T>
252 std::vector<T*> getVisible() {
253 if constexpr (std::is_same_v<T, EmptyEntity>) {
254 return grid->visible_emptyEntities;
255 }
256 else if constexpr (std::is_same_v<T, NodeEntity>) {
257 return grid->visible_nodes;
258 }
259 else if constexpr (std::is_same_v<T, LinkEntity>) {
260 return grid->visible_links;
261 }
262 else {
263 static_assert(sizeof(T) == 0, "Unsupported entity type.");
264 }
265 }
266
267 template <typename T>
268 std::vector<T*>& getVisibleGroup(Group mGroup) {
269 if constexpr (std::is_same_v<T, EmptyEntity>) {
270 return visible_groupedEmptyEntities[mGroup];
271 }
272 else if constexpr (std::is_same_v<T, NodeEntity>) {
273 return visible_groupedNodeEntities[mGroup];
274 }
275 else if constexpr (std::is_same_v<T, LinkEntity>) {
276 return visible_groupedLinkEntities[mGroup];
277 }
278 else {
279 static_assert(sizeof(T) == 0, "Unsupported entity type.");
280 }
281 }
282
283 template <typename T>
284 std::vector<T*>& getGroup(Group mGroup) {
285 if constexpr (std::is_same_v<T, EmptyEntity>) {
286 return groupedEmptyEntities[mGroup];
287 }
288 else if constexpr (std::is_same_v<T, NodeEntity>) {
289 return groupedNodeEntities[mGroup];
290 }
291 else if constexpr (std::is_same_v<T, LinkEntity>) {
292 return groupedLinkEntities[mGroup];
293 }
294 else {
295 static_assert(sizeof(T) == 0, "Unsupported entity type.");
296 }
297 }
298
299 template <typename T, typename... TArgs>
300 T& addEntityNoId(TArgs&&... mArgs)
301 {
302 T* e(new T(*this, std::forward<TArgs>(mArgs)...));
303 e->setId(negativeEntityId--);
304 std::unique_ptr<T> uPtr{ e };
305 entities.emplace_back(std::move(uPtr));
306
307 return *e;
308 }
309
310 template <typename T, typename... TArgs>
311 T& addEntity(TArgs&&... mArgs)
312 {
313 T* e(new T(*this, std::forward<TArgs>(mArgs)...));
314 e->setId(lastEntityId++);
315 std::unique_ptr<T> uPtr{ e };
316 entities.emplace_back(std::move(uPtr));
317
318 return *e;
319 }
320
321 void resetEntityId() {
322 lastEntityId = 0;
323 }
324
325 Entity* getEntityFromId(unsigned int mId) {
326 for (auto& entity : entities) {
327 if (entity->getId() == mId && entity->isActive()) {
328 return &*entity;
329 }
330 }
331 return nullptr;
332 }
333
334 void clearAllEntities() {
335 for (auto& group : groupedNodeEntities) {
336 group.clear();
337 }
338 for (auto& group : groupedLinkEntities) {
339 group.clear();
340 }
341 entities.clear();
342 }
343
344 void removeAllEntites() {
345 for (std::size_t group = Manager::groupBackgroundLayer; group != Manager::buttonLabels; group++) {
346 removeAllEntitiesFromGroup(group);
347 removeAllEntitiesFromLinkGroup(group);
348 }
349 }
350
351 void removeAllEntitiesFromGroup(Group mGroup) {
352 auto& entitiesInGroup = groupedNodeEntities[mGroup];
353
354 for (Entity* entity : entitiesInGroup) {
355 entity->destroy();
356 }
357 }
358
359 void removeAllEntitiesFromEmptyGroup(Group mGroup) {
360 auto& entitiesInGroup = groupedEmptyEntities[mGroup];
361
362 for (Entity* entity : entitiesInGroup) {
363 entity->destroy();
364 }
365 }
366
367 void removeAllEntitiesFromLinkGroup(Group mGroup) {
368 auto& entitiesInGroup = groupedLinkEntities[mGroup];
369
370 for (Entity* entity : entitiesInGroup) {
371 entity->destroy();
372 }
373 }
374
375 std::vector<Entity*> adjacentEntities(Entity* mainEntity, Group group) {
376 std::vector<Entity*> nearbyEntities;
377
378 auto adjacentCells = grid->getAdjacentCells(*mainEntity, grid->getGridLevel());
379
380 for (Cell* adjCell : adjacentCells) {
381 for (auto& neighbor : adjCell->nodes) {
382 if (neighbor->hasGroup(group) && (neighbor != mainEntity) ) {
383 nearbyEntities.push_back(neighbor);
384 }
385 }
386 }
387
388 return nearbyEntities;
389 }
390
391 enum groupLabels : std::size_t //todo should add groups at end for some reason
392 {
393 //back
394 groupBackgroundLayer,
395 panelBackground,
396
397 //action
398 groupLinks_0,
399 groupGroupLinks_0,
400 groupGroupLinks_1,
401
402 groupPathLinks_0,
403 groupPathLinks_1,
404 groupPathLinks_2,
405
406 groupPathInnerLinks,
407
408 groupPathLinksHolder,
409
410 groupArrowHeads_0,
411
412 groupNodes_0,
413 groupGroupNodes_0,
414 groupGroupNodes_1,
415
416 groupMinimapNodes,
417
418 groupColliders,
419
420 groupEmpties,
421 groupSphereEmpties,
422
423 groupRenderSprites,
424
425 groupPorts,
426 groupPortSlots,
427
428 //fore
429 buttonLabels,
430 };
431
432 const std::unordered_map<Group, std::string> groupNames = {
433 {groupBackgroundLayer, "groupBackgroundLayer" },
434 {panelBackground, "panelBackground"},
435
436 //action
437 { groupLinks_0,"groupLinks_0" },
438 {groupGroupLinks_0, "groupGroupLinks_0"},
439 {groupGroupLinks_1, "groupGroupLinks_1"},
440
441 {groupPathLinks_0, "groupPathLinks_0"},
442 {groupPathLinks_1, "groupPathLinks_1"},
443 {groupPathLinks_2, "groupPathLinks_2"},
444
445 {groupPathInnerLinks, "groupPathInnerLinks"},
446
447 {groupPathLinksHolder, "groupPathLinksHolder"},
448
449 {groupArrowHeads_0, "groupArrowHeads_0"},
450
451 { groupNodes_0,"groupNodes_0" },
452 { groupGroupNodes_0, "groupGroupNodes_0"},
453 { groupGroupNodes_1, "groupGroupNodes_1"},
454
455 { groupMinimapNodes,"groupMinimapNodes" },
456
457 { groupEmpties,"groupEmpties" },
458 { groupSphereEmpties,"groupSphereEmpties" },
459
460 { groupColliders,"groupColliders" },
461 { groupRenderSprites,"groupRenderSprites" },
462 { groupPorts,"groupPorts" },
463 { groupPortSlots,"groupPortSlots" },
464
465 //fore
466 { buttonLabels,"buttonLabels" },
467 };
468
469 std::string getGroupName(Group mGroup) const;
470
471 void scanComponentNames(const std::string& folderPath);
472
473 void setComponentNames();
474};
Definition GECSEntity.h:7
Definition GECS.h:152
Definition ICamera.h:14
Definition GECSEntity.h:107
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