SleakEngine 1.0.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
GameObject.cpp
Go to the documentation of this file.
1#include <Core/GameObject.hpp>
4#include <Math/Vector.hpp>
10#include <Runtime/MeshData.hpp>
11
12namespace Sleak {
13
15 DestroyComponents();
16
17 if (m_parent) {
18 m_parent->RemoveChild(this);
19 m_parent = nullptr;
20 }
21
22 for (size_t i = 0; i < m_children.GetSize(); ++i) {
23 if (m_children[i]) {
24 m_children[i]->m_parent = nullptr;
25 }
26 }
27 m_children.clear();
28 }
29
31 for (size_t i = 0; i < Components.GetSize(); ++i) {
32 if (Components[i])
33 Components[i]->Initialize();
34 }
35 bIsInitialized = true;
36 }
37
38 void GameObject::Update(float deltaTime) {
39 if (!m_isActive || m_pendingDestroy) return;
40
41 for (size_t i = 0; i < Components.GetSize(); ++i) {
42 if (Components[i])
43 Components[i]->Update(deltaTime);
44 }
45
46 for (size_t i = 0; i < m_children.GetSize(); ++i) {
47 if (m_children[i] && m_children[i]->IsActive())
48 m_children[i]->Update(deltaTime);
49 }
50 }
51
52 void GameObject::FixedUpdate(float fixedDeltaTime) {
53 if (!m_isActive || m_pendingDestroy) return;
54
55 for (size_t i = 0; i < Components.GetSize(); ++i) {
56 if (Components[i])
57 Components[i]->FixedUpdate(fixedDeltaTime);
58 }
59
60 for (size_t i = 0; i < m_children.GetSize(); ++i) {
61 if (m_children[i] && m_children[i]->IsActive())
62 m_children[i]->FixedUpdate(fixedDeltaTime);
63 }
64 }
65
66 void GameObject::LateUpdate(float deltaTime) {
67 if (!m_isActive || m_pendingDestroy) return;
68
69 for (size_t i = 0; i < Components.GetSize(); ++i) {
70 if (Components[i])
71 Components[i]->LateUpdate(deltaTime);
72 }
73
74 for (size_t i = 0; i < m_children.GetSize(); ++i) {
75 if (m_children[i] && m_children[i]->IsActive())
76 m_children[i]->LateUpdate(deltaTime);
77 }
78 }
79
80 void GameObject::SetActive(bool active) {
81 if (m_isActive == active) return;
82 m_isActive = active;
83
84 for (size_t i = 0; i < Components.GetSize(); ++i) {
85 if (Components[i]) {
86 if (active)
87 Components[i]->OnEnable();
88 else
89 Components[i]->OnDisable();
90 }
91 }
92
93 for (size_t i = 0; i < m_children.GetSize(); ++i) {
94 if (m_children[i])
95 m_children[i]->SetActive(active);
96 }
97 }
98
100 if (m_parent == parent) return;
101
102 if (m_parent) {
103 m_parent->RemoveChild(this);
104 }
105
106 m_parent = parent;
107
108 if (m_parent) {
109 if (m_parent->m_children.indexOf(this) == -1) {
110 m_parent->m_children.add(this);
111 }
112 }
113 }
114
116 if (!child || child == this) return;
117 child->SetParent(this);
118 }
119
121 if (!child) return;
122 int index = m_children.indexOf(child);
123 if (index != -1) {
124 m_children.erase(index);
125 child->m_parent = nullptr;
126 }
127 }
128
129 void GameObject::DestroyComponents() {
130 for (size_t i = 0; i < Components.GetSize(); ++i) {
131 if (Components[i])
132 Components[i]->OnDestroy();
133 }
134 Components.clear();
135 }
136
137 /// Fallback material for the primitive factories below, using the default shader.
139 auto* mat = new Material();
140 mat->SetShader("assets/shaders/default_shader.hlsl");
141 return mat;
142 }
143
145 GameObject* object = new GameObject("Plane");
146
147 MeshData meshData = GetPlaneMesh(width, height);
148 object->AddComponent<ColliderComponent>(meshData, Physics::ColliderType::AABB);
149 object->AddComponent<RigidbodyComponent>(BodyType::Static);
150 object->AddComponent<MaterialComponent>(CreateDefaultMaterial());
151 object->AddComponent<MeshComponent>(std::move(meshData));
152 object->AddComponent<TransformComponent>(position);
153 object->Initialize();
154
155 return object;
156 }
157
159 GameObject* object = new GameObject("Cube");
160
161 MeshData meshData = GetCubeMesh();
162 object->AddComponent<ColliderComponent>(meshData, Physics::ColliderType::AABB);
163 object->AddComponent<RigidbodyComponent>(BodyType::Static);
164 object->AddComponent<TransformComponent>(position);
165 object->AddComponent<MaterialComponent>(CreateDefaultMaterial());
166 object->AddComponent<MeshComponent>(std::move(meshData));
167 object->Initialize();
168
169 return object;
170 }
171
172 GameObject* GameObject::CreateSphere(Math::Vector3D position, int stack, int slices) {
173 GameObject* object = new GameObject("Sphere");
174
175 MeshData meshData = GetSphereMesh(stack, slices);
176 object->AddComponent<ColliderComponent>(meshData, Physics::ColliderType::Sphere);
177 object->AddComponent<RigidbodyComponent>(BodyType::Static);
178 object->AddComponent<TransformComponent>(position);
179 object->AddComponent<MaterialComponent>(CreateDefaultMaterial());
180 object->AddComponent<MeshComponent>(std::move(meshData));
181 object->Initialize();
182
183 return object;
184 }
185
186 GameObject* GameObject::CreateCapsule(Math::Vector3D position, int segments, int rings, float height, float radius) {
187 GameObject* object = new GameObject("Capsule");
188
189 MeshData meshData = GetCapsuleMesh(segments, rings, height, radius);
190 object->AddComponent<ColliderComponent>(meshData, Physics::ColliderType::Capsule);
191 object->AddComponent<RigidbodyComponent>(BodyType::Static);
192 object->AddComponent<TransformComponent>(position);
193 object->AddComponent<MaterialComponent>(CreateDefaultMaterial());
194 object->AddComponent<MeshComponent>(std::move(meshData));
195 object->Initialize();
196
197 return object;
198 }
199
200 GameObject* GameObject::CreateCylinder(Math::Vector3D position, int segments, float height, float radius) {
201 GameObject* object = new GameObject("Cylinder");
202
203 MeshData meshData = GetCylinderMesh(segments, height, radius);
204 object->AddComponent<ColliderComponent>(meshData, Physics::ColliderType::AABB);
205 object->AddComponent<RigidbodyComponent>(BodyType::Static);
206 object->AddComponent<MaterialComponent>(CreateDefaultMaterial());
207 object->AddComponent<MeshComponent>(std::move(meshData));
208 object->AddComponent<TransformComponent>(position);
209 object->Initialize();
210
211 return object;
212 }
213}
int width
int height
Sleak::MeshData GetPlaneMesh(float width, float height, int subdivisionsX=1, int subdivisionsY=1)
Builds a subdivided flat quad on the XZ plane, facing up.
Sleak::MeshData GetCapsuleMesh(int segments, int rings, float height, float radius)
Builds a capsule (cylinder with hemispherical caps) centered at the origin.
Sleak::MeshData GetCylinderMesh(int segments, float height, float radius)
Builds a capped cylinder centered at the origin, extending +/- height/2 along Y.
Sleak::MeshData GetSphereMesh(int stacks=16, int slices=16)
Builds a UV sphere of unit radius with the given stack/slice resolution.
Sleak::MeshData GetCubeMesh()
Builds a unit cube with per-face vertices for hard-edged normals.
static GameObject * CreateCylinder(Math::Vector3D position, int segments=16, float height=1, float radius=0.5)
virtual void LateUpdate(float deltaTime)
virtual void Initialize()
Initializes the object and its components; called once before the first Update.
static GameObject * CreateCapsule(Math::Vector3D position, int segments=16, int rings=8, float height=1, float radius=0.5)
bool IsActive() const
virtual void Update(float deltaTime)
void RemoveChild(GameObject *child)
virtual void FixedUpdate(float fixedDeltaTime)
GameObject(const std::string &name="GameObject")
void SetActive(bool active)
Enables or disables the object, firing OnEnable/OnDisable on its components.
static GameObject * CreatePlane(Math::Vector3D position, int width=100, int height=100)
Built-in primitive factories, mainly for prototyping and debug scenes.
static GameObject * CreateCube(Math::Vector3D position)
void AddChild(GameObject *child)
~GameObject() override
static GameObject * CreateSphere(Math::Vector3D position, int stack=16, int slices=16)
void SetParent(GameObject *parent)
Reparents this object, updating both the old and new parent's child lists.
virtual bool Initialize() override
Validates that vertex and index buffers were created successfully.
Represents the position, rotation, and scale of an entity in 3D space.
bool Initialize() override
Allocates the transform's GPU constant buffer.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10
static Material * CreateDefaultMaterial()
Fallback material for the primitive factories below, using the default shader.