TazGraph Project v0.1.0
Loading...
Searching...
No Matches
Grid.h
1#pragma once
2#include "../GECS/Core/GECSEntity.h"
3#include "../GECS/Components.h"
4
5#include "../AABB/AABB.h"
6
7#include <vector>
8
9#include <cmath>
10
11constexpr int CELL_SIZE = 100;
12constexpr int AXIS_CELLS = 80;
13constexpr int DEPTH_AXIS_CELLS = 4;
14constexpr int ROW_CELL_SIZE = AXIS_CELLS * CELL_SIZE;
15constexpr int COLUMN_CELL_SIZE = AXIS_CELLS * CELL_SIZE;
16constexpr int DEPTH_CELL_SIZE = DEPTH_AXIS_CELLS * CELL_SIZE;
17
18
20 float numXCells, numYCells, numZCells;
21 float startX, endX, startY, endY, startZ, endZ;
22 float cameraMargin = 0.0f;
23};
24
25class Grid {
26public:
27 enum Level {
28 Basic,
29 Outer1,
30 Outer2
31 };
32
33 std::vector<EmptyEntity*> visible_emptyEntities;
34 std::vector<NodeEntity*> visible_nodes;
35 std::vector<LinkEntity*> visible_links;
36
37 Grid(int width, int height, int depth, int cellSize);
38 ~Grid();
39
40 void setSize(int cellSize);
41 void init(int width, int height, int depth, int cellSize);
42
43 void createCells(Grid::Level size);
44
45 void addLink(LinkEntity* link, Grid::Level m_level);
46 std::vector<Cell*> getLinkCells(const LinkEntity& link, Grid::Level m_level);
47 void addLink(LinkEntity* link, std::vector<Cell*> cell);
48
49 void addEmpty(EmptyEntity* entity, Grid::Level m_level);
50
51 void addNode(NodeEntity* entity, Grid::Level m_level);
52 void addEmpty(EmptyEntity* entity, Cell* cell);
53 void addNode(NodeEntity* entity, Cell* cell);
54
55 Cell* getCell(int x, int y, int z, Grid::Level m_level);
56 Cell* getCell(const Entity& position, Grid::Level m_level);
57 std::vector<Cell*> getAdjacentCells(int x, int y, int z, Grid::Level m_level);
58 std::vector<Cell*> getAdjacentCells(const Entity& entity, Grid::Level m_level);
59 std::vector<Cell>& getCells(Grid::Level m_level);
60 int getCellSize();
61 int getNumXCells();
62 int getNumYCells();
63 int getNumZCells();
64
65 bool setIntersectedCameraCells(ICamera& camera);
66
67 std::vector<Cell*> getIntersectedCameraCells(ICamera& camera);
68
69 // loops through the intrecepted cells and just get the entities
70 template <typename T>
71 std::vector<T*> getRevealedEntitiesInCameraCells() {
72 std::vector<T*> result;
73
74 if constexpr (std::is_same_v<T, NodeEntity>) {
75 for (auto& cell : _interceptedCells) {
76 for (auto& entity : cell->nodes) {
77 if (!entity->isHidden()) { // Check if the entity is visible
78 result.push_back(entity);
79
80 for (auto* port : entity->children) {
81 if (port && !port->isHidden()) {
82 visible_emptyEntities.push_back(port);
83
84 if (port->hasComponent<PortComponent>()) {
85 for (auto& portSlots : port->GetComponent<PortComponent>().portSlots)
86 visible_emptyEntities.push_back(portSlots);
87 }
88 }
89 }
90 }
91 }
92 }
93 }
94 else if constexpr (std::is_same_v<T, EmptyEntity>) {
95 for (auto& cell : _interceptedCells) {
96 for (auto& entity : cell->emptyEntities) {
97 if (!entity->isHidden()) { // Check if the entity is visible
98 result.push_back(entity);
99 }
100 }
101 }
102 }
103 else if constexpr (std::is_same_v<T, LinkEntity>) {
104 std::map<unsigned int, LinkEntity*> uniqueEntities;
105
106 for (auto& cell : _interceptedCells) {
107 for (auto& link : cell->links) {
108 if (!link->isHidden()) {
109 unsigned int linkId = link->getId();
110
111 if (uniqueEntities.find(linkId) == uniqueEntities.end()) {
112 uniqueEntities[linkId] = link;
113 }
114 }
115 }
116 }
117 for (auto& entry : uniqueEntities) {
118 result.push_back(entry.second);
119 }
120 }
121 else {
122 static_assert(sizeof(T) == 0, "Unsupported entity type.");
123 }
124 return result;
125
126 }
127
128 // loops through the intrecepted cells and just get the entities
129 template <typename T>
130 std::vector<T*> getEntitiesInCameraCells() {
131 std::vector<T*> result;
132
133 if constexpr (std::is_same_v<T, NodeEntity>) {
134 for (auto& cell : _interceptedCells) {
135 result.insert(result.end(), cell->nodes.begin(), cell->nodes.end());
136 }
137
138 for (auto& cell : _interceptedCells) {
139 for (auto& entity : cell->nodes) {
140 if (!entity->isHidden()) {
141 // Also include children(ports) if they exist
142 for (auto* port : entity->children) {
143 if (port && !port->isHidden()) {
144 visible_emptyEntities.push_back(port);
145
146 if (port->hasComponent<PortComponent>()) {
147 for (auto& portSlots : port->GetComponent<PortComponent>().portSlots)
148 visible_emptyEntities.push_back(portSlots);
149 }
150 }
151 }
152 }
153 }
154 }
155
156 }
157 else if constexpr (std::is_same_v<T, EmptyEntity>) {
158 for (auto& cell : _interceptedCells) {
159 result.insert(result.end(), cell->emptyEntities.begin(), cell->emptyEntities.end());
160 }
161 }
162 else {
163 static_assert(sizeof(T) == 0, "Unsupported entity type.");
164 }
165 return result;
166 }
167
168 std::vector<LinkEntity*> getLinksInCameraCells();
169
170
171 bool gridLevelChanged();
172
173 Level getGridLevel();
174 void setGridLevel(Level newLevel);
175
176 int getLevelCellScale();
177
178 int getLevelCellScale(Level level);
179private:
180 std::vector<Cell*> _interceptedCells;
181
182 std::vector<Cell> _cells;
183 std::vector<Cell> _parentCells;
184 std::vector<Cell> _superParentCells;
185
186 int _cellSize;
187
188 int _width;
189 int _height;
190 int _depth;
191
192 int _numXCells;
193 int _numYCells;
194 int _numZCells;
195
196 // can change between different scenes/managers
197 std::map<Level, GridLevelData> gridLevelsData;
198
199 std::map<Level, int> gridLevels = {
200 {Level::Basic, 1},
201 {Level::Outer1, 2},
202 {Level::Outer2, 4}
203 };
204
205 Level _level = Level::Basic;
206 Level _lastLevel = Level::Basic;
207};
Definition GECSEntity.h:7
Definition GECS.h:152
Definition Grid.h:25
Definition ICamera.h:14
Definition GECSEntity.h:107
Definition GECSEntity.h:43
Definition PortComponent.h:7
Definition CellEntity.h:5
Definition Grid.h:19