16 for (
size_t i = 0; i < children.
GetSize(); ++i) {
19 child->m_parent =
nullptr;
20 if (!child->m_ownedByScene)
delete child;
26 m_parent->RemoveChild(
this);
35 for (
size_t i = 0; i < Components.GetSize(); ++i) {
36 if (Components[i]) InitializeComponent(Components[i].get());
39 for (
size_t i = 0; i < m_children.GetSize(); ++i) {
40 if (m_children[i]) m_children[i]->Initialize();
44 void GameObject::InitializeComponent(
Component* component) {
47 if (m_isActive) component->
OnEnable();
51 if (!m_isActive || m_pendingDestroy)
return;
53 for (
size_t i = 0; i < Components.GetSize(); ++i) {
55 Components[i]->Update(deltaTime);
58 for (
size_t i = 0; i < m_children.GetSize(); ++i) {
59 if (m_children[i] && m_children[i]->
IsActive())
60 m_children[i]->Update(deltaTime);
65 if (!m_isActive || m_pendingDestroy)
return;
67 for (
size_t i = 0; i < Components.GetSize(); ++i) {
69 Components[i]->FixedUpdate(fixedDeltaTime);
72 for (
size_t i = 0; i < m_children.GetSize(); ++i) {
73 if (m_children[i] && m_children[i]->
IsActive())
74 m_children[i]->FixedUpdate(fixedDeltaTime);
79 if (!m_isActive || m_pendingDestroy)
return;
81 for (
size_t i = 0; i < Components.GetSize(); ++i) {
83 Components[i]->LateUpdate(deltaTime);
86 for (
size_t i = 0; i < m_children.GetSize(); ++i) {
87 if (m_children[i] && m_children[i]->
IsActive())
88 m_children[i]->LateUpdate(deltaTime);
93 if (m_isActive == active)
return;
96 for (
size_t i = 0; i < Components.GetSize(); ++i) {
99 Components[i]->OnEnable();
101 Components[i]->OnDisable();
105 for (
size_t i = 0; i < m_children.GetSize(); ++i) {
107 m_children[i]->SetActive(active);
112 if (m_parent == parent)
return;
114 for (
GameObject* p = parent; p; p = p->m_parent) {
116 SLEAK_WARN(
"Cannot parent '{}' to its own descendant",
123 m_parent->RemoveChild(
this);
129 if (m_parent->m_children.indexOf(
this) == -1) {
130 m_parent->m_children.add(
this);
136 if (!child || child ==
this)
return;
142 int index = m_children.indexOf(child);
144 m_children.erase(index);
145 child->m_parent =
nullptr;
149 void GameObject::DestroyComponents() {
150 for (
size_t i = 0; i < Components.GetSize(); ++i) {
152 Components[i]->OnDestroy();
160 mat->SetShader(
"assets/shaders/default_shader.hlsl");
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.
virtual bool Initialize()=0
One-time setup, called after the component is attached and the owner is initialized.
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, components and children; repeats no-op.
static GameObject * CreateCapsule(Math::Vector3D position, int segments=16, int rings=8, float height=1, float radius=0.5)
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)
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.
Implements a dynamic array-like list for storing and managing a collection of elements.
virtual bool Initialize() override
Validates that vertex and index buffers were created successfully.
const std::string & GetName() const
Root namespace for everything the engine exposes.
static Material * CreateDefaultMaterial()
Fallback material for the primitive factories below, using the default shader.