SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
GameObject.h
1#pragma once
2
3#include "spark/core/Component.h"
4#include "spark/core/Export.h"
5#include "spark/core/details/AbstractGameObject.h"
6
7#include "experimental/ser/SerializerScheme.h"
8#include "spark/base/Macros.h"
9#include "spark/rtti/HasRtti.h"
10
11#include <vector>
12
13namespace spark::core::components
14{
15 class Transform;
16}
17
18namespace spark::core
19{
25 class SPARK_CORE_EXPORT GameObject : public rtti::HasRtti, public details::AbstractGameObject<GameObject>
26 {
27 DECLARE_SPARK_RTTI(GameObject)
28 SPARK_ALLOW_PRIVATE_SERIALIZATION
29
30 public:
41 template <typename T = GameObject, typename... Args> requires std::is_base_of_v<GameObject, T>
42 static T* Instantiate(std::string name, GameObject* parent, Args&&... args);
43
49 static void Destroy(GameObject* object, bool immediate = false);
50
57 static GameObject* FindById(GameObject* root, const lib::Uuid& uuid);
58
65 static GameObject* FindByName(GameObject* root, const std::string& name);
66
67 public:
75 explicit GameObject(std::string name, GameObject* parent = nullptr);
76
77 GameObject(const GameObject& other) = delete;
78 GameObject(GameObject&& other) noexcept = default;
79 GameObject& operator=(const GameObject& other) = delete;
80 GameObject& operator=(GameObject&& other) noexcept = default;
81
86 [[nodiscard]] const lib::Uuid& uuid() const;
87
92 [[nodiscard]] const std::string& name() const;
93
98 [[nodiscard]] components::Transform* transform() const;
99
105 void addComponent(Component* component, bool managed = false);
106
113 template <typename T, typename... Args> requires std::is_base_of_v<Component, T>
114 void addComponent(Args&&... args);
115
120 void removeComponent(Component* component);
121
126 template <typename T> requires std::is_base_of_v<Component, T>
127 void removeComponent();
128
134 template <typename T> requires std::is_base_of_v<Component, T>
135 [[nodiscard]] bool hasComponent() const;
136
141 [[nodiscard]] std::vector<Component*> components() const;
142
148 template <typename T> requires std::is_base_of_v<Component, T>
149 [[nodiscard]] std::vector<T*> componentsInChildren() const;
150
156 template <typename T> requires std::is_base_of_v<Component, T>
157 [[nodiscard]] T* component() const;
158
164 template <typename T> requires std::is_base_of_v<Component, T>
165 [[nodiscard]] T* componentInChildren() const;
166
172 template <typename T> requires std::is_base_of_v<Component, T>
173 [[nodiscard]] T* componentInParent() const;
174
178 virtual void onSpawn() {}
179
184 virtual void onUpdate(float dt) { SPARK_UNUSED(dt); }
185
189 virtual void onDestroyed() {}
190
191 public:
195 bool isShown = true;
196
197 private:
198 lib::Uuid m_uuid;
199 std::string m_name;
200 };
201}
202
203IMPLEMENT_SPARK_RTTI(spark::core::GameObject)
204
205#include "spark/core/impl/GameObject.h"
A component that can be attached to a GameObject to provide additional functionality.
Definition Component.h:20
A GameObject is any object in the game. It contains a list of components that provides functionality ...
Definition GameObject.h:26
virtual void onUpdate(float dt)
Method called on every frame.
Definition GameObject.h:184
virtual void onDestroyed()
Method called when the GameObject is destroyed.
Definition GameObject.h:189
virtual void onSpawn()
Method called when the GameObject is spawned in the scene.
Definition GameObject.h:178
A component that stores the position of a game object.
Definition Transform.h:21
A CRTP class to implement a GameObject. It is used to wrap the onSpawn, onUpdate and onDestroyed meth...
Definition AbstractGameObject.h:22
A 128-bit universally unique identifier (UUID).
Definition Uuid.h:22
The class anything using the RTTI should implement.
Definition HasRtti.h:16