R-Type
R-Type project
Scene.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** gameEngine2
4 ** File description:
5 ** Scene
6 */
7 
8 #ifndef SCENE_HPP_
9 #define SCENE_HPP_
10 
11 #include "Engine.hpp"
12 #include "GameObject.hpp"
13 
14 #include "KapEngineSettings.hpp"
15 
16 #include <functional>
17 
18 namespace KapEngine {
19  class KEngine;
20  class GameObject;
21  class Component;
22  class Entity;
23 
24  namespace SceneManagement {
25  class SceneManager;
26  }
27 }
28 
29 #include <string>
30 #include <memory>
31 #include <vector>
32 
33 namespace KapEngine {
34 
35  namespace SceneManagement {
36 
37  class Scene {
38  public:
39  Scene(SceneManager &manager, std::string const& sceneName);
40  ~Scene();
41 
42  void addGameObject(std::shared_ptr<GameObject> go);
43  void destroyGameObject(std::shared_ptr<GameObject> const go);
44  void destroyGameObject(GameObject const& go);
45  void destroyGameObject(std::size_t id);
46 
47  bool setId(std::size_t id) {
48  PROFILER_FUNC_START();
49  if (_id != 0) {
50  PROFILER_FUNC_END();
51  return false;
52  }
53  _id = id;
54  PROFILER_FUNC_END();
55  return true;
56  }
57 
58  std::size_t getId() const {
59  PROFILER_FUNC_START();
60  PROFILER_FUNC_END();
61  return _id;
62  }
63 
64  std::string getName() const {
65  PROFILER_FUNC_START();
66  PROFILER_FUNC_END();
67  return _name;
68  }
69 
70  bool operator==(Scene const& scene) {
71  PROFILER_FUNC_START();
72  if (_id == scene.getId()) {
73  PROFILER_FUNC_END();
74  return true;
75  }
76  PROFILER_FUNC_END();
77  return false;
78  }
79 
80  bool operator==(std::shared_ptr<Scene> scene) {
81  PROFILER_FUNC_START();
82  PROFILER_FUNC_END();
83  return *this == *scene;
84  }
85 
86  bool operator!=(Scene const& scene) {
87  PROFILER_FUNC_START();
88  if (_id != scene.getId()) {
89  PROFILER_FUNC_END();
90  return true;
91  }
92  PROFILER_FUNC_END();
93  return false;
94  }
95 
96  bool operator!=(std::shared_ptr<Scene> scene) {
97  PROFILER_FUNC_START();
98  PROFILER_FUNC_END();
99  return *this != *scene;
100  }
101 
102  Component &getActiveCamera() const;
103 
110  std::shared_ptr<GameObject> &getGameObject(std::size_t id);
118  std::shared_ptr<GameObject> &getObject(std::size_t id) {
119  PROFILER_FUNC_START();
120  PROFILER_FUNC_END();
121  return getGameObject(id);
122  }
123 
124  std::shared_ptr<GameObject> getGameObjectConst(std::size_t id) const;
125 
133  std::shared_ptr<GameObject> getObjectConst(std::size_t id) const {
134  PROFILER_FUNC_START();
135  PROFILER_FUNC_END();
136  return getGameObjectConst(id);
137  }
138 
139  std::vector<std::shared_ptr<GameObject>> getAllGameObjects();
140 
147  std::vector<std::shared_ptr<GameObject>> getAllObjects() {
148  PROFILER_FUNC_START();
149  PROFILER_FUNC_END();
150  return getAllGameObjects();
151  }
152 
153  std::shared_ptr<GameObject> &getGameObject(Entity const& en);
154 
162  std::shared_ptr<GameObject> &getObject(Entity const& en) {
163  PROFILER_FUNC_START();
164  PROFILER_FUNC_END();
165  return getGameObject(en);
166  }
167 
168  std::vector<std::shared_ptr<GameObject>> getGameObjects(std::string const& name);
169 
170  std::vector<std::shared_ptr<GameObject>> getGameObjectByTag(std::string const& tag);
171 
172  KEngine &getEngine();
173 
181  bool isGameObjectExists(std::size_t const& index);
182 
190  bool isGameObjectExists(Entity const& en);
191 
192  #if !KAPENGINE_BETA_ACTIVE
193 
196  void __update();
197 
198  #if KAPENGINE_THREAD_ACTIVE
199 
205  static void __threadSceneUpdate(std::vector<std::shared_ptr<GameObject>> gos, bool physics);
206  #endif
207  #else
208  #if KAPENGINE_THREAD_ACTIVE
209  void __update(int idThread);
210  void __updateMain();
211  void __updatePhysics();
212  void __updateComponents();
213  void __updateRender();
214  #else
215  void __update();
216  #endif
217  #endif
218 
221  void __changingScene();
225  void __engineStop(bool currentScene = false);
229  void __init();
233  void __finit();
234 
240  void dump(bool showComponents = false);
241 
242  std::shared_ptr<GameObject> createGameObject(std::string const& name);
243 
250  std::shared_ptr<GameObject> findFirstGameObject(std::string const& name);
251 
257  void registerTmpActionAfterUpdate(std::function<void(Scene &scene)> action) {
258  PROFILER_FUNC_START();
259  _tmpActionsAfterUpdate.push_back(action);
260  PROFILER_FUNC_END();
261  }
262 
263  bool __isChangingScene() const {
264  return _changingScene;
265  }
266 
267  protected:
268  private:
269  std::size_t _id = 0;
270  std::string _name;
271  std::size_t _idObjectMax = 0;
272  SceneManager &manager;
273  std::vector<std::shared_ptr<GameObject>> _gameObjects;
274  std::vector<std::shared_ptr<GameObject>> _gameObjectsRun;
275  std::vector<std::size_t> _gameObjectsToDestroy;
276  bool _changingScene = false;
277 
278  void __checkDestroy();
279  #if KAPENGINE_THREAD_ACTIVE
280  #if KAPENGINE_BETA_ACTIVE
281  std::mutex _mutex;
282  bool _destroying = false;
283  #else
284  void __checkThread();
285  #endif
286  #endif
287  std::size_t __nbGameObjectNoParent();
288  std::vector<std::shared_ptr<GameObject>> __getGameObjectsNoParent();
289  void __updateGameObjects(std::vector<std::shared_ptr<GameObject>> objs);
290  std::vector<std::function<void(Scene &scene)>> _tmpActionsAfterUpdate;
291  };
292 
293  }
294 
295 }
296 
297 #endif /* !SCENE_HPP_ */
KapEngine::SceneManagement::Scene::__changingScene
void __changingScene()
Definition: Scene.cpp:307
KapEngine::SceneManagement::Scene::__update
void __update()
Definition: Scene.cpp:528
KapEngine::SceneManagement::SceneManager
Definition: SceneManager.hpp:32
KapEngine::SceneManagement::Scene::findFirstGameObject
std::shared_ptr< GameObject > findFirstGameObject(std::string const &name)
get first GameObject with it's name
Definition: Scene.cpp:136
KapEngine::SceneManagement::Scene::getAllObjects
std::vector< std::shared_ptr< GameObject > > getAllObjects()
Get the All Objects object.
Definition: Scene.hpp:147
KapEngine::GameObject
Definition: GameObject.hpp:40
KapEngine::SceneManagement::Scene::__init
void __init()
Definition: Scene.cpp:359
KapEngine::SceneManagement::Scene::getObject
std::shared_ptr< GameObject > & getObject(Entity const &en)
Get the Object object.
Definition: Scene.hpp:162
KapEngine::Component
Definition: Component.hpp:38
KapEngine::SceneManagement::Scene::dump
void dump(bool showComponents=false)
Display all objects in scene.
Definition: Scene.cpp:273
KapEngine::SceneManagement::Scene::__finit
void __finit()
Definition: Scene.cpp:372
KapEngine::SceneManagement::Scene::getObjectConst
std::shared_ptr< GameObject > getObjectConst(std::size_t id) const
Get the Object Const object.
Definition: Scene.hpp:133
KapEngine
main namespace
Definition: Component.hpp:17
KapEngine::SceneManagement::Scene::__engineStop
void __engineStop(bool currentScene=false)
Definition: Scene.cpp:400
KapEngine::SceneManagement::Scene::isGameObjectExists
bool isGameObjectExists(std::size_t const &index)
return if GameObject exists
Definition: Scene.cpp:196
KapEngine::SceneManagement::Scene::registerTmpActionAfterUpdate
void registerTmpActionAfterUpdate(std::function< void(Scene &scene)> action)
add temporary action when scene updated
Definition: Scene.hpp:257
KapEngine::SceneManagement::Scene::getGameObject
std::shared_ptr< GameObject > & getGameObject(std::size_t id)
Get the Game Object object.
Definition: Scene.cpp:72
KapEngine::SceneManagement::Scene
Definition: Scene.hpp:37
KapEngine::SceneManagement::Scene::getObject
std::shared_ptr< GameObject > & getObject(std::size_t id)
Get GameObject object.
Definition: Scene.hpp:118
KapEngine::KEngine
Class of engine.
Definition: Engine.hpp:60
KapEngine::Entity
Definition: Entity.hpp:15