NAP
scene.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 "instanceproperty.h"
9 #include "entitycreationparameters.h"
10 
11 // External Includes
12 #include <rtti/object.h>
13 #include <utility/uniqueptrmapiterator.h>
14 #include <rtti/factory.h>
15 #include <nap/resource.h>
16 
17 namespace nap
18 {
19  // Forward Declares
20  class Entity;
21  class SpawnedEntityInstance;
22  class Core;
23 
27  class NAPAPI RootEntity final
28  {
29  RTTI_ENABLE()
30  public:
32  std::vector<ComponentInstanceProperties> mInstanceProperties;
33  };
34 
39  class NAPAPI Scene : public Resource
40  {
41  RTTI_ENABLE(Resource)
42  public:
43  using EntityByIDMap = std::unordered_map<std::string, std::unique_ptr<EntityInstance>>;
45  using RootEntityList = std::vector<RootEntity>;
46  using InstanceByIDMap = std::unordered_map<std::string, rtti::Object*>;
47  using SortedComponentInstanceList = std::vector<ComponentInstance*>;
48  using SpawnedComponentInstanceMap = std::unordered_map<EntityInstance*, SortedComponentInstanceList>;
49 
50  Scene(Core& core);
51  virtual ~Scene() override;
52 
58  virtual bool init(utility::ErrorState& errorState) override;
59 
63  virtual void onDestroy() override;
64 
69  void update(double deltaTime);
70 
78  SpawnedEntityInstance spawn(const Entity& entity, utility::ErrorState& errorState);
79 
88  SpawnedEntityInstance spawn(const Entity& entity, const std::vector<ComponentInstanceProperties>& instanceProperties, utility::ErrorState& errorState);
89 
95  void destroy(SpawnedEntityInstance& entity);
96 
102  void updateTransforms(double deltaTime);
103 
107  EntityIterator getEntities() { return EntityIterator(mEntityInstancesByID); }
108 
124  const rtti::ObjectPtr<EntityInstance> findEntity(const std::string& inID) const;
125 
129  const EntityInstance& getRootEntity() const { return *mRootEntityInstance;}
130 
134  EntityInstance& getRootEntity() { return *mRootEntityInstance;}
135 
139  RootEntityList getEntityResources() { return mEntities; }
140 
144  RootEntityList& getEntityResourcesRef() { return mEntities; }
145 
146  private:
147  EntityInstance* createEntityInstance(const Entity& entityResource, EntityCreationParameters& entityCreationParams, utility::ErrorState& errorState);
148 
152  EntityInstance* createChildEntityInstance(const Entity& entity, int childIndex, EntityCreationParameters& entityCreationParams, utility::ErrorState& errorState);
153 
157  bool spawnInternal(const RootEntityList& rootEntities, const std::vector<rtti::Object*>& allObjects, bool clearChildren, std::vector<EntityInstance*>& spawnedRootEntityInstances, SortedComponentInstanceList& sortedComponentInstances, utility::ErrorState& errorState);
158 
159  public:
161 
162  private:
163  friend class EntityInstance;
164 
165  Core* mCore;
166  std::unique_ptr<EntityInstance> mRootEntityInstance;
167  std::unique_ptr<Entity> mRootEntityResource;
168  EntityByIDMap mEntityInstancesByID;
169  InstanceByIDMap mInstancesByID;
170  ClonedComponentResourceList mAllClonedComponents;
171  SortedComponentInstanceList mLoadedComponentInstances;
172  SpawnedComponentInstanceMap mSpawnedComponentInstanceMap;
173  };
174 
176 }
nap::Scene
Definition: scene.h:39
nap::Scene::SpawnedComponentInstanceMap
std::unordered_map< EntityInstance *, SortedComponentInstanceList > SpawnedComponentInstanceMap
Definition: scene.h:48
nap::Scene::getEntityResources
RootEntityList getEntityResources()
Definition: scene.h:139
nap::RootEntity::mEntity
rtti::ObjectPtr< Entity > mEntity
Root entity to spawn.
Definition: scene.h:31
nap::Scene::getRootEntity
EntityInstance & getRootEntity()
Definition: scene.h:134
nap::rtti::ObjectPtr
Definition: objectptr.h:154
nap::Scene::SortedComponentInstanceList
std::vector< ComponentInstance * > SortedComponentInstanceList
Definition: scene.h:47
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::SpawnedEntityInstance
Definition: entity.h:245
nap::utility::ErrorState
Definition: errorstate.h:19
nap::ClonedComponentResourceList
std::vector< ClonedComponentResource > ClonedComponentResourceList
Definition: entitycreationparameters.h:41
nap::Scene::EntityByIDMap
std::unordered_map< std::string, std::unique_ptr< EntityInstance > > EntityByIDMap
Definition: scene.h:43
nap::Scene::RootEntityList
std::vector< RootEntity > RootEntityList
Definition: scene.h:45
nap::Scene::InstanceByIDMap
std::unordered_map< std::string, rtti::Object * > InstanceByIDMap
Definition: scene.h:46
nap::EntityCreationParameters
Definition: entitycreationparameters.h:46
nap::Core
Definition: core.h:82
nap::utility::UniquePtrMapWrapper
Definition: uniqueptrmapiterator.h:47
nap::RootEntity
Definition: scene.h:27
nap::EntityInstance
Definition: entity.h:34
nap::Scene::getRootEntity
const EntityInstance & getRootEntity() const
Definition: scene.h:129
nap::Scene::getEntities
EntityIterator getEntities()
Definition: scene.h:107
nap::Scene::mEntities
RootEntityList mEntities
List of root entities owned by the Scene.
Definition: scene.h:160
nap
Definition: templateapp.h:17
nap::Scene::getEntityResourcesRef
RootEntityList & getEntityResourcesRef()
Definition: scene.h:144
nap::Resource
Definition: resource.h:19
nap::RootEntity::mInstanceProperties
std::vector< ComponentInstanceProperties > mInstanceProperties
The instance properties for this entity (and all of its children)
Definition: scene.h:32
nap::Entity
Definition: entity.h:339