Practical Tools for Simple Design
Loading...
Searching...
No Matches
GameObject.hpp
1#ifndef UTIL_GAME_OBJECT_HPP
2#define UTIL_GAME_OBJECT_HPP
3
4#include "pch.hpp" // IWYU pragma: export
5
6#include "Core/Drawable.hpp"
7
8#include "Util/Transform.hpp"
9
10namespace Util {
21public:
22 Util::Transform m_Transform; // IDC if this should be here.
23
24public:
28 GameObject() = default;
29
38 GameObject(const std::shared_ptr<Core::Drawable> &drawable,
39 const float zIndex, const glm::vec2 &pivot = {0, 0},
40 const bool visible = true,
41 const std::vector<std::shared_ptr<GameObject>> &children =
42 std::vector<std::shared_ptr<GameObject>>())
43 : m_Drawable(drawable),
44 m_Children(children),
45 m_ZIndex(zIndex),
46 m_Visible(visible),
47 m_Pivot(pivot) {}
48
56 GameObject(const GameObject &other) = default;
57
61 GameObject(GameObject &&other) = default;
62
66 virtual ~GameObject() = default;
67
68 // Deleted assignment operator.
69 GameObject &operator=(const GameObject &other) = delete;
70
76 float GetZIndex() const { return m_ZIndex; }
77
83 Transform GetTransform() const { return m_Transform; }
84
90 glm::vec2 GetScaledSize() const {
91 return m_Drawable->GetSize() * m_Transform.scale;
92 };
93
99 const std::vector<std::shared_ptr<GameObject>> &GetChildren() const {
100 return m_Children;
101 }
102
108 void SetPivot(const glm::vec2 &pivot) { m_Pivot = pivot; }
109
117 void SetZIndex(float index) { m_ZIndex = index; }
118
124 void SetDrawable(const std::shared_ptr<Core::Drawable> &drawable) {
125 m_Drawable = drawable;
126 }
127
133 void SetVisible(const bool visible) { m_Visible = visible; }
134
140 void AddChild(const std::shared_ptr<GameObject> &child) {
141 m_Children.push_back(child);
142 }
143
149 void RemoveChild(const std::shared_ptr<GameObject> &child) {
150 m_Children.erase(
151 std::remove(m_Children.begin(), m_Children.end(), child),
152 m_Children.end());
153 }
154
155 void Draw();
156
157protected:
158 std::shared_ptr<Core::Drawable> m_Drawable = nullptr;
159 std::vector<std::shared_ptr<GameObject>> m_Children;
160
161 float m_ZIndex = 0;
162 bool m_Visible = true;
163 glm::vec2 m_Pivot = {0, 0};
164};
165} // namespace Util
166#endif
A class representing a game object.
Definition: GameObject.hpp:20
GameObject(const std::shared_ptr< Core::Drawable > &drawable, const float zIndex, const glm::vec2 &pivot={0, 0}, const bool visible=true, const std::vector< std::shared_ptr< GameObject > > &children=std::vector< std::shared_ptr< GameObject > >())
Parameterized constructor.
Definition: GameObject.hpp:38
float GetZIndex() const
Get the z-index of the game object.
Definition: GameObject.hpp:76
GameObject(GameObject &&other)=default
Default move constructor..
GameObject(const GameObject &other)=default
Copy constructor.
void SetPivot(const glm::vec2 &pivot)
Set the pivot of the game object.
Definition: GameObject.hpp:108
void RemoveChild(const std::shared_ptr< GameObject > &child)
Remove a child from the game object.
Definition: GameObject.hpp:149
void SetDrawable(const std::shared_ptr< Core::Drawable > &drawable)
Set the drawable component of the game object.
Definition: GameObject.hpp:124
const std::vector< std::shared_ptr< GameObject > > & GetChildren() const
Get the children of the game object.
Definition: GameObject.hpp:99
virtual ~GameObject()=default
Default destructor.
glm::vec2 GetScaledSize() const
Get the size of its drawable component.
Definition: GameObject.hpp:90
GameObject()=default
Default constructor.
void SetVisible(const bool visible)
Set the visibility of the game object.
Definition: GameObject.hpp:133
Transform GetTransform() const
Get the transform of the game object.
Definition: GameObject.hpp:83
void AddChild(const std::shared_ptr< GameObject > &child)
Add a child to the game object.
Definition: GameObject.hpp:140
void SetZIndex(float index)
Set the z-index of the game object. z-index is used to determine the order in which game objects are ...
Definition: GameObject.hpp:117
Useful tools for development.
Definition: Animation.hpp:12
A struct representing a transformation in 2D space.
Definition: Transform.hpp:16
glm::vec2 scale
The scale of the transformation.
Definition: Transform.hpp:40