1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/********************************************************************
*
*	File:		fsCEntity.h
*
*	Purpose:	Core container for components.
*
*				Interface components via fsCUniqueId which is also
*				handily wrapped up using templates to make the client
*				code cleaner.
*
*				Based on fsCUniqueId so you can't remove the interface
*				to it!
*
*********************************************************************/

#ifndef fsCEntityhdr
#define fsCEntityhdr

#include <typeinfo>
#include <map>
#include <vector>
#include <algorithm>
#include <memory>


#include "../fsINode.h"

class fsIComponent;
class fsCUniqueId;
class fsIRenderableComponent;
class fsISpriteComponent;
class fsIScene;
class fsITextSpriteComponent;
class fsCComponentFactory;
class fsCEntityFactory;
class fsCTaggableComponent;

class fsCEntity : public fsINode
{
	friend class fsCEntityFactory;
	friend class gfCTestLoadGuis;
	friend class fsIGuiComponent;

public:
  fsCEntity *grandparentGet() const;

	void listenersRegister();
	void listenersDeRegister();

	static void componentFactorySet(std::shared_ptr<fsCComponentFactory> pComponentFactory);
        fsCComponentFactory *componentFactoryGet();
	static void entityFactorySet(std::shared_ptr<fsCEntityFactory> pEntityFactory);
        fsCEntityFactory *entityFactoryGet();

        fsITextSpriteComponent *textComponentGet() const;
        fsITextSpriteComponent *textComponentAdd();

        fsISpriteComponent *spriteComponentGet() const;
        fsISpriteComponent *spriteComponentAdd();

	fsBool tagExists(fsStr const& pTag) const;
	void tagAdd(fsStr const& pTag);

	template <typename TComponent>
	TComponent* componentAdd();<--- The member function 'fsCEntity::componentAdd < fsCBarGuiComponent >' can be static.<--- The member function 'fsCEntity::componentAdd < fsCTaggableComponent >' can be static.<--- The member function 'fsCEntity::componentAdd < fsCTextSpriteComponent >' can be static.<--- The member function 'fsCEntity::componentAdd < fsCSpriteComponent >' can be static.
        fsIComponent *componentAdd(const fsCUniqueId& pTypeId);

	template <typename TComponent>
	TComponent* componentGet() const;<--- The member function 'fsCEntity::componentGet < fsCTaggableComponent >' can be static.
        fsIComponent *componentGet(const fsCUniqueId& pTypeId) const;

	void componentDestroy(const fsCUniqueId& pTypeId);

	template <typename TInterface>
	TInterface* firstComponentOfFamilyType() const;

	template <typename TLamda>
	void forMyChildren(TLamda&& pLamda);
	template <typename TLamda>
	void forAllChildren(TLamda&& pLamda);

	fsCEntity* childFind(const fsStr& pName, fsBool pCaseless = false);

	template <typename TInterface>
	std::vector<TInterface*> componentsOfFamilyTypeGet() const;
	template <typename TInterface>
	std::vector<fsCEntity*> childEntitiesWithComponentsOfFamilyType();
	std::vector<fsCEntity*> childEntitiesWithComponentsOfTypeGet(
		const std::vector<const std::type_info*>& pPossibleComponentTypes);
	std::vector<fsCEntity*> childEntitiesTaggedWithGet(std::vector<fsStr> pTags);

	template <typename TInterface>
        fsCEntity *firstChildEntityWithComponentsOfFamilyType();

protected:

	fsCEntity(const fsStr& pName);
	virtual ~fsCEntity();<--- Destructor in derived class

	void initialiseFromVariableTableDo(const fsCVariableTable& pVars) override;

	void updateDo(fsS32 pDeltaMs) override;
	void renderDo() override;
	void updatedMatrixProcess() override;

	void initDo() override;
	void resetDo() override;
	void serialiseDo() override;
	void deserialiseDo() override;
	void postLoad() override;
	void postLinkDo() override;

private:


	void componentUpdateMethodExecute(void (fsIComponent::*pMethod)());

	void deferredComponentsAdd();
	void deferredComponentsRemove();

	void componentUnregisterAndDestroy(const fsCUniqueId& pTypeId);

	void childEntitiesWithComponentsOfTypeGetDo(std::vector<fsCEntity*>& pVectorToFill,
	                                            const std::vector<const std::type_info*>& pPossibleComponentTypes);
	void childEntitiesTaggedWithGetDo(std::vector<fsStr> pTags, std::vector<fsCEntity*>& pEnts);

	std::map<fsU32, fsIComponent*> mComponentsMap;
	std::vector<fsIComponent*> mComponentsVector;

	std::vector<std::pair<fsIComponent*, fsU32>> mComponentsToAdd;
	std::vector<fsCUniqueId> mComponentsToDestroy;

	fsBool mInComponentUpdate;

	static std::shared_ptr<fsCEntityFactory> mEntityFactory;
	static std::shared_ptr<fsCComponentFactory> mComponentFactory;
};


template <typename TInterface>
TInterface* fsCEntity::firstComponentOfFamilyType() const
{
	for (auto iter = mComponentsMap.begin(); iter != mComponentsMap.end(); ++iter)
	{
		if (auto* result = dynamic_cast<TInterface*>(iter->second))
		{<--- Consider using std::any_of algorithm instead of a raw loop.
			return result;
		}
	}

	for (auto iter = mComponentsToAdd.begin(); iter != mComponentsToAdd.end(); ++iter)
	{
		if (auto* result = dynamic_cast<TInterface*>(iter->first))
		{<--- Consider using std::any_of algorithm instead of a raw loop.
			return result;
		}
	}
	return nullptr;
}

template <typename TInterface>
std::vector<TInterface*> fsCEntity::componentsOfFamilyTypeGet() const
{
	std::vector<TInterface*> comps;
	for (auto iter = mComponentsMap.begin(); iter != mComponentsMap.end(); ++iter)
	{
		if (auto* result = dynamic_cast<TInterface*>(iter->second))
		{
			comps.push_back(result);
		}
	}

	for (auto compIt = mComponentsToAdd.begin(); compIt != mComponentsToAdd.end(); ++compIt)
	{
		if (auto* result = dynamic_cast<TInterface*>(compIt->first))
		{
			comps.push_back(result);
		}
	}
	return comps;
}


template <typename TInterface>
std::vector<fsCEntity*> fsCEntity::childEntitiesWithComponentsOfFamilyType()
{
	std::vector<fsCEntity*> ents;

	for (fsINode* child : mChildren)
	{
		if (fsCEntity* childEnt = dynamic_cast<fsCEntity*>(child))
		{
			auto ret = childEnt->childEntitiesWithComponentsOfFamilyType<TInterface>();
			ents.insert(ents.end(), ret.begin(), ret.end());
		}
	}

	for (auto iter = mComponentsMap.begin(); iter != mComponentsMap.end(); ++iter)
	{
		if (dynamic_cast<TInterface*>(iter->second))
		{
			ents.push_back(this);
		}
	}
	return ents;
}

template <typename TComponent>
TComponent* fsCEntity::componentGet() const<--- The member function 'fsCEntity::componentGet < fsCTaggableComponent >' can be static.
{
	return dynamic_cast<TComponent* const>(componentGet(TComponent::mTypeId));
}

template <typename TComponent>
TComponent* fsCEntity::componentAdd()<--- The member function 'fsCEntity::componentAdd < fsCBarGuiComponent >' can be static.<--- The member function 'fsCEntity::componentAdd < fsCTaggableComponent >' can be static.<--- The member function 'fsCEntity::componentAdd < fsCTextSpriteComponent >' can be static.<--- The member function 'fsCEntity::componentAdd < fsCSpriteComponent >' can be static.
{
	return dynamic_cast<TComponent* const>(componentAdd(TComponent::mTypeId));
}

template <typename TInterface>
fsCEntity *fsCEntity::firstChildEntityWithComponentsOfFamilyType()
{
	for (fsINode* child : mChildren)
	{
		if (fsCEntity* childEnt = dynamic_cast<fsCEntity*>(child))
		{
			if (auto matchEnt = childEnt->firstChildEntityWithComponentsOfFamilyType<TInterface>())
			{
				return matchEnt;
			}
		}
	}

	for (auto component : mComponentsMap)
	{
		if (dynamic_cast<TInterface*>(component.second))
		{<--- Consider using std::any_of algorithm instead of a raw loop.
			return this;
		}
	}
	return nullptr;
}

template <typename TLamda>
void fsCEntity::forAllChildren(TLamda&& pLamda)
{
	std::for_each(mChildren.begin(), mChildren.end(),
	              [&pLamda](fsINode* node)
	              {
		              if (fsCEntity* const ent = dynamic_cast<fsCEntity*>(node))
		              {
			              ent->forAllChildren(pLamda);
		              }
	              });
	pLamda(this);
}

template <typename TLamda>
void fsCEntity::forMyChildren(TLamda&& pLamda)
{
	std::for_each(mChildren.begin(), mChildren.end(), pLamda);
}

#endif