NAP
component.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 #include <rtti/object.h>
8 #include <rtti/objectptr.h>
9 #include <utility/dllexport.h>
10 #include <nap/resource.h>
11 
12 #include <cassert>
13 
14 namespace nap
15 {
16  // Forward Declares
17  namespace utility
18  {
19  class ErrorState;
20  }
21 
22  class Entity;
23  class EntityInstance;
24  class Component;
25  struct EntityCreationParameters;
26 
27  template<class TargetComponentType>
29 
30  template<class ComponentType>
31  class ComponentPtr;
32 
33  class EntityPtr;
34  class EntityInstancePtr;
35 
43  class NAPAPI ComponentInstance : public rtti::Object
44  {
45  RTTI_ENABLE(rtti::Object)
46 
47  public:
48  using Object::init;
49 
56  mEntityInstance(&entity),
57  mResource(&resource)
58  { }
59 
64  virtual void update(double deltaTime) { }
65 
69  nap::EntityInstance* getEntityInstance() const { return mEntityInstance; }
70 
74  nap::Component* getComponent() const { return mResource; }
75 
80  template<typename T>
81  T* getComponent() const;
82 
88  virtual bool init(utility::ErrorState& errorState);
89 
90  private:
91  template<typename TargetComponentType, typename SourceComponentType>
92  friend std::vector<ComponentInstancePtr<TargetComponentType>> initComponentInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<ComponentPtr<TargetComponentType>>(SourceComponentType::*componentMemberPointer));
93 
94  template<typename SourceComponentType>
95  friend std::vector<EntityInstancePtr> initEntityInstancePtr(ComponentInstance* sourceComponentInstance, std::vector<EntityPtr>(SourceComponentType::*entityMemberPointer));
96 
97  template<class TargetComponentType> friend class ComponentInstancePtr;
98  friend class EntityInstancePtr;
99  friend class SceneInstantiation;
100 
108  void addToComponentLinkMap(Component* targetResource, const std::string& instancePath, ComponentInstance** targetInstancePtr);
109 
117  void addToEntityLinkMap(Entity* targetResource, const std::string& instancePath, EntityInstance** targetInstancePtr);
118 
119  private:
123  template<class INSTANCETYPE>
124  struct TargetInstanceLink
125  {
126  INSTANCETYPE** mTargetPtr;
127  std::string mInstancePath;
128  };
129 
130  using TargetComponentLink = TargetInstanceLink<ComponentInstance>;
131  using TargetEntityLink = TargetInstanceLink<EntityInstance>;
132 
133  using ComponentLinkMap = std::unordered_map<Component*, std::vector<TargetComponentLink>>;
134  using EntityLinkMap = std::unordered_map<Entity*, std::vector<TargetEntityLink>>;
135 
136  ComponentLinkMap mComponentLinkMap; // Map containing component instance link information that is used to resolve pointers
137  EntityLinkMap mEntityLinkMap; // Map containing entity instance link information that is used to resolve pointers
138  EntityInstance* mEntityInstance; // The entity this component belongs to
139  Component* mResource; // The resource this instance was created from
140  };
141 
143  // Component
145 
151  class NAPAPI Component : public Resource
152  {
153  RTTI_ENABLE(Resource)
154 
155  public:
167  virtual void getDependentComponents(std::vector<rtti::TypeInfo>& components) const { }
168 
172  virtual const rtti::TypeInfo getInstanceType() const = 0;
173 
174  private:
178  const std::string& getOriginalID() const;
179 
180  private:
181  friend class Scene;
182  friend class SceneInstantiation;
183  Component* mOriginalComponent = nullptr; // If this Component was cloned from another component (for instance properties), this property holds the Component it was cloned from
184  };
185 
187 
188  template<typename T>
190  {
191  T* comp = rtti_cast<T>(mResource);
192  assert(comp != nullptr);
193  return comp;
194  }
195 
196 
197 #define DECLARE_COMPONENT(ComponentType, ComponentInstanceType) \
198  public: \
199  using InstanceType = ComponentInstanceType; \
200  virtual const rtti::TypeInfo getInstanceType() const override \
201  { \
202  return nap::rtti::TypeInfo::get<InstanceType>(); \
203  } \
204  private:
205 
206 }
nap::EntityInstancePtr
Definition: entityptr.h:107
nap::Scene
Definition: scene.h:39
nap::ComponentInstance::getEntityInstance
nap::EntityInstance * getEntityInstance() const
Definition: component.h:69
nap::rtti::Object::init
virtual bool init(utility::ErrorState &errorState)
Definition: object.h:46
nap::EntityPtr
Definition: entityptr.h:15
nap::initComponentInstancePtr
ComponentInstancePtrInitProxy< TargetComponentType, SourceComponentType > initComponentInstancePtr(ComponentInstance *sourceComponentInstance, ComponentPtr< TargetComponentType >(SourceComponentType::*componentMemberPointer))
Definition: componentptr.h:364
nap::rtti::Object
Definition: object.h:30
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ComponentInstance::update
virtual void update(double deltaTime)
Definition: component.h:64
nap::ComponentInstance::getComponent
nap::Component * getComponent() const
Definition: component.h:74
nap::ComponentInstance::ComponentInstance
ComponentInstance(EntityInstance &entity, Component &resource)
Definition: component.h:55
nap::EntityInstance
Definition: entity.h:34
nap::ComponentInstancePtr
Definition: component.h:28
nap::ComponentInstance
Definition: component.h:43
nap::Component::getDependentComponents
virtual void getDependentComponents(std::vector< rtti::TypeInfo > &components) const
Definition: component.h:167
nap::Component
Definition: component.h:151
nap
Definition: templateapp.h:17
nap::Resource
Definition: resource.h:19
nap::rtti::TypeInfo
rttr::type TypeInfo
Definition: typeinfo.h:140
nap::ComponentPtr
Definition: component.h:31
nap::Entity
Definition: entity.h:339
nap::initEntityInstancePtr
EntityInstancePtrInitProxy< SourceComponentType > initEntityInstancePtr(ComponentInstance *sourceComponentInstance, EntityPtr(SourceComponentType::*entityMemberPointer))
Definition: entityptr.h:211