TazGraph Project v0.1.0
Loading...
Searching...
No Matches
GECS.h
1#pragma once
2
3#include <iostream>
4#include <vector>
5#include <memory>
6#include <algorithm>
7#include <bitset>
8#include <array>
9#include <unordered_map>
10
11#include <SDL2/SDL.h>
12#include "../../Renderers/PlaneRenderers/PlaneModelRenderer/PlaneModelRenderer.h"
13#include "../../Renderers/LineRenderer/LineRenderer.h"
14#include "../../Renderers/PlaneRenderers/PlaneColorRenderer/PlaneColorRenderer.h"
15#include "../../Renderers/PlaneRenderers/LightRenderer/LightRenderer.h"
16#include "../../Camera2.5D/CameraManager.h"
17#include "../../Window/Window.h"
18#include "../../ConsoleLogger.h"
19
20#include <optional>
21
22#define CULLING_OFFSET 100
23
24class BaseComponent;
25class Entity;
26class EmptyEntity;
27class NodeEntity;
28class LinkEntity;
29
30class Manager;
31class Window;
32struct Cell;
33
34using ComponentID = std::size_t;
35using Group = std::size_t;
36
37using layer = std::size_t;
38
39namespace Layer {
40 enum layerIndexes : std::size_t
41 {
42 action,
43 menubackground
44 };
45}
46
47const std::unordered_map<layer, float> layerNames = {
48 {Layer::action, 0.0f},
49 {Layer::menubackground, -100.0f}
50
51};
52
53inline float getLayerDepth(layer mLayer) {
54 return layerNames.at(mLayer);
55}
56
57
58inline ComponentID getNewComponentTypeID()
59{
60 static ComponentID lastID = 0u;
61 return lastID++;
62}
63
64inline ComponentID getNewNodeComponentTypeID()
65{
66 static ComponentID lastID_nodeC = 0u;
67 return lastID_nodeC++;
68}
69
70inline ComponentID getNewLinkComponentTypeID()
71{
72 static ComponentID lastID_linkC = 0u;
73 return lastID_linkC++;
74}
75
76
77template <typename T> inline ComponentID GetComponentTypeID() noexcept
78{
79 static ComponentID typeID = getNewComponentTypeID(); // typeID is unique for each function type T and only initialized once.
80 return typeID;
81}
82
83template <typename T> inline ComponentID GetNodeComponentTypeID() noexcept
84{
85 static ComponentID typeID = getNewNodeComponentTypeID(); // typeID is unique for each function type T and only initialized once.
86 return typeID;
87}
88
89template <typename T> inline ComponentID GetLinkComponentTypeID() noexcept
90{
91 static ComponentID typeID = getNewLinkComponentTypeID(); // typeID is unique for each function type T and only initialized once.
92 return typeID;
93}
94
95constexpr std::size_t maxComponents = 16;
96constexpr std::size_t maxGroups = 24;
97
98using ComponentBitSet = std::bitset<maxComponents>;
99using GroupBitSet = std::bitset<maxGroups>;
100
101using ComponentArray = std::array<BaseComponent*, maxComponents>;
102
104{
105public:
106
107 ComponentID id = 0u;
108
109 virtual void init() {}
110 virtual void update(float deltaTime) {}
111 virtual void draw(size_t e_index, PlaneModelRenderer& batch, TazGraphEngine::Window& window) {}
112 virtual void draw(size_t e_index, LineRenderer& batch, TazGraphEngine::Window& window) {}
113 virtual void draw(size_t e_index, PlaneColorRenderer& batch, TazGraphEngine::Window& window) {}
114 virtual void draw(size_t e_index, LightRenderer& batch, TazGraphEngine::Window& window) {}
115
116 virtual std::string GetComponentName() { return ""; };
117
118 virtual void showGUI() {
119 ImGui::Text("MyComponent Properties:");
120 };
121
122 virtual ~BaseComponent() {}
123};
124
125class Component : public BaseComponent {
126public:
127 Entity* entity = nullptr;
128};
129
131public:
132 NodeEntity* entity = nullptr;
133};
134
136public:
137 LinkEntity* entity = nullptr;
138};
139
140enum NodePorts {
141 TOP = 0,
142 RIGHT = 1,
143 BOTTOM = 2,
144 LEFT = 3
145};
146
147enum LinkPorts {
148 ARROWHEAD = 0
149};
150
152{
153private:
154 unsigned int id = 0;
155
156 bool active = true; // false if about to delete
157 bool hidden = false; // true if not do updates
158 ComponentArray componentArray = {};//create 2 arrays, this is for the fast access
159
160 ComponentBitSet componentBitSet;
161 GroupBitSet groupBitSet;
162
163protected:
164 std::optional<ComponentArray> nodeComponentArray;
165 std::optional<ComponentBitSet> nodeComponentBitSet;
166
167 Manager& manager;
168public:
169 std::vector<EmptyEntity*> children;
170
171 void setId(unsigned int m_id) { id = m_id; }
172 unsigned int getId() { return id; }
173
174 void hide() {
175 hidden = true;
176 }
177
178 void reveal() {
179 hidden = false;
180 }
181
182 bool isHidden() {
183 return hidden;
184 }
185
186 std::vector<std::unique_ptr<BaseComponent>> components; //create 2 arrays, this is for the concurrent access
187
188 Entity(Manager& mManager) : manager(mManager) {}
189 virtual ~Entity() {}
190
191 virtual void update(float deltaTime)
192 {
193
194 for (auto& c : components) {
195 c->update(deltaTime); // start from which was added first
196 }
197 }
198
199 virtual void cellUpdate() {};
200
201 virtual Cell* getOwnerCell() const { return nullptr; };
202
203 void draw(size_t e_index, PlaneModelRenderer& batch, TazGraphEngine::Window& window)
204 {
205 for (auto& c : components) {
206 c->draw(e_index, batch, window);
207 }
208 }
209 void draw(size_t e_index, LineRenderer& batch, TazGraphEngine::Window& window)
210 {
211 for (auto& c : components) {
212 c->draw(e_index, batch, window);
213 }
214 }
215 void draw(size_t e_index, PlaneColorRenderer& batch, TazGraphEngine::Window& window)
216 {
217 for (auto& c : components) {
218 c->draw(e_index, batch, window);
219 }
220 }
221 void draw(size_t e_index, LightRenderer& batch, TazGraphEngine::Window& window)
222 {
223 for (auto& c : components) {
224 c->draw(e_index, batch, window);
225 }
226 }
227 bool isActive() { return active; }
228 virtual void destroy() { active = false;
229 } // destroy happens relative to the group referencing
230
231 bool hasGroup(Group mGroup)
232 {
233 return groupBitSet[mGroup];
234 }
235
236 virtual void addGroup(Group mGroup);
237 void removeGroup(Group mGroup);
238
239 template <typename T> bool hasComponent() const
240 {
241 if constexpr (std::is_base_of_v<LinkComponent, T>) {
242 return this && componentBitSet[GetLinkComponentTypeID<T>()];
243 }
244 else if constexpr (std::is_base_of_v<NodeComponent, T>) {
245 return this && nodeComponentBitSet.has_value() && (*nodeComponentBitSet)[GetNodeComponentTypeID<T>()];
246 }
247 return this && componentBitSet[GetComponentTypeID<T>()];
248 }
250 template <typename T, typename... TArgs>
251 T& addComponent(TArgs&&... mArgs)
252 {
253 T* c(new T(std::forward<TArgs>(mArgs)...));
254 if constexpr (std::is_base_of_v<LinkComponent, T>) {
255 std::unique_ptr<LinkComponent> uPtr{ c };
256 components.emplace_back(std::move(uPtr));
257
258 setComponentEntity(c);
259 componentArray[GetLinkComponentTypeID<T>()] = c;
260 componentBitSet[GetLinkComponentTypeID<T>()] = true;
261
262 c->id = GetLinkComponentTypeID<T>();
263
264 c->init();
265 return *c;
266 }
267 else if constexpr (std::is_base_of_v<NodeComponent, T>) {
268 std::unique_ptr<NodeComponent> uPtr{ c };
269 components.emplace_back(std::move(uPtr));
270
271 setComponentEntity(c);
272 (*nodeComponentArray)[GetNodeComponentTypeID<T>()] = c;
273 (*nodeComponentBitSet)[GetNodeComponentTypeID<T>()] = true;
274
275 c->id = GetNodeComponentTypeID<T>();
276
277 c->init();
278 return *c;
279 }
280 else {
281 std::unique_ptr<Component> uPtr{ c };
282 components.emplace_back(std::move(uPtr));
283
284 setComponentEntity(c);
285 componentArray[GetComponentTypeID<T>()] = c;
286 componentBitSet[GetComponentTypeID<T>()] = true;
287
288 c->id = GetComponentTypeID<T>();
289
290 c->init();
291 return *c;
292 }
293
294
295 }
296
297 template <typename T>
298 void removeComponent()
299 {
300 if constexpr (std::is_base_of_v<LinkComponent, T>)
301 {
302 size_t id = GetLinkComponentTypeID<T>();
303 auto it = std::remove_if(components.begin(), components.end(),
304 [id](const std::unique_ptr<BaseComponent>& comp) {
305 return typeid(*comp).hash_code() == typeid(T).hash_code();
306 });
307
308 if (it != components.end())
309 {
310 components.erase(it, components.end());
311 componentArray[id] = nullptr;
312 componentBitSet[id] = false;
313 }
314 }
315 else if constexpr (std::is_base_of_v<NodeComponent, T>)
316 {
317 size_t id = GetNodeComponentTypeID<T>();
318 auto it = std::remove_if(components.begin(), components.end(),
319 [id](const std::unique_ptr<BaseComponent>& comp) {
320 return typeid(*comp).hash_code() == typeid(T).hash_code();
321 });
322
323 if (it != components.end())
324 {
325 components.erase(it, components.end());
326 (*nodeComponentArray)[id] = nullptr;
327 (*nodeComponentBitSet)[id] = false;
328 }
329 }
330 else
331 {
332 size_t id = GetComponentTypeID<T>();
333 auto it = std::remove_if(components.begin(), components.end(),
334 [id](const std::unique_ptr<BaseComponent>& comp) {
335 return typeid(*comp).hash_code() == typeid(T).hash_code();
336 });
337
338 if (it != components.end())
339 {
340 components.erase(it, components.end());
341 componentArray[id] = nullptr;
342 componentBitSet[id] = false;
343 }
344 }
345 }
346
347 virtual void setComponentEntity(Component* c) {
348
349 }
350 virtual void setComponentEntity(NodeComponent* c) {
351
352 }
353 virtual void setComponentEntity(LinkComponent* c) {
354
355 }
356
357 template<typename T> T& GetComponent() const
358 {
359 if constexpr (std::is_base_of_v<LinkComponent, T>) {
360 auto ptr(componentArray[GetLinkComponentTypeID<T>()]);
361 return *static_cast<T*>(ptr);
362 }
363 else if constexpr (std::is_base_of_v<NodeComponent, T>) {
364 auto ptr((*nodeComponentArray)[GetNodeComponentTypeID<T>()]);
365 return *static_cast<T*>(ptr);
366 }
367 else {
368 auto ptr(componentArray[GetComponentTypeID<T>()]);
369 return *static_cast<T*>(ptr);
370 }
371 }
372
373 bool hasComponentByName(const std::string& componentName) {
374 for (auto& component : components) {
375 if (component &&
376 component->GetComponentName() == componentName) {
377 return true;
378 }
379 }
380 return false;
381 }
382
383
384
385 // for when wanting to add new entities from components
386 Manager* getManager() {
387 return &manager;
388 }
389
390 virtual void addMessage(std::string mMessage) {}
391
392
393 virtual Entity* getParentEntity() {
394 return nullptr;
395 }
396
397 virtual void setParentEntity(Entity* pEntity) {}
398
399 virtual void imgui_print() {}
400
401 virtual void imgui_display() {}
402
403 virtual void removeEntity() {}
404};
405
Definition GECS.h:104
Definition GECS.h:125
Definition GECSEntity.h:7
Definition GECS.h:152
T & addComponent(TArgs &&... mArgs)
have addScript function
Definition GECS.h:251
Definition LightRenderer.h:20
Definition LineRenderer.h:131
Definition GECS.h:135
Definition GECSEntity.h:107
Definition GECSManager.h:14
Definition GECS.h:130
Definition GECSEntity.h:43
Definition PlaneColorRenderer.h:19
Definition PlaneModelRenderer.h:21
Definition Window.h:18
Definition CellEntity.h:5