R-Type
R-Type project
GameObject.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** gameEngine2
4 ** File description:
5 ** GameObject
6 */
7 
8 #ifndef GAMEOBJECT_HPP_
9 #define GAMEOBJECT_HPP_
10 
11 #include "Component.hpp"
12 #include "Scene.hpp"
13 #include "Entity.hpp"
14 #include "Type.hpp"
15 
16 #if KAPENGINE_BETA_ACTIVE
17  #if KAPENGINE_THREAD_ACTIVE
18  #include <mutex>
19  #endif
20 #endif
21 
22 namespace KapEngine {
23 
24  class Component;
25  class KEngine;
26  class Entity;
27 
28  namespace SceneManagement {
29  class Scene;
30  }
31 }
32 
33 #include <vector>
34 #include <memory>
35 #include <string>
36 #include <typeinfo>
37 
38 namespace KapEngine {
39 
40  class GameObject : public Entity {
41  public:
42  GameObject(SceneManagement::Scene &scene, std::string const& name);
43  ~GameObject();
44 
45  //all function can be called by dev
46 
52  void addComponent(std::shared_ptr<Component> comp);
58  void removeComponent(std::shared_ptr<Component> comp);
64  void removeComponent(std::size_t id);
65 
73  Component &getComponent(std::string const& componentName);
74 
85  template<typename T, typename = std::enable_if<std::is_base_of<Component, T>::value>>
86  T &getComponent(std::string const& componentName) {
87  PROFILER_FUNC_START();
88  PROFILER_FUNC_END();
89  return dynamic_cast<T &>(getComponent(componentName));
90  }
99  template<typename T, typename = std::enable_if<std::is_base_of<Component, T>::value>>
101  std::size_t hash = Type::getHashCode<T>();
102  for (std::size_t i = 0; i < _components.size(); i++) {
103  std::size_t componentHash = Type::getHashCode(*_components[i]);
104  if (_components[i] && componentHash == hash) {
105  return dynamic_cast<T &>(*_components[i]);
106  }
107  }
108  for (std::size_t i = 0; i < _componentsRun.size(); i++) {
109  std::size_t componentHash = Type::getHashCode(*_componentsRun[i]);
110  if (_componentsRun[i] && componentHash == hash) {
111  return dynamic_cast<T &>(*_componentsRun[i]);
112  }
113  }
114  throw Errors::GameObjectError("No component found");
115  }
124  template<typename T, typename = std::enable_if<std::is_base_of<Component, T>::value>>
125  std::vector<std::shared_ptr<T>> getComponents() {
126  PROFILER_FUNC_START();
127  std::vector<std::shared_ptr<T>> components;
128  std::size_t hash = Type::getHashCode<T>();
129  for (std::size_t i = 0; i < _components.size(); i++) {
130  std::size_t componentHash = Type::getHashCode(*_components[i]);
131  if (_components[i] && componentHash == hash)
132  components.push_back(std::dynamic_pointer_cast<T>(_components[i]));
133  }
134  for (std::size_t i = 0; i < _componentsRun.size(); i++) {
135  std::size_t componentHash = Type::getHashCode(*_componentsRun[i]);
136  if (_componentsRun[i] && componentHash == hash)
137  components.push_back(std::dynamic_pointer_cast<T>(_componentsRun[i]));
138  }
139  PROFILER_FUNC_END();
140  return components;
141  }
142 
151  bool hasComponent(std::string const& componentName) const;
152 
162  template<typename T, typename = std::enable_if<std::is_base_of<Component, T>::value>>
163  bool hasComponent() const {
164  PROFILER_FUNC_START();
165  std::size_t hash = Type::getHashCode<T>();
166  for (std::size_t i = 0; i < _components.size(); i++) {
167  std::size_t componentHash = Type::getHashCode(*_components[i]);
168  if (_components[i] && componentHash == hash) {
169  PROFILER_FUNC_END();
170  return true;
171  }
172  }
173  for (std::size_t i = 0; i < _componentsRun.size(); i++) {
174  std::size_t componentHash = Type::getHashCode(*_componentsRun[i]);
175  if (_componentsRun[i] && componentHash == hash) {
176  PROFILER_FUNC_END();
177  return true;
178  }
179  }
180  return false;
181  }
182 
188  KEngine &getEngine();
189 
190  #if KAPENGINE_BETA_ACTIVE
191  #if KAPENGINE_THREAD_ACTIVE
192  void __updatePhysics();
193  void __updateComponents();
194  void __updateDisplay();
195  #else
196  void __update();
200  void __updateDisplay();
201  #endif
202  #else
203 
206  void __update(bool physics = false, bool runDisplay = true);
210  void __updateDisplay();
211  #endif
212 
218  void setActive(bool b);
225  bool isActive() const {
226  return _active;
227  }
234  bool isDestroyed() const {
235  return _destroyed;
236  }
237 
244  PROFILER_FUNC_START();
245  PROFILER_FUNC_END();
246  return _scene;
247  }
248 
255  PROFILER_FUNC_START();
256  PROFILER_FUNC_END();
257  return _scene;
258  }
259 
266 
270  void __engineStop();
271 
277  std::string getName() const {
278  return _name;
279  }
280 
286  Entity &getEntity() const {
287  PROFILER_FUNC_START();
288  PROFILER_FUNC_END();
289  return (Entity &)*this;
290  }
291 
295  void __destroyIt();
299  void __init();
303  void __stoppingGame();
307  void __onSceneGonnaUpdated();
311  void __onSceneUpdated();
312 
319  void dump(bool displayComponent = false, std::string prefix = "");
320 
326  std::vector<std::shared_ptr<Component>> getAllComponents() const;
327 
332  void destroy();
333 
339  void __setPrefab(std::string const& name);
340 
346  std::string getPrefabName() const {
347  PROFILER_FUNC_START();
348  PROFILER_FUNC_END();
349  return _prefabName;
350  }
351 
357  void setName(std::string const& name) {
358  PROFILER_FUNC_START();
359  #if KAPENGINE_BETA_ACTIVE
360  #if KAPENGINE_THREAD_ACTIVE
361  _mutex.lock();
362  #endif
363  #endif
364  _name = name;
365  #if KAPENGINE_BETA_ACTIVE
366  #if KAPENGINE_THREAD_ACTIVE
367  _mutex.unlock();
368  #endif
369  #endif
370  PROFILER_FUNC_END();
371  }
372 
378  void setTag(std::string const& tag) {
379  PROFILER_FUNC_START();
380  #if KAPENGINE_BETA_ACTIVE
381  #if KAPENGINE_THREAD_ACTIVE
382  _mutex.lock();
383  #endif
384  #endif
385  _tag = tag;
386  #if KAPENGINE_BETA_ACTIVE
387  #if KAPENGINE_THREAD_ACTIVE
388  _mutex.unlock();
389  #endif
390  #endif
391  PROFILER_FUNC_END();
392  }
393 
399  std::string getTag() const {
400  PROFILER_FUNC_START();
401  PROFILER_FUNC_END();
402  return _tag;
403  }
404 
409  void __onSceneChanged();
410 
411  protected:
412  private:
413  std::string _name;
414  bool _active = true;
415  bool _startActive = true;
416  bool _destroyed = false;
417  bool _firstUpdateDone = false;
418  std::vector<std::shared_ptr<Component>> _components;
419  std::vector<std::shared_ptr<Component>> _componentsRun;
420  SceneManagement::Scene &_scene;
421  std::size_t _idComp = 0;
422  std::string _prefabName;
423  std::string _tag;
424  #if KAPENGINE_BETA_ACTIVE
425  #if KAPENGINE_THREAD_ACTIVE
426  std::mutex _mutex;
427  #endif
428  #endif
429  };
430 
431 }
432 
433 #endif /* !GAMEOBJECT_HPP_ */
KapEngine::GameObject::removeComponent
void removeComponent(std::shared_ptr< Component > comp)
remove component from its shared_ptr
KapEngine::GameObject::getComponent
T & getComponent(std::string const &componentName)
Get the Component by its type.
Definition: GameObject.hpp:86
KapEngine::GameObject::getEntity
Entity & getEntity() const
Get the Entity of GameObject.
Definition: GameObject.hpp:286
KapEngine::GameObject::getPrefabName
std::string getPrefabName() const
Get the Prefab Name.
Definition: GameObject.hpp:346
KapEngine::GameObject::__onSceneChanged
void __onSceneChanged()
call when scene changed to another scene
Definition: GameObject.cpp:644
KapEngine::GameObject::__engineStop
void __engineStop()
Definition: GameObject.cpp:470
KapEngine::GameObject::__update
void __update(bool physics=false, bool runDisplay=true)
Definition: GameObject.cpp:288
KapEngine::GameObject::__onSceneGonnaUpdated
void __onSceneGonnaUpdated()
Definition: GameObject.cpp:629
KapEngine::GameObject::destroy
void destroy()
destroy the GameObject
Definition: GameObject.cpp:602
KapEngine::GameObject::isDestroyed
bool isDestroyed() const
return if GameObject is destroyed
Definition: GameObject.hpp:234
KapEngine::GameObject::isActive
bool isActive() const
return if GameObject is active
Definition: GameObject.hpp:225
KapEngine::GameObject::__destroyIt
void __destroyIt()
Definition: GameObject.cpp:499
KapEngine::GameObject
Definition: GameObject.hpp:40
KapEngine::GameObject::getEngine
KEngine & getEngine()
Get the Engine.
Definition: GameObject.cpp:454
KapEngine::GameObject::getComponents
std::vector< std::shared_ptr< T > > getComponents()
Get the Components by there type.
Definition: GameObject.hpp:125
KapEngine::GameObject::getComponent
T & getComponent()
Get the Component by its type.
Definition: GameObject.hpp:100
KapEngine::GameObject::getAllComponents
std::vector< std::shared_ptr< Component > > getAllComponents() const
Get the All Components of GameObject.
Definition: GameObject.cpp:589
KapEngine::Errors::GameObjectError
Definition: Errors.hpp:52
KapEngine::GameObject::__init
void __init()
Definition: GameObject.cpp:512
KapEngine::GameObject::__stoppingGame
void __stoppingGame()
Definition: GameObject.cpp:548
KapEngine::GameObject::__updateDisplay
void __updateDisplay()
Definition: GameObject.cpp:363
KapEngine::GameObject::getScene
SceneManagement::Scene & getScene()
Get the Scene.
Definition: GameObject.hpp:243
KapEngine::GameObject::__setPrefab
void __setPrefab(std::string const &name)
set prefab name
Definition: GameObject.cpp:608
KapEngine::Component
Definition: Component.hpp:38
KapEngine::GameObject::setName
void setName(std::string const &name)
Set the Name of GameObject.
Definition: GameObject.hpp:357
KapEngine::GameObject::getName
std::string getName() const
Get GameObject Name.
Definition: GameObject.hpp:277
KapEngine::GameObject::dump
void dump(bool displayComponent=false, std::string prefix="")
display GameObject in log
Definition: GameObject.cpp:559
KapEngine::GameObject::getSceneConst
SceneManagement::Scene & getSceneConst() const
Get the Scene.
Definition: GameObject.hpp:254
KapEngine::GameObject::setActive
void setActive(bool b)
Set the Active status of GameObject.
Definition: GameObject.cpp:523
KapEngine::GameObject::getTag
std::string getTag() const
Get the GameObject Tag.
Definition: GameObject.hpp:399
KapEngine::GameObject::getTransform
Component & getTransform()
Get the Transform.
Definition: GameObject.cpp:446
KapEngine::GameObject::addComponent
void addComponent(std::shared_ptr< Component > comp)
add new component to GameObject
Definition: GameObject.cpp:458
KapEngine
main namespace
Definition: Component.hpp:17
KapEngine::GameObject::setTag
void setTag(std::string const &tag)
Set the GameObject Tag.
Definition: GameObject.hpp:378
KapEngine::GameObject::hasComponent
bool hasComponent() const
return if GameObject has component from its type
Definition: GameObject.hpp:163
KapEngine::GameObject::__onSceneUpdated
void __onSceneUpdated()
Definition: GameObject.cpp:614
KapEngine::SceneManagement::Scene
Definition: Scene.hpp:37
KapEngine::KEngine
Class of engine.
Definition: Engine.hpp:60
KapEngine::Entity
Definition: Entity.hpp:15