Platformer
Platformer
GameObject.hpp
1 #ifndef GAME_OBJECT_HPP
2 #define GAME_OBJECT_HPP
3 
4 #include <unordered_map>
5 #include "RenderManager.hpp"
6 
7 #include "Component.hpp"
8 #include "Vector2.hpp"
9 
14 class GameObject : public std::enable_shared_from_this<GameObject> {
15  public:
20  static std::shared_ptr<GameObject>
21  Create(
22  int posX,
24  int posY,
26  int width,
28  int height,
30  bool isUI = false,
32  bool isPermanent = false);
33 
38  static std::shared_ptr<GameObject>
40  int posX,
42  int posY,
44  int width,
46  int height);
47 
49  void Update();
50 
53 
56 
60  void Disable();
61 
63  void Enable();
64 
69  bool IsActive() const;
70 
75  bool IsColliding(
76  const GameObject& other) const;
77 
82  SDL_Rect ScreenRegion();
83 
89  template <class C>
90  std::shared_ptr<C> GetComponent() {
91  static_assert(std::is_base_of<Component, C>::value,
92  "C must derive from Component");
93 
94  auto it = componentMap.find(GetTypeID<C>());
95  if (it == componentMap.end()) {
96  return std::shared_ptr<C>(NULL);
97  }
98  return std::static_pointer_cast<C>(it->second);
99  }
100 
105  template <class C>
106  std::shared_ptr<GameObject> AddComponent(std::shared_ptr<C> comp) {
107  static_assert(std::is_base_of<Component, C>::value,
108  "C must derive from Component");
109 
110  static_cast<std::shared_ptr<Component>>(comp)->SetParentGameObject(
111  std::weak_ptr<GameObject>(shared_from_this()));
112  componentMap[GetTypeID<C>()] = comp;
113  comp->Init();
114 
115  return shared_from_this();
116  }
117 
118  private:
120  GameObject(
121  int posX,
123  int posY,
125  int width,
127  int height,
129  bool isUI);
130 
132  std::unordered_map<int, std::shared_ptr<Component>> componentMap;
133 
135  bool active;
136 
138  bool isUI;
139 
141  static int lastTypeID;
142 
147  template <class Key>
148  inline static int GetTypeID() {
149  static const int id = lastTypeID++;
150  return id;
151  }
152 };
153 
154 #endif
Vector2 scale
The scale of this GameObject.
Definition: GameObject.hpp:55
bool IsColliding(const GameObject &other) const
Gets whether this GameObject is colliding with another.
Definition: GameObject.cpp:35
void Enable()
Enables this GameObject.
Definition: GameObject.cpp:31
static std::shared_ptr< GameObject > Create(int posX, int posY, int width, int height, bool isUI=false, bool isPermanent=false)
Creates a GameObject.
Definition: GameObject.cpp:10
static std::shared_ptr< GameObject > CreateStandard(int posX, int posY, int width, int height)
Creates a non-UI, non-permanent GameObject.
Definition: GameObject.cpp:18
static int GetTypeID()
Gets a new unused component id.
Definition: GameObject.hpp:148
std::shared_ptr< C > GetComponent()
Gets the first component of type C in the component map or null if none are found.
Definition: GameObject.hpp:90
bool IsActive() const
Gets whether this GameObject is active.
Definition: GameObject.cpp:33
SDL_Rect ScreenRegion()
Determines the screen region this object takes up.
Definition: GameObject.cpp:44
Basic class representation of a vector 2 with an x and y coordinate.
Definition: Vector2.hpp:8
GameObject(int posX, int posY, int width, int height, bool isUI)
Constructs a GameObject.
Definition: GameObject.cpp:7
void Update()
Updates this GameObject.
Definition: GameObject.cpp:23
A GameObject class.
Definition: GameObject.hpp:14
std::unordered_map< int, std::shared_ptr< Component > > componentMap
The map of component ids and components for this GameObject.
Definition: GameObject.hpp:132
bool active
Whether or not this GameObject is active.
Definition: GameObject.hpp:135
Vector2 position
The position of this GameObject.
Definition: GameObject.hpp:52
void Disable()
Disables this GameObject.
Definition: GameObject.cpp:29
static int lastTypeID
The last component id used.
Definition: GameObject.hpp:141
std::shared_ptr< GameObject > AddComponent(std::shared_ptr< C > comp)
Adds a component of type C to the component map.
Definition: GameObject.hpp:106
bool isUI
Whether this GameObject should use direct screen coordinates.
Definition: GameObject.hpp:138