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