Skylicht Engine
Loading...
Searching...
No Matches
CEntityManager.h
1/*
2!@
3MIT License
4
5Copyright (c) 2019 Skylicht Technology CO., LTD
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
8(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
9merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
10subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20This file is part of the "Skylicht Engine".
21https://github.com/skylicht-lab/skylicht-engine
22!#
23*/
24
25#pragma once
26
27#include "IEntitySystem.h"
28#include "IRenderSystem.h"
29#include "CEntity.h"
30#include "CEntityGroup.h"
31
32#include "GameObject/CGameObject.h"
33#include "Camera/CCamera.h"
34#include "Transform/CWorldTransformData.h"
35
36#define MAX_ENTITY_DEPTH 256
37
38namespace Skylicht
39{
40 class IEntityManagerCallback
41 {
42 public:
43 IEntityManagerCallback()
44 {
45
46 }
47
48 virtual ~IEntityManagerCallback()
49 {
50
51 }
52
53 virtual void onEntityCreated(CEntity* entity)
54 {
55
56 }
57
58 virtual void onEntityCreated(CEntity** entity, int count)
59 {
60
61 }
62
63 virtual void onEntityRemoved(CEntity* entity)
64 {
65
66 }
67
68 virtual void onEntityRemoved(CEntity** entity, int count)
69 {
70
71 }
72 };
73
75
88 class SKYLICHT_API CEntityManager
89 {
90 protected:
91 core::array<CEntity*> m_alives;
92 core::array<CEntity*> m_entities;
93 core::array<CEntity*> m_unused;
94 core::array<CEntity*> m_delayRemove;
95
97
98 CFastArray<CEntity*> m_sortDepth[MAX_ENTITY_DEPTH];
99
100 std::vector<IEntitySystem*> m_systems;
101 std::vector<IRenderSystem*> m_renders;
102
103 std::vector<IEntitySystem*> m_sortUpdate;
104 std::vector<IRenderSystem*> m_sortRender;
105
106 std::vector<IEntityManagerCallback*> m_callbacks;
107
108 bool m_systemChanged;
109 bool m_rendererChanged;
110 bool m_needSortEntities;
111
112 CCamera* m_camera;
113
114 IRenderPipeline* m_renderPipeline;
115
116 public:
117 CEntityManager();
118
119 virtual ~CEntityManager();
120
121 void update();
122
123 void render();
124
125 void renderEmission();
126
127 void cullingAndRender();
128
129 protected:
130
131 int getDepth(CWorldTransformData* t);
132
133 void sortAliveEntities();
134
135 public:
136
137 void registerCallback(IEntityManagerCallback* callback);
138
139 void unRegisterCallback(IEntityManagerCallback* callback);
140
141 inline void setCamera(CCamera* camera)
142 {
143 m_camera = camera;
144 }
145
146 inline CCamera* getCamera()
147 {
148 return m_camera;
149 }
150
151 inline void setRenderPipeline(IRenderPipeline* p)
152 {
153 m_renderPipeline = p;
154 }
155
156 inline IRenderPipeline* getRenderPipeline()
157 {
158 return m_renderPipeline;
159 }
160
161 CEntity* createEntity();
162
163 CEntity** createEntity(int num, core::array<CEntity*>& entities);
164
165 void releaseAllEntities();
166
167 void releaseAllSystems();
168
169 void releaseAllGroups();
170
171 inline int getNumEntities()
172 {
173 return (int)m_entities.size();
174 }
175
176 inline CEntity* getEntity(int index)
177 {
178 return m_entities[index];
179 }
180
181 inline CEntity** getEntities()
182 {
183 return m_entities.pointer();
184 }
185
186 CEntity* getEntityByID(const char* id);
187
188 void removeEntity(int index);
189
190 void removeEntity(CEntity* entity);
191
192 void updateRemoveEntity();
193
194 template<class T>
195 T* addSystem();
196
197 template<class T>
198 T* addRenderSystem();
199
200 template<class T>
201 T* getSystem();
202
203 template<class T>
204 T* getRenderSystem();
205
206 bool removeSystem(IEntitySystem* system);
207
208 void addTransformDataToEntity(CEntity* entity, CTransform* transform);
209
210 void updateEntityParent(CEntity* entity);
211
212 CEntityGroup* addCustomGroup(CEntityGroup* group);
213
214 CEntityGroup* createGroupFromVisible(const u32* types, int count);
215
216 CEntityGroup* createGroup(const u32* types, int count);
217
218 CEntityGroup* createGroup(const u32* types, int count, CEntityGroup* parent);
219
220 CEntityGroup* findGroup(const u32* types, int count);
221
222 void removeGroup(CEntityGroup* group);
223
224 void removeAllGroup();
225
226 void notifyUpdateSortEntities();
227
228 void notifyUpdateGroup(u32 dataType);
229
230 inline void notifySystemOrderChanged()
231 {
232 m_systemChanged = true;
233 }
234
235 inline void notifyRendererOrderChanged()
236 {
237 m_rendererChanged = true;
238 }
239
240 protected:
241
242 void initDefaultData(CEntity* entity);
243
244 void sortRenderer();
245
246 void sortSystem();
247 };
248
249 template<class T>
250 T* CEntityManager::addSystem()
251 {
252 T* existSystem = getSystem<T>();
253 if (existSystem != NULL)
254 {
255 return existSystem;
256 }
257
258 T* newSystem = new T();
259
260 IEntitySystem* system = dynamic_cast<IEntitySystem*>(newSystem);
261 if (system == NULL)
262 {
263 char exceptionInfo[512];
264 sprintf(exceptionInfo, "CEntityManager::addSystem %s must inherit IEntitySystem", typeid(T).name());
265 os::Printer::log(exceptionInfo);
266 delete newSystem;
267 return NULL;
268 }
269
270 system->init(this);
271
272 m_systemChanged = true;
273
274 int order = (int)m_systems.size();
275 m_systems.push_back(system);
276 newSystem->setSystemOrder(order);
277
278 return newSystem;
279 }
280
281 template<class T>
282 T* CEntityManager::addRenderSystem()
283 {
284 T* system = getSystem<T>();
285 if (system != NULL)
286 {
287 return system;
288 }
289
290 T* newSystem = new T();
291
292 IRenderSystem* render = dynamic_cast<IRenderSystem*>(newSystem);
293 if (render == NULL)
294 {
295 char exceptionInfo[512];
296 sprintf(exceptionInfo, "CEntityManager::addRenderSystem %s must inherit IRenderSystem", typeid(T).name());
297 os::Printer::log(exceptionInfo);
298 delete newSystem;
299 return NULL;
300 }
301
302 render->init(this);
303
304 int order = (int)m_systems.size();
305
306 m_systems.push_back(render);
307 m_renders.push_back(render);
308
309 newSystem->setSystemOrder(order);
310
311 m_rendererChanged = true;
312
313 return newSystem;
314 }
315
316 template<class T>
317 T* CEntityManager::getSystem()
318 {
319 for (IEntitySystem*& s : m_systems)
320 {
321 T* system = dynamic_cast<T*>(s);
322 if (system != NULL)
323 {
324 return system;
325 }
326 }
327 return NULL;
328 }
329
330 template<class T>
331 T* CEntityManager::getRenderSystem()
332 {
333 for (IRenderSystem*& s : m_renders)
334 {
335 T* system = dynamic_cast<T*>(s);
336 if (system != NULL)
337 {
338 return system;
339 }
340 }
341 return NULL;
342 }
343}
This is an object class used to set up the camera, including its position, viewing angle,...
Definition CCamera.h:70
The object class is designed to optimize entity queries.
Definition CEntityGroup.h:54
This is the object class that describes an entity.
Definition CEntity.h:58
Definition CArrayUtils.h:31
An abstract object class for classes that describe a GameObject's Transform.
Definition CTransform.h:47
Definition CWorldTransformData.h:32
Definition CEntityManager.h:41
This is the interface for object classes responsible for processing Entity data.
Definition IEntitySystem.h:48
Definition IRenderPipeline.h:37
Self reallocating template array (like stl vector) with additional features.
Definition irrArray.h:23
Everything in the Skylicht Engine. You can start by looking at the topics.
Definition AudioDebugLog.h:29
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.h:58