Skylicht Engine
Loading...
Searching...
No Matches
CEntity.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 "IEntityData.h"
28#include "CEntityDataTypeManager.h"
29
30namespace Skylicht
31{
32 class CEntityManager;
33 class CEntityPrefab;
34
35#define GET_ENTITY_DATA(entity, DataType) ((DataType*)(entity->Data[DataType##_DataTypeIndex]))
36
37#define GET_LIST_ENTITY_DATA(DataType) { DataType##_DataTypeIndex }
38
39#define GET_LIST_ENTITY_DATA2(DataType1, DataType2) { DataType1##_DataTypeIndex, DataType2##_DataTypeIndex }
40
41#define MAX_ENTITY_DATA 64
42
45
57 class SKYLICHT_API CEntity
58 {
59 friend class CEntityManager;
60 friend class CEntityPrefab;
61
62 protected:
63 bool m_visible;
64 bool m_alive;
65 int m_index;
66 std::string m_id;
67
68 CEntityManager* m_mgr;
69 public:
70
71 IEntityData* Data[MAX_ENTITY_DATA];
72
73 public:
74 CEntity(CEntityManager* mgr);
75 CEntity(CEntityPrefab* mgr);
76
77 virtual ~CEntity();
78
79 void remove();
80
81 template<class T>
82 T* addData();
83
84 template<class T>
85 T* addData(int index);
86
87 IEntityData* addDataByActivator(const char* dataType);
88
89 inline CEntityManager* getEntityManager()
90 {
91 return m_mgr;
92 }
93
94 inline int getDataCount()
95 {
96 return MAX_ENTITY_DATA;
97 }
98
99 inline IEntityData* getDataByIndex(u32 dataIndex)
100 {
101 return Data[dataIndex];
102 }
103
104 inline void setID(const char* id)
105 {
106 m_id = id;
107 }
108
109 inline std::string& getID()
110 {
111 return m_id;
112 }
113
114 template<class T>
115 T* getData(); // "Replaced by getDataByIndex, which has an improved performance"
116
117 template<class T>
118 bool removeData();
119
120 bool removeData(u32 index);
121
122 void removeAllData();
123
124 inline int getIndex()
125 {
126 return m_index;
127 }
128
129 inline bool isAlive()
130 {
131 return m_alive;
132 }
133
134 inline bool isVisible()
135 {
136 return m_visible;
137 }
138
139 void setVisible(bool b);
140
141 void notifyUpdateGroup(int type);
142
143 protected:
144
145 inline void setAlive(bool b)
146 {
147 m_alive = b;
148 }
149
150 };
151
152 template<class T>
153 T* CEntity::addData()
154 {
155 T* newData = new T();
156 IEntityData* data = dynamic_cast<IEntityData*>(newData);
157 if (data == NULL)
158 {
159 char exceptionInfo[512];
160 sprintf(exceptionInfo, "CEntity::addData %s must inherit IEntityData", typeid(T).name());
161 os::Printer::log(exceptionInfo);
162
163 delete newData;
164 return NULL;
165 }
166
167 // get index of type
168 u32 index = CEntityDataTypeManager::getDataIndex(typeid(T));
169
170 // also save this entity index
171 data->EntityIndex = m_index;
172 data->Entity = this;
173
174 if (Data[index])
175 delete Data[index];
176
177 // save at index
178 Data[index] = newData;
179
180 notifyUpdateGroup(index);
181
182 return newData;
183 }
184
185 template<class T>
186 T* CEntity::addData(int index)
187 {
188 T* newData = new T();
189 IEntityData* data = dynamic_cast<IEntityData*>(newData);
190 if (data == NULL)
191 {
192 char exceptionInfo[512];
193 sprintf(exceptionInfo, "CEntity::addData %s must inherit IEntityData", typeid(T).name());
194 os::Printer::log(exceptionInfo);
195
196 delete newData;
197 return NULL;
198 }
199
200 // also save this entity index
201 data->EntityIndex = m_index;
202 data->Entity = this;
203
204 if (Data[index])
205 delete Data[index];
206
207 // save at index
208 Data[index] = newData;
209
210 notifyUpdateGroup(index);
211
212 return newData;
213 }
214
215 // "Replaced by getDataByIndex, which has an improved performance"
216 template<class T>
217 T* CEntity::getData()
218 {
219 for (u32 i = 0; i < MAX_ENTITY_DATA; i++)
220 {
221 if (Data[i])
222 {
223 T* t = dynamic_cast<T*>(Data[i]);
224 if (t)
225 {
226 return t;
227 }
228 }
229 }
230
231 return NULL;
232 }
233
234 template<class T>
235 bool CEntity::removeData()
236 {
237 u32 index = CEntityDataTypeManager::getDataIndex(typeid(T));
238
239 if (Data[index])
240 {
241 delete Data[index];
242 Data[index] = NULL;
243
244 notifyUpdateGroup(index);
245
246 return true;
247 }
248
249 return false;
250 }
251}
This object class manages all entities within a scene.
Definition CEntityManager.h:89
This object class is created to store data in an array of multiple CEntities.
Definition CEntityPrefab.h:38
This is the Interface for object classes that describe data to be attached to an entity.
Definition IEntityData.h:40
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