NAP
entity.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "componentptr.h"
9 #include "component.h"
10 #include "instanceproperty.h"
11 
12 // External Includes
13 #include <utility/uniqueptrvectoriterator.h>
14 #include <nap/resource.h>
15 #include <nap/resourceptr.h>
16 
17 namespace nap
18 {
19  // Forward Declares
20  class Core;
21  class Component;
22  class Entity;
23  class EntityInstance;
24  class Scene;
25 
26  // Using
27  using EntityList = std::vector<EntityInstance*>;
28 
34  class NAPAPI EntityInstance : public rtti::Object
35  {
36  RTTI_ENABLE(rtti::Object)
37 
38  public:
39  using rtti::Object::init;
40 
41  using ComponentList = std::vector<std::unique_ptr<ComponentInstance>>;
42  using ChildList = std::vector<EntityInstance*>;
45 
51  EntityInstance(Core& core, const Entity* entity);
52 
59  bool init(Scene& scene, EntityCreationParameters& entityCreationParams, utility::ErrorState& errorState);
60 
64  virtual void onDestroy() override {}
65 
69  void update(double deltaTime);
70 
75  void addComponent(std::unique_ptr<ComponentInstance> component);
76 
82  ComponentInstance* findComponent(const std::string& type) const;
83 
89  ComponentInstance* findComponentByID(const std::string& identifier) const;
90 
96  template<class T >
97  T* findComponentByID(const std::string& identifier) const;
98 
104  ComponentInstance* findComponent(const rtti::TypeInfo& type) const;
105 
109  template<class T>
110  T* findComponent() const;
111 
116  bool hasComponent(const rtti::TypeInfo& type) const;
117 
121  template<class T>
122  bool hasComponent() const;
123 
128  ComponentInstance& getComponent(const rtti::TypeInfo& type) const;
129 
135  template<class T>
136  T& getComponent() const;
137 
143  void getComponentsOfType(const rtti::TypeInfo& type, std::vector<ComponentInstance*>& components) const;
144 
149  template<class T>
150  void getComponentsOfType(std::vector<T*>& outComponents) const;
151 
156  template<class T>
157  void getComponentsOfTypeRecursive(std::vector<T*>& outComponents);
158 
163  bool hasComponentsOfType(const rtti::TypeInfo& type) const;
164 
168  template<class T>
169  bool hasComponentsOfType() const;
170 
175  void addChild(EntityInstance& child);
176 
180  void clearChildren();
181 
186  void removeChild(const EntityInstance& entityInstance);
187 
191  const ChildList& getChildren() const;
192 
196  EntityInstance* getParent() const;
197 
203  const Entity* getEntity() const;
204 
208  Core* getCore() const;
209 
214 
219 
224  EntityInstance& operator[](std::size_t index) { assert(index < mChildren.size()); return *(mChildren[index]); }
225 
229  const EntityInstance& operator[](std::size_t index) const { assert(index < mChildren.size()); return *(mChildren[index]); }
230 
231  private:
232  Core* mCore = nullptr;
233  const Entity* mResource = nullptr; // Resource of this entity
234  EntityInstance* mParent = nullptr; // Parent of this entity
235  ComponentList mComponents; // The components of this entity
236  ChildList mChildren; // The children of this entity
237  };
238 
246  {
247  public:
248  SpawnedEntityInstance() = default;
249 
250  rtti::ObjectPtr<EntityInstance>& get() { return mEntityInstance; }
251 
252  const rtti::ObjectPtr<EntityInstance>& get() const { return mEntityInstance; }
253 
254  const EntityInstance& operator*() const
255  {
256  assert(mEntityInstance != nullptr);
257  return *mEntityInstance;
258  }
259 
261  {
262  assert(mEntityInstance != nullptr);
263  return *mEntityInstance;
264  }
265 
267  {
268  assert(mEntityInstance != nullptr);
269  return mEntityInstance.get();
270  }
271 
273  {
274  assert(mEntityInstance != nullptr);
275  return mEntityInstance.get();
276  }
277 
278  bool operator==(const SpawnedEntityInstance& other) const
279  {
280  return mEntityInstance == other.mEntityInstance;
281  }
282 
283  bool operator==(const EntityInstance* ptr) const
284  {
285  return mEntityInstance == ptr;
286  }
287 
288  bool operator!=(const SpawnedEntityInstance& other) const
289  {
290  return mEntityInstance != other.mEntityInstance;
291  }
292 
293  bool operator!=(const EntityInstance* ptr) const
294  {
295  return mEntityInstance != ptr;
296  }
297 
298  bool operator<(const SpawnedEntityInstance& other) const
299  {
300  return mEntityInstance < other.mEntityInstance;
301  }
302 
303  bool operator>(const SpawnedEntityInstance& other) const
304  {
305  return mEntityInstance > other.mEntityInstance;
306  }
307 
308  bool operator<=(const SpawnedEntityInstance& other) const
309  {
310  return mEntityInstance <= other.mEntityInstance;
311  }
312 
313  bool operator>=(const SpawnedEntityInstance& other) const
314  {
315  return mEntityInstance >= other.mEntityInstance;
316  }
317 
318  private:
319  friend class Scene;
320 
321  SpawnedEntityInstance(EntityInstance* entityInstance) :
322  mEntityInstance(entityInstance)
323  {
324  }
325 
326  private:
327  rtti::ObjectPtr<EntityInstance> mEntityInstance;
328  };
329 
331  // Entity Resource
333 
339  class NAPAPI Entity : public Resource
340  {
341  RTTI_ENABLE(Resource)
342  public:
343  using ComponentList = std::vector<rtti::ObjectPtr<Component>>;
344  using EntityList = std::vector<rtti::ObjectPtr<Entity>>;
345 
352  rtti::ObjectPtr<Component> findComponent(const rtti::TypeInfo& type) const;
353 
359  bool hasComponent(const rtti::TypeInfo& type) const;
360 
364  template<class T>
365  bool hasComponent() const;
366 
370  const ComponentList& getComponents() const { return mComponents; }
371 
375  static constexpr const char* componentsPropertyName() { return "Components"; }
376 
380  static constexpr const char* childrenPropertyName() { return "Children"; }
381 
382  public:
383  ComponentList mComponents; // The components of this entity
384  EntityList mChildren; // The children of this entity
385  };
386 
388 
389  template<class T>
390  void EntityInstance::getComponentsOfType(std::vector<T*>& components) const
391  {
392  const rtti::TypeInfo type = rtti::TypeInfo::get<T>();
393  for (auto& component : mComponents)
394  if (rtti::isTypeMatch(component->get_type(), type, rtti::ETypeCheck::IS_DERIVED_FROM))
395  components.emplace_back(rtti_cast<T>(component.get()));
396  }
397 
398 
399  template<typename T>
400  void getComponentsOfTypeRecursive(nap::EntityInstance& entity, std::vector<T*>& outComponents)
401  {
402  entity.getComponentsOfType<T>(outComponents);
403  for (auto& child : entity.getChildren())
404  getComponentsOfTypeRecursive<T>(*child, outComponents);
405  }
406 
407 
408  template<class T>
409  void EntityInstance::getComponentsOfTypeRecursive(std::vector<T*>& outComponents)
410  {
411  nap::getComponentsOfTypeRecursive<T>(*this, outComponents);
412  }
413 
414 
415  template<class T>
417  {
418  return hasComponentsOfType(rtti::TypeInfo::get<T>());
419  }
420 
421  template<class T >
422  T* EntityInstance::findComponentByID(const std::string& identifier) const
423  {
424  T* element = rtti_cast<T>(findComponentByID(identifier));
425  if (element == nullptr)
426  return nullptr;
427  return rtti::isTypeMatch(element->get_type(), RTTI_OF(T), rtti::ETypeCheck::IS_DERIVED_FROM) ? element : nullptr;
428  }
429 
430  template<class T>
432  {
433  return rtti_cast<T>(findComponent(rtti::TypeInfo::get<T>()));
434  }
435 
436  template<class T>
438  {
439  return hasComponent(rtti::TypeInfo::get<T>());
440  }
441 
442  template<class T>
444  {
445  return *rtti_cast<T>(&getComponent(rtti::TypeInfo::get<T>()));
446  }
447 
449 
450  template<class T>
451  bool Entity::hasComponent() const
452  {
453  return hasComponent(rtti::TypeInfo::get<T>());
454  }
455 }
nap::Scene
Definition: scene.h:39
nap::SpawnedEntityInstance::operator<=
bool operator<=(const SpawnedEntityInstance &other) const
Definition: entity.h:308
nap::rtti::Object::init
virtual bool init(utility::ErrorState &errorState)
Definition: object.h:46
nap::utility::UniquePtrConstVectorWrapper
Definition: uniqueptrvectoriterator.h:70
nap::SpawnedEntityInstance::operator>=
bool operator>=(const SpawnedEntityInstance &other) const
Definition: entity.h:313
nap::SpawnedEntityInstance::operator>
bool operator>(const SpawnedEntityInstance &other) const
Definition: entity.h:303
nap::Entity::componentsPropertyName
static constexpr const char * componentsPropertyName()
Definition: entity.h:375
nap::EntityInstance::findComponent
T * findComponent() const
Definition: entity.h:431
nap::EntityInstance::getComponents
ComponentConstIterator getComponents() const
Definition: entity.h:218
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::rtti::Object
Definition: object.h:30
nap::SpawnedEntityInstance::operator*
const EntityInstance & operator*() const
Definition: entity.h:254
nap::Entity::EntityList
std::vector< rtti::ObjectPtr< Entity > > EntityList
Definition: entity.h:344
nap::getComponentsOfTypeRecursive
void getComponentsOfTypeRecursive(nap::EntityInstance &entity, std::vector< T * > &outComponents)
Definition: entity.h:400
nap::SpawnedEntityInstance
Definition: entity.h:245
nap::utility::ErrorState
Definition: errorstate.h:19
nap::EntityInstance::hasComponentsOfType
bool hasComponentsOfType() const
Definition: entity.h:416
nap::SpawnedEntityInstance::operator==
bool operator==(const SpawnedEntityInstance &other) const
Definition: entity.h:278
nap::Entity::childrenPropertyName
static constexpr const char * childrenPropertyName()
Definition: entity.h:380
nap::SpawnedEntityInstance::operator->
EntityInstance * operator->()
Definition: entity.h:272
nap::rtti::isTypeMatch
bool isTypeMatch(const rtti::TypeInfo &typeA, const rtti::TypeInfo &typeB, ETypeCheck typeCheck)
Definition: typeinfo.h:245
nap::Entity::ComponentList
std::vector< rtti::ObjectPtr< Component > > ComponentList
Definition: entity.h:343
nap::EntityInstance::hasComponent
bool hasComponent() const
Definition: entity.h:437
nap::EntityInstance::getComponentsOfTypeRecursive
void getComponentsOfTypeRecursive(std::vector< T * > &outComponents)
Definition: entity.h:409
nap::SpawnedEntityInstance::operator!=
bool operator!=(const SpawnedEntityInstance &other) const
Definition: entity.h:288
nap::EntityInstance::getComponentsOfType
void getComponentsOfType(const rtti::TypeInfo &type, std::vector< ComponentInstance * > &components) const
nap::Entity::hasComponent
bool hasComponent() const
Definition: entity.h:451
nap::EntityList
std::vector< EntityInstance * > EntityList
Definition: entity.h:27
nap::EntityCreationParameters
Definition: entitycreationparameters.h:46
nap::Core
Definition: core.h:82
nap::EntityInstance::operator[]
const EntityInstance & operator[](std::size_t index) const
Definition: entity.h:229
nap::EntityInstance::onDestroy
virtual void onDestroy() override
Definition: entity.h:64
nap::SpawnedEntityInstance::operator*
EntityInstance & operator*()
Definition: entity.h:260
nap::EntityInstance
Definition: entity.h:34
nap::EntityInstance::findComponentByID
ComponentInstance * findComponentByID(const std::string &identifier) const
nap::SpawnedEntityInstance::operator->
EntityInstance * operator->() const
Definition: entity.h:266
nap::ComponentInstance
Definition: component.h:43
nap::EntityInstance::getChildren
const ChildList & getChildren() const
nap
Definition: templateapp.h:17
nap::SpawnedEntityInstance::operator<
bool operator<(const SpawnedEntityInstance &other) const
Definition: entity.h:298
nap::EntityInstance::ChildList
std::vector< EntityInstance * > ChildList
Definition: entity.h:42
nap::EntityInstance::ComponentList
std::vector< std::unique_ptr< ComponentInstance > > ComponentList
Definition: entity.h:41
nap::Entity::getComponents
const ComponentList & getComponents() const
Definition: entity.h:370
nap::Entity::mChildren
EntityList mChildren
Definition: entity.h:384
nap::Resource
Definition: resource.h:19
nap::EntityInstance::operator[]
EntityInstance & operator[](std::size_t index)
Definition: entity.h:224
nap::rtti::ETypeCheck::IS_DERIVED_FROM
@ IS_DERIVED_FROM
The type is derived from the specified type.
nap::SpawnedEntityInstance::get
rtti::ObjectPtr< EntityInstance > & get()
Definition: entity.h:250
nap::SpawnedEntityInstance::get
const rtti::ObjectPtr< EntityInstance > & get() const
Definition: entity.h:252
nap::Entity::mComponents
ComponentList mComponents
Definition: entity.h:383
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::SpawnedEntityInstance::operator!=
bool operator!=(const EntityInstance *ptr) const
Definition: entity.h:293
nap::EntityInstance::getComponent
T & getComponent() const
Definition: entity.h:443
nap::SpawnedEntityInstance::operator==
bool operator==(const EntityInstance *ptr) const
Definition: entity.h:283
nap::Entity
Definition: entity.h:339
nap::EntityInstance::getComponents
ComponentIterator getComponents()
Definition: entity.h:213
nap::utility::UniquePtrVectorWrapper
Definition: uniqueptrvectoriterator.h:48