Level H Engine
GameObject.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <vector>
5 #include "../Components/Component.h"
6 #include "Application.h"
7 
9 class Application;
10 class Component;
11 
15 class GameObject : public std::enable_shared_from_this<GameObject>
16 {
17 public:
22  GameObject(std::string name);
23 
27  virtual ~GameObject();
28 
32  template <class T>
33  std::weak_ptr<T> addComponent()
34  {
35  // Create new component
36  std::shared_ptr<T> component(new T());
37 
38  //Store the new component in the game object component vector
39  components.push_back(component);
40 
41  // Store a reference to the game object inside component
42  component->gameObject = shared_from_this();
43  //Activate the component
44  component->onAwake();
45 
46  //store the id
47  componentIDs.push_back(component->getID());
48 
49  //Return a weak pointer to the component to allow instance use.
50  return component;
51  }
52 
56  template<class T>
57  std::weak_ptr<T> getComponent()
58  {
59  //loop though the components
60  for (unsigned int i = 0; i < components.size(); i++)
61  {
62  // Get a weak pointer for the current component
63  std::shared_ptr<T> component = std::dynamic_pointer_cast<T>(components.at(i));
64 
65  // Check if the component is found
66  if (component.get() != nullptr)
67  {
68  //return a weak pointer to the component.
69  return component;
70  }
71  }
72 
73  //if the component isn't found return a NULL weak pointer
74  return std::weak_ptr<T>();
75  }
76 
82  static std::weak_ptr<GameObject> create(std::string name);
83 
88  void setName(std::string inName) { name = inName; }
89 
94  std::string getName() { return name; }
95 
99  virtual void awake();
100 
104  virtual void update();
105 
109  virtual void render();
110 
114  virtual void destroy();
115 
120  void setDestroyed(bool inDestroyed) { destroyed = inDestroyed; }
121 
126  bool getDestroyed() { return destroyed; }
127 
133  bool checkForComponent(std::string id);
134 
135 private:
137  std::string name;
139  std::vector < std::shared_ptr<Component> > components;
141  std::vector < std::string > componentIDs;
143  bool destroyed;
144 };
GameObject(std::string name)
Constructs Application.
Definition: GameObject.cpp:3
std::vector< std::shared_ptr< Component > > components
A vector of components attached to the game object.
Definition: GameObject.h:139
std::weak_ptr< T > addComponent()
A template for adding components to the components vector.
Definition: GameObject.h:33
A class that handles the components.
Definition: Component.h:13
static std::weak_ptr< GameObject > create(std::string name)
A static function to create a game object.
Definition: GameObject.cpp:11
bool destroyed
A boolean for if the game object is destroyed.
Definition: GameObject.h:143
void setDestroyed(bool inDestroyed)
A function to set the componets destroyed boolean.
Definition: GameObject.h:120
std::string getName()
A function to get the name of the game object.
Definition: GameObject.h:94
virtual void destroy()
A virtual function for the game objects destroy.
Definition: GameObject.cpp:55
bool getDestroyed()
A function to get the componets destroyed boolean.
Definition: GameObject.h:126
void setName(std::string inName)
A function to set the name of the game object.
Definition: GameObject.h:88
std::string name
The name of the game object.
Definition: GameObject.h:137
virtual void render()
A virtual function for the game objects render.
Definition: GameObject.cpp:47
Contains details and functions for the game object.
Definition: GameObject.h:15
virtual ~GameObject()
Destructs GameObject (virtual).
Definition: GameObject.cpp:7
virtual void update()
A virtual function for the game objects update.
Definition: GameObject.cpp:28
Contains details and functions for the application.
Definition: Application.h:17
std::vector< std::string > componentIDs
A vector of the component ID&#39;s contained in the game object.
Definition: GameObject.h:141
std::weak_ptr< T > getComponent()
A template for getting a component from the components vector.
Definition: GameObject.h:57
bool checkForComponent(std::string id)
A function to check the if the game object has a specific componet.
Definition: GameObject.cpp:63
virtual void awake()
A virtual function for the game objects awake.
Definition: GameObject.cpp:20