SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
Sleak::GameObject Class Reference

#include <GameObject.hpp>

Inheritance diagram for Sleak::GameObject:

Public Member Functions

 GameObject (const std::string &name="GameObject")
 ~GameObject () override
void AddChild (GameObject *child)
template<typename T, typename... Args>
void AddComponent (Args &&... args)
 Constructs and attaches a component of type T. Warns and no-ops if one already exists.
virtual void FixedUpdate (float fixedDeltaTime)
const List< GameObject * > & GetChildren () const
template<typename T>
T * GetComponent ()
 Finds the first attached component of type T, or nullptr.
GameObject * GetParent () const
const std::string & GetTag () const
bool HasChildren () const
template<typename T>
bool HasComponent ()
 True if a component of type T is attached.
bool HasParent () const
virtual void Initialize ()
 Initializes the object, components and children; repeats no-op.
bool IsActive () const
bool IsInitialized () const
virtual bool IsLight () const
bool IsOwnedByScene () const
 True once a scene took ownership through SceneBase::AddObject().
bool IsPendingDestroy () const
virtual void LateUpdate (float deltaTime)
void MarkForDestroy ()
 Flags the object for deferred destruction on the next scene pass.
void RemoveChild (GameObject *child)
template<typename T>
void RemoveComponent ()
 Destroys and detaches the first component of type T, if present.
void SetActive (bool active)
 Enables or disables the object, firing OnEnable/OnDisable on its components.
void SetParent (GameObject *parent)
 Reparents this object, updating both the old and new parent's child lists.
void SetTag (const std::string &tag)
virtual void Update (float deltaTime)
Public Member Functions inherited from Sleak::Object
 Object (const std::string &name="Object")
virtual ~Object ()=default
std::string GetDebugName () const
 Name plus unique ID, for logs where names can repeat.
const std::string & GetName () const
uint64_t GetUniqueID () const
void SetName (const std::string &value)

Static Public Member Functions

static GameObject * CreateCapsule (Math::Vector3D position, int segments=16, int rings=8, float height=1, float radius=0.5)
static GameObject * CreateCube (Math::Vector3D position)
static GameObject * CreateCylinder (Math::Vector3D position, int segments=16, float height=1, float radius=0.5)
static GameObject * CreatePlane (Math::Vector3D position, int width=100, int height=100)
 Built-in primitive factories, mainly for prototyping and debug scenes.
static GameObject * CreateSphere (Math::Vector3D position, int stack=16, int slices=16)
static GameObject * CreateTorus (Math::Vector3D position, int segments=16, int rings=8, float innerRadius=8, float outerRadius=9)

Protected Attributes

bool bIsInitialized

Friends

class SceneBase

Detailed Description

Scene entity holding components and an optional parent/child transform hierarchy. Scene owns the instance; destroy via Scene::DestroyObject().

A GameObject on its own does nothing. Behavior comes from the components attached to it: a TransformComponent for placement, a MeshComponent and MaterialComponent to be drawn, a ColliderComponent and RigidbodyComponent to be simulated, and your own Component subclasses for gameplay.

AddComponent<T>() constructs the component in place, forwarding extra arguments to its constructor after the owner pointer. Only one component of a given type is allowed per object: a second AddComponent<T>() logs a warning and does nothing. GetComponent<T>() resolves through dynamic_cast, so it also matches subclasses.

The scene that received the object through SceneBase::AddObject() owns it. Never delete a GameObject. Use SceneBase::DestroyObject() for deferred destruction, which is what you want while iterating, or SceneBase::RemoveObject() for immediate destruction.

A parent owns every child that was not itself added to a scene, so a hierarchy built with SetParent() (for example by ModelLoader) only needs its root added. Deleting a parent deletes those children too.

Camera, Light, and its subclasses all derive from GameObject, so they are added and found the same way as anything else.

Sleak::Math::Vector3D(0.0f, 1.0f, 0.0f));
crate->SetTag("Destructible");
crate->AddComponent<Sleak::MaterialComponent>(
crate->AddComponent<Sleak::RigidbodyComponent>(
if (auto* t = crate->GetComponent<Sleak::TransformComponent>()) {
t->SetScale(Sleak::Math::Vector3D(2.0f, 2.0f, 2.0f));
}
AddObject(crate); // the scene now owns it
static GameObject * CreateCube(Math::Vector3D position)
Represents the position, rotation, and scale of an entity in 3D space.
See also
Component, SceneBase, TransformComponent, MeshComponent, MaterialComponent, Camera

Definition at line 62 of file GameObject.hpp.

Constructor & Destructor Documentation

◆ GameObject()

Sleak::GameObject::GameObject ( const std::string & name = "GameObject")
inline

Definition at line 64 of file GameObject.hpp.

◆ ~GameObject()

Sleak::GameObject::~GameObject ( )
override

Definition at line 14 of file GameObject.cpp.

Member Function Documentation

◆ AddChild()

void Sleak::GameObject::AddChild ( GameObject * child)

Definition at line 135 of file GameObject.cpp.

◆ AddComponent()

template<typename T, typename... Args>
void Sleak::GameObject::AddComponent ( Args &&... args)
inline

Constructs and attaches a component of type T. Warns and no-ops if one already exists.

Definition at line 72 of file GameObject.hpp.

◆ CreateCapsule()

GameObject * Sleak::GameObject::CreateCapsule ( Math::Vector3D position,
int segments = 16,
int rings = 8,
float height = 1,
float radius = 0.5 )
static

Definition at line 206 of file GameObject.cpp.

◆ CreateCube()

GameObject * Sleak::GameObject::CreateCube ( Math::Vector3D position)
static

Definition at line 178 of file GameObject.cpp.

◆ CreateCylinder()

GameObject * Sleak::GameObject::CreateCylinder ( Math::Vector3D position,
int segments = 16,
float height = 1,
float radius = 0.5 )
static

Definition at line 220 of file GameObject.cpp.

◆ CreatePlane()

GameObject * Sleak::GameObject::CreatePlane ( Math::Vector3D position,
int width = 100,
int height = 100 )
static

Built-in primitive factories, mainly for prototyping and debug scenes.

Definition at line 164 of file GameObject.cpp.

◆ CreateSphere()

GameObject * Sleak::GameObject::CreateSphere ( Math::Vector3D position,
int stack = 16,
int slices = 16 )
static

Definition at line 192 of file GameObject.cpp.

◆ CreateTorus()

GameObject * Sleak::GameObject::CreateTorus ( Math::Vector3D position,
int segments = 16,
int rings = 8,
float innerRadius = 8,
float outerRadius = 9 )
static

◆ FixedUpdate()

void Sleak::GameObject::FixedUpdate ( float fixedDeltaTime)
virtual

Definition at line 64 of file GameObject.cpp.

◆ GetChildren()

const List< GameObject * > & Sleak::GameObject::GetChildren ( ) const
inline

Definition at line 141 of file GameObject.hpp.

◆ GetComponent()

template<typename T>
T * Sleak::GameObject::GetComponent ( )
inline

Finds the first attached component of type T, or nullptr.

Definition at line 101 of file GameObject.hpp.

◆ GetParent()

GameObject * Sleak::GameObject::GetParent ( ) const
inline

Definition at line 140 of file GameObject.hpp.

◆ GetTag()

const std::string & Sleak::GameObject::GetTag ( ) const
inline

Definition at line 136 of file GameObject.hpp.

◆ HasChildren()

bool Sleak::GameObject::HasChildren ( ) const
inline

Definition at line 145 of file GameObject.hpp.

◆ HasComponent()

template<typename T>
bool Sleak::GameObject::HasComponent ( )
inline

True if a component of type T is attached.

Definition at line 118 of file GameObject.hpp.

◆ HasParent()

bool Sleak::GameObject::HasParent ( ) const
inline

Definition at line 144 of file GameObject.hpp.

◆ Initialize()

void Sleak::GameObject::Initialize ( )
virtual

Initializes the object, components and children; repeats no-op.

Reimplemented in Sleak::Camera.

Definition at line 31 of file GameObject.cpp.

◆ IsActive()

bool Sleak::GameObject::IsActive ( ) const
inline

Definition at line 133 of file GameObject.hpp.

◆ IsInitialized()

bool Sleak::GameObject::IsInitialized ( ) const
inline

Definition at line 126 of file GameObject.hpp.

◆ IsLight()

virtual bool Sleak::GameObject::IsLight ( ) const
inlinevirtual

Reimplemented in Sleak::Light.

Definition at line 149 of file GameObject.hpp.

◆ IsOwnedByScene()

bool Sleak::GameObject::IsOwnedByScene ( ) const
inline

True once a scene took ownership through SceneBase::AddObject().

Definition at line 147 of file GameObject.hpp.

◆ IsPendingDestroy()

bool Sleak::GameObject::IsPendingDestroy ( ) const
inline

Definition at line 153 of file GameObject.hpp.

◆ LateUpdate()

void Sleak::GameObject::LateUpdate ( float deltaTime)
virtual

Definition at line 78 of file GameObject.cpp.

◆ MarkForDestroy()

void Sleak::GameObject::MarkForDestroy ( )
inline

Flags the object for deferred destruction on the next scene pass.

Definition at line 152 of file GameObject.hpp.

◆ RemoveChild()

void Sleak::GameObject::RemoveChild ( GameObject * child)

Definition at line 140 of file GameObject.cpp.

◆ RemoveComponent()

template<typename T>
void Sleak::GameObject::RemoveComponent ( )
inline

Destroys and detaches the first component of type T, if present.

Definition at line 87 of file GameObject.hpp.

◆ SetActive()

void Sleak::GameObject::SetActive ( bool active)

Enables or disables the object, firing OnEnable/OnDisable on its components.

Definition at line 92 of file GameObject.cpp.

◆ SetParent()

void Sleak::GameObject::SetParent ( GameObject * parent)

Reparents this object, updating both the old and new parent's child lists.

Definition at line 111 of file GameObject.cpp.

◆ SetTag()

void Sleak::GameObject::SetTag ( const std::string & tag)
inline

Definition at line 135 of file GameObject.hpp.

◆ Update()

void Sleak::GameObject::Update ( float deltaTime)
virtual

Reimplemented in Sleak::Camera.

Definition at line 50 of file GameObject.cpp.

◆ SceneBase

friend class SceneBase
friend

Definition at line 183 of file GameObject.hpp.

Member Data Documentation

◆ bIsInitialized

bool Sleak::GameObject::bIsInitialized
protected

Definition at line 164 of file GameObject.hpp.