Fission FSN
Loading...
Searching...
No Matches
fsCEntity.h
Go to the documentation of this file.
1/********************************************************************
2*
3* File: fsCEntity.h
4*
5* Purpose: Core container for components.
6*
7* Interface components via fsCUniqueId which is also
8* handily wrapped up using templates to make the client
9* code cleaner.
10*
11* Based on fsCUniqueId so you can't remove the interface
12* to it!
13*
14*********************************************************************/
15
16#ifndef fsCEntityhdr
17#define fsCEntityhdr
18
19#include <typeinfo>
20#include <map>
21#include <vector>
22#include <algorithm>
23#include <memory>
24
25
26#include "../fsINode.h"
27
28class fsIComponent;
29class fsCUniqueId;
32class fsIScene;
37
38class fsCEntity : public fsINode
39{
40 friend class fsCEntityFactory;
41 friend class gfCTestLoadGuis;
42 friend class fsIGuiComponent;
43
44public:
46
47 void listenersRegister();
49
50 static void componentFactorySet(std::shared_ptr<fsCComponentFactory> pComponentFactory);
52 static void entityFactorySet(std::shared_ptr<fsCEntityFactory> pEntityFactory);
54
57
60
61 fsBool tagExists(fsStr const& pTag) const;
62 void tagAdd(fsStr const& pTag);
63
64 template <typename TComponent>
65 TComponent* componentAdd();
66 fsIComponent *componentAdd(const fsCUniqueId& pTypeId);
67
68 template <typename TComponent>
69 TComponent* componentGet() const;
70 fsIComponent *componentGet(const fsCUniqueId& pTypeId) const;
71
72 void componentDestroy(const fsCUniqueId& pTypeId);
73
74 template <typename TInterface>
75 TInterface* firstComponentOfFamilyType() const;
76
77 template <typename TLamda>
78 void forMyChildren(TLamda&& pLamda);
79 template <typename TLamda>
80 void forAllChildren(TLamda&& pLamda);
81
82 fsCEntity* childFind(const fsStr& pName, fsBool pCaseless = false);
83
84 template <typename TInterface>
85 std::vector<TInterface*> componentsOfFamilyTypeGet() const;
86 template <typename TInterface>
87 std::vector<fsCEntity*> childEntitiesWithComponentsOfFamilyType();
88 std::vector<fsCEntity*> childEntitiesWithComponentsOfTypeGet(
89 const std::vector<const std::type_info*>& pPossibleComponentTypes);
90 std::vector<fsCEntity*> childEntitiesTaggedWithGet(std::vector<fsStr> pTags);
91
92 template <typename TInterface>
94
95protected:
96
97 fsCEntity(const fsStr& pName);
98 virtual ~fsCEntity();
99
100 void initialiseFromVariableTableDo(const fsCVariableTable& pVars) override;
101
102 void updateDo(fsS32 pDeltaMs) override;
103 void renderDo() override;
104 void updatedMatrixProcess() override;
105
106 void initDo() override;
107 void resetDo() override;
108 void serialiseDo() override;
109 void deserialiseDo() override;
110 void postLoad() override;
111 void postLinkDo() override;
112
113private:
114
115
116 void componentUpdateMethodExecute(void (fsIComponent::*pMethod)());
117
118 void deferredComponentsAdd();
119 void deferredComponentsRemove();
120
121 void componentUnregisterAndDestroy(const fsCUniqueId& pTypeId);
122
123 void childEntitiesWithComponentsOfTypeGetDo(std::vector<fsCEntity*>& pVectorToFill,
124 const std::vector<const std::type_info*>& pPossibleComponentTypes);
125 void childEntitiesTaggedWithGetDo(std::vector<fsStr> pTags, std::vector<fsCEntity*>& pEnts);
126
127 std::map<fsU32, fsIComponent*> mComponentsMap;
128 std::vector<fsIComponent*> mComponentsVector;
129
130 std::vector<std::pair<fsIComponent*, fsU32>> mComponentsToAdd;
131 std::vector<fsCUniqueId> mComponentsToDestroy;
132
133 fsBool mInComponentUpdate;
134
135 static std::shared_ptr<fsCEntityFactory> mEntityFactory;
136 static std::shared_ptr<fsCComponentFactory> mComponentFactory;
137};
138
139
140template <typename TInterface>
142{
143 for (auto iter = mComponentsMap.begin(); iter != mComponentsMap.end(); ++iter)
144 {
145 if (auto* result = dynamic_cast<TInterface*>(iter->second))
146 {
147 return result;
148 }
149 }
150
151 for (auto iter = mComponentsToAdd.begin(); iter != mComponentsToAdd.end(); ++iter)
152 {
153 if (auto* result = dynamic_cast<TInterface*>(iter->first))
154 {
155 return result;
156 }
157 }
158 return nullptr;
159}
160
161template <typename TInterface>
162std::vector<TInterface*> fsCEntity::componentsOfFamilyTypeGet() const
163{
164 std::vector<TInterface*> comps;
165 for (auto iter = mComponentsMap.begin(); iter != mComponentsMap.end(); ++iter)
166 {
167 if (auto* result = dynamic_cast<TInterface*>(iter->second))
168 {
169 comps.push_back(result);
170 }
171 }
172
173 for (auto compIt = mComponentsToAdd.begin(); compIt != mComponentsToAdd.end(); ++compIt)
174 {
175 if (auto* result = dynamic_cast<TInterface*>(compIt->first))
176 {
177 comps.push_back(result);
178 }
179 }
180 return comps;
181}
182
183
184template <typename TInterface>
186{
187 std::vector<fsCEntity*> ents;
188
189 for (fsINode* child : mChildren)
190 {
191 if (fsCEntity* childEnt = dynamic_cast<fsCEntity*>(child))
192 {
193 auto ret = childEnt->childEntitiesWithComponentsOfFamilyType<TInterface>();
194 ents.insert(ents.end(), ret.begin(), ret.end());
195 }
196 }
197
198 for (auto iter = mComponentsMap.begin(); iter != mComponentsMap.end(); ++iter)
199 {
200 if (dynamic_cast<TInterface*>(iter->second))
201 {
202 ents.push_back(this);
203 }
204 }
205 return ents;
206}
207
208template <typename TComponent>
209TComponent* fsCEntity::componentGet() const
210{
211 return dynamic_cast<TComponent* const>(componentGet(TComponent::mTypeId));
212}
213
214template <typename TComponent>
216{
217 return dynamic_cast<TComponent* const>(componentAdd(TComponent::mTypeId));
218}
219
220template <typename TInterface>
222{
223 for (fsINode* child : mChildren)
224 {
225 if (fsCEntity* childEnt = dynamic_cast<fsCEntity*>(child))
226 {
227 if (auto matchEnt = childEnt->firstChildEntityWithComponentsOfFamilyType<TInterface>())
228 {
229 return matchEnt;
230 }
231 }
232 }
233
234 for (auto component : mComponentsMap)
235 {
236 if (dynamic_cast<TInterface*>(component.second))
237 {
238 return this;
239 }
240 }
241 return nullptr;
242}
243
244template <typename TLamda>
245void fsCEntity::forAllChildren(TLamda&& pLamda)
246{
247 std::for_each(mChildren.begin(), mChildren.end(),
248 [&pLamda](fsINode* node)
249 {
250 if (fsCEntity* const ent = dynamic_cast<fsCEntity*>(node))
251 {
252 ent->forAllChildren(pLamda);
253 }
254 });
255 pLamda(this);
256}
257
258template <typename TLamda>
259void fsCEntity::forMyChildren(TLamda&& pLamda)
260{
261 std::for_each(mChildren.begin(), mChildren.end(), pLamda);
262}
263
264#endif
Definition fsCComponentFactory.h:26
Definition fsCEntityFactory.h:25
void initialiseFromVariableTableDo(const fsCVariableTable &pVars) override
Definition fsCEntity.cpp:135
static void componentFactorySet(std::shared_ptr< fsCComponentFactory > pComponentFactory)
Definition fsCEntity.cpp:49
void resetDo() override
Definition fsCEntity.cpp:194
void forAllChildren(TLamda &&pLamda)
Definition fsCEntity.h:245
TComponent * componentAdd()
Definition fsCEntity.h:215
friend class gfCTestLoadGuis
Definition fsCEntity.h:41
void renderDo() override
Definition fsCEntity.cpp:184
fsCEntity * firstChildEntityWithComponentsOfFamilyType()
Definition fsCEntity.h:221
fsISpriteComponent * spriteComponentAdd()
Definition fsCEntity.cpp:98
void listenersRegister()
Definition fsCEntity.cpp:19
fsITextSpriteComponent * textComponentGet() const
Definition fsCEntity.cpp:93
static void entityFactorySet(std::shared_ptr< fsCEntityFactory > pEntityFactory)
Definition fsCEntity.cpp:59
fsITextSpriteComponent * textComponentAdd()
Definition fsCEntity.cpp:88
fsCEntity * childFind(const fsStr &pName, fsBool pCaseless=false)
Definition fsCEntity.cpp:199
void listenersDeRegister()
Definition fsCEntity.cpp:31
fsCEntityFactory * entityFactoryGet()
Definition fsCEntity.cpp:64
void forMyChildren(TLamda &&pLamda)
Definition fsCEntity.h:259
std::vector< fsCEntity * > childEntitiesWithComponentsOfTypeGet(const std::vector< const std::type_info * > &pPossibleComponentTypes)
Definition fsCEntity.cpp:302
void postLoad() override
Definition fsCEntity.cpp:129
fsCEntity * grandparentGet() const
Definition fsCEntity.cpp:14
fsBool tagExists(fsStr const &pTag) const
Definition fsCEntity.cpp:79
TInterface * firstComponentOfFamilyType() const
Definition fsCEntity.h:141
fsISpriteComponent * spriteComponentGet() const
Definition fsCEntity.cpp:103
fsCComponentFactory * componentFactoryGet()
Definition fsCEntity.cpp:54
TComponent * componentGet() const
Definition fsCEntity.h:209
void initDo() override
Definition fsCEntity.cpp:108
friend class fsCEntityFactory
Definition fsCEntity.h:40
fsCEntity(const fsStr &pName)
Definition fsCEntity.cpp:335
std::vector< fsCEntity * > childEntitiesWithComponentsOfFamilyType()
Definition fsCEntity.h:185
void postLinkDo() override
Definition fsCEntity.cpp:43
void tagAdd(fsStr const &pTag)
Definition fsCEntity.cpp:69
std::vector< fsCEntity * > childEntitiesTaggedWithGet(std::vector< fsStr > pTags)
Definition fsCEntity.cpp:275
void deserialiseDo() override
Definition fsCEntity.cpp:122
std::vector< TInterface * > componentsOfFamilyTypeGet() const
Definition fsCEntity.h:162
void componentDestroy(const fsCUniqueId &pTypeId)
Definition fsCEntity.cpp:257
void updateDo(fsS32 pDeltaMs) override
Definition fsCEntity.cpp:163
virtual ~fsCEntity()
Definition fsCEntity.cpp:342
void updatedMatrixProcess() override
Definition fsCEntity.cpp:189
void serialiseDo() override
Definition fsCEntity.cpp:115
friend class fsIGuiComponent
Definition fsCEntity.h:42
Definition fsCTaggableComponent.h:19
Definition fsCUniqueId.h:21
Definition fsCVariableTable.h:27
Definition fsIComponent.h:26
fsINode(const fsStr &pName)
Definition fsINode.cpp:657
friend class fsCEntity
Definition fsINode.h:31
std::vector< fsINode * > mChildren
Definition fsINode.h:117
Definition fsIRenderableComponent.h:21
Definition fsIScene.h:18
Definition fsISpriteComponent.h:23
Definition fsITextSpriteComponent.h:22
Definition fsStr.h:23
bool fsBool
Definition fsBaseTypes.h:26
int fsS32
Definition fsBaseTypes.h:20