R-Type
R-Type project
Component.hpp
1 /*
2 ** EPITECH PROJECT, 2022
3 ** gameEngine2
4 ** File description:
5 ** Component
6 */
7 
8 #ifndef COMPONENT_HPP_
9 #define COMPONENT_HPP_
10 
11 #include "GameObject.hpp"
12 #include "IComponent.hpp"
13 #include "Input.hpp"
14 #include "Mouse.hpp"
15 #include "Profiler/KapProfiler.hpp"
16 
17 namespace KapEngine {
18  class GameObject;
19  class Transform;
20  class KEngine;
21 
22  namespace Events {
23  class Input;
24  class Mouse;
25  }
26 
27  namespace SceneManagement {
28  class Scene;
29  }
30 }
31 
32 #include <memory>
33 #include <vector>
34 #include <string>
35 
36 namespace KapEngine {
37 
38  class Component : public IComponent {
39  public:
40  Component(std::shared_ptr<GameObject> &go, std::string const& name);
41  Component(GameObject &go, std::string const& name);
42  ~Component();
43 
48  void __update(bool runDisplay = true);
49 
54  bool __setId(std::size_t i) {
55  PROFILER_FUNC_START();
56  if (_id != 0) {
57  PROFILER_FUNC_END();
58  return false;
59  }
60  _id = i;
61  PROFILER_FUNC_END();
62  return true;
63  }
64 
71  GameObject &getGameObject() const;
72  std::size_t getGameObjectId() const;
73 
74  //updates functions
75  virtual void onInit() override {}
76  virtual void onAwake() override {}
77  virtual void onStart() override {}
78  virtual void onUpdate() override {}
79  virtual void onFixedUpdate() override {}
80 
81  virtual void onDisplay() override {}
82 
83  virtual void onTriggerEnter(std::shared_ptr<GameObject> go) override {}
84  virtual void onTriggerStay(std::shared_ptr<GameObject> go) override {}
85  virtual void onTriggerExit(std::shared_ptr<GameObject> go) override {}
86 
87  virtual void onMouseEnter() override {}
88  virtual void onMouseStay() override {}
89  virtual void onMouseExit() override {}
90 
91  virtual bool checkComponentValidity() override {
92  return true;
93  }
94 
95  virtual void onGameQuit() override {}
96  virtual void onDestroy() override {}
97  virtual void onDisable() override {}
98  virtual void onEnable() override {}
99 
100  virtual void onSceneUpdated() override {}
101  virtual void onSceneGonnaUpdated() override {}
102  virtual void onSceneChanged() override {}
103 
111  std::size_t getLevel() const {
112  PROFILER_FUNC_START();
113  PROFILER_FUNC_END();
114  return _level;
115  }
123  void setLevel(std::size_t lvl) {
124  PROFILER_FUNC_START();
125  _level = lvl;
126  PROFILER_FUNC_END();
127  }
128 
135  bool isEnable() const {
136  PROFILER_FUNC_START();
137  PROFILER_FUNC_END();
138  return _enable;
139  }
140 
147  bool isDisable() const {
148  PROFILER_FUNC_START();
149  PROFILER_FUNC_END();
150  return !_enable;
151  }
152 
158  void setActive(bool b) {
159  PROFILER_FUNC_START();
160  PROFILER_FUNC_END();
161  _enable = b;
162  }
163 
168  std::string getName() const {
169  return _name;
170  }
171 
175  void __engineStop() {
176  PROFILER_FUNC_START();
177  PROFILER_FUNC_END();
178  }
179 
185  int getThreadRunning() const {
186  PROFILER_FUNC_START();
187  PROFILER_FUNC_END();
188  return threadRunning;
189  }
190 
201 
207  void addRequireComponent(std::string const& componentName) {
208  PROFILER_FUNC_START();
209  _componentsNeeded.push_back(componentName);
210  PROFILER_FUNC_END();
211  }
212 
216  void __fixedUpdate();
217 
222  void __awake();
223 
230 
236  std::size_t getId() const {
237  PROFILER_FUNC_START();
238  PROFILER_FUNC_END();
239  return _id;
240  }
241 
248  void __setPhysics(bool b) {
249  PROFILER_FUNC_START();
250  _physics = b;
251  PROFILER_FUNC_END();
252  }
253 
260  bool __isPhysics() const {
261  return _physics;
262  }
263 
264  void __sceneChanged() {
265  PROFILER_FUNC_START();
266  onSceneChanged();
267  _awakeDone = false;
268  _startDone = false;
269  PROFILER_FUNC_END();
270  }
271 
272  SceneManagement::Scene &getScene() const {
273  PROFILER_FUNC_START();
274  PROFILER_FUNC_END();
275  return _scene;
276  }
277 
278  KEngine &getEngine() const {
279  PROFILER_FUNC_START();
280  PROFILER_FUNC_END();
281  return _engine;
282  }
283 
284  protected:
288  std::string _name;
289  private:
290  bool _physics = false;
291  int threadRunning = 2;
292  std::size_t _id = 0;
293  std::size_t _level = 0;
294  bool _enable = true;
295  bool _awakeDone = false;
296  bool _startDone = false;
297  void __start();
298  std::vector<std::string> _componentsNeeded;
299  SceneManagement::Scene &_scene;
300  KEngine &_engine;
301  GameObject &obj;
302 
303  bool __checkValidity();
304  };
305 
306 }
307 
308 #endif /* !COMPONENT_HPP_ */
KapEngine::Component::__fixedUpdate
void __fixedUpdate()
Definition: Component.cpp:102
KapEngine::Component::onTriggerEnter
virtual void onTriggerEnter(std::shared_ptr< GameObject > go) override
something enter in object
Definition: Component.hpp:83
KapEngine::Component::isDisable
bool isDisable() const
return if component is disbale
Definition: Component.hpp:147
KapEngine::Component::onTriggerStay
virtual void onTriggerStay(std::shared_ptr< GameObject > go) override
something stay in object
Definition: Component.hpp:84
KapEngine::Component::_name
std::string _name
Component name.
Definition: Component.hpp:288
KapEngine::Component::onSceneChanged
virtual void onSceneChanged() override
call when scene is changed
Definition: Component.hpp:102
KapEngine::Component::getId
std::size_t getId() const
Get the Id of component.
Definition: Component.hpp:236
KapEngine::Component::onAwake
virtual void onAwake() override
call component awake
Definition: Component.hpp:76
KapEngine::Events::Mouse
Definition: Mouse.hpp:30
KapEngine::Component::getThreadRunning
int getThreadRunning() const
Get the Thread Running index index of thread component.
Definition: Component.hpp:185
KapEngine::Component::onDisable
virtual void onDisable() override
call when GameObject is disable after being enable
Definition: Component.hpp:97
KapEngine::Component::__setPhysics
void __setPhysics(bool b)
set if it is a physic component if component is a physic component then it will be updated in physic ...
Definition: Component.hpp:248
KapEngine::Component::getGameObject
GameObject & getGameObject() const
Get the Game Object object.
Definition: Component.cpp:35
KapEngine::Transform
Definition: Transform.hpp:28
KapEngine::IComponent
Definition: IComponent.hpp:19
KapEngine::Component::__isPhysics
bool __isPhysics() const
check if component is a physic component
Definition: Component.hpp:260
KapEngine::Component::isEnable
bool isEnable() const
return if component is enable
Definition: Component.hpp:135
KapEngine::Component::onSceneGonnaUpdated
virtual void onSceneGonnaUpdated() override
call when scene will update (not threaded)
Definition: Component.hpp:101
KapEngine::GameObject
Definition: GameObject.hpp:40
KapEngine::Component::getLevel
std::size_t getLevel() const
Get the Level component.
Definition: Component.hpp:111
KapEngine::Component::getName
std::string getName() const
Get the component name.
Definition: Component.hpp:168
KapEngine::Component::getMouse
Events::Mouse getMouse()
Get mouse events.
Definition: Component.cpp:115
KapEngine::Component::__update
void __update(bool runDisplay=true)
function call by KapEngine to update your component
Definition: Component.cpp:43
Input.hpp
KapEngine::Component::getInput
Events::Input getInput()
Get input events.
Definition: Component.cpp:109
KapEngine::Component::onMouseStay
virtual void onMouseStay() override
called when mouse stay on object
Definition: Component.hpp:88
KapEngine::Component::onMouseExit
virtual void onMouseExit() override
called when mouse leave object
Definition: Component.hpp:89
KapEngine::Component::onStart
virtual void onStart() override
call component start
Definition: Component.hpp:77
KapEngine::Component::onMouseEnter
virtual void onMouseEnter() override
called when mouse enter object
Definition: Component.hpp:87
KapEngine::Component::__awake
void __awake()
check awake validity
Definition: Component.cpp:75
KapEngine::Component::onDisplay
virtual void onDisplay() override
use to display some things this function is call after all updates fucntion
Definition: Component.hpp:81
KapEngine::Component::onUpdate
virtual void onUpdate() override
call each frame
Definition: Component.hpp:78
KapEngine::Component::addRequireComponent
void addRequireComponent(std::string const &componentName)
Add require component If your component need another component to work, you can add it's name with th...
Definition: Component.hpp:207
KapEngine::Component
Definition: Component.hpp:38
KapEngine::Component::__setId
bool __setId(std::size_t i)
function call by KapEngine to init your component
Definition: Component.hpp:54
KapEngine::Component::__engineStop
void __engineStop()
Definition: Component.hpp:175
KapEngine::Events::Input
Input actions.
Definition: Input.hpp:38
KapEngine::Component::setLevel
void setLevel(std::size_t lvl)
Set the Level object.
Definition: Component.hpp:123
KapEngine::Component::onEnable
virtual void onEnable() override
call when GameObject is enable after being disable
Definition: Component.hpp:98
KapEngine
main namespace
Definition: Component.hpp:17
KapEngine::Component::onFixedUpdate
virtual void onFixedUpdate() override
call eachv x ms
Definition: Component.hpp:79
KapEngine::Component::onDestroy
virtual void onDestroy() override
call when component destroyed
Definition: Component.hpp:96
KapEngine::Component::onGameQuit
virtual void onGameQuit() override
call when game is turn off
Definition: Component.hpp:95
KapEngine::Component::onTriggerExit
virtual void onTriggerExit(std::shared_ptr< GameObject > go) override
something out of object
Definition: Component.hpp:85
KapEngine::Component::checkComponentValidity
virtual bool checkComponentValidity() override
Check you component is runnable With this function you can add some conditions to make your component...
Definition: Component.hpp:91
KapEngine::Component::getTransform
Transform & getTransform()
Get the Transform component.
Definition: Component.cpp:156
KapEngine::Component::setActive
void setActive(bool b)
Set the active status of component if true : component is active else component is disable.
Definition: Component.hpp:158
KapEngine::Component::onSceneUpdated
virtual void onSceneUpdated() override
call when scene is updated (not threaded)
Definition: Component.hpp:100
KapEngine::SceneManagement::Scene
Definition: Scene.hpp:37
KapEngine::Component::onInit
virtual void onInit() override
call on component init
Definition: Component.hpp:75
KapEngine::KEngine
Class of engine.
Definition: Engine.hpp:60