SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Component.hpp
Go to the documentation of this file.
1
#ifndef _COMPONENT_H_
2
#define _COMPONENT_H_
3
4
#include <
Core/Object.hpp
>
5
#include <
Memory/RefPtr.hpp
>
6
#include <
Core/Logger.hpp
>
7
8
namespace
Sleak
{
9
class
GameObject
;
10
/// Base for behavior attached to a GameObject. Owner deletes it via
11
/// GameObject::RemoveComponent(), never directly.
12
///
13
/// Derive from Component to add your own behavior to an object. The
14
/// constructor must take the owning `GameObject*` as its first
15
/// parameter, because GameObject::AddComponent() supplies it and
16
/// forwards the rest. Initialize() and Update() are pure virtual;
17
/// FixedUpdate(), LateUpdate(), OnEnable(), OnDisable(), and
18
/// OnDestroy() are optional.
19
///
20
/// The owning GameObject holds components in a RefPtr and destroys them
21
/// with itself, so never delete a component directly. Reach the owner
22
/// through GetOwner() to find sibling components.
23
///
24
/// Lifecycle: the object calls Initialize() once (immediately if the
25
/// object is already initialized when the component is attached, and
26
/// otherwise during the object's own Initialize()), then OnEnable() if
27
/// the object is active, then Update() every frame.
28
///
29
/// @code{.cpp}
30
/// class SpinComponent : public Sleak::Component {
31
/// public:
32
/// SpinComponent(Sleak::GameObject* owner, float degreesPerSecond)
33
/// : Sleak::Component(owner), m_speed(degreesPerSecond) {}
34
///
35
/// bool Initialize() override {
36
/// m_transform = GetOwner()
37
/// ->GetComponent<Sleak::TransformComponent>();
38
/// return m_transform != nullptr;
39
/// }
40
///
41
/// void Update(float deltaTime) override {
42
/// if (!m_transform) return;
43
/// m_transform->RotateAround(
44
/// Sleak::Math::Vector3D(0.0f, 1.0f, 0.0f),
45
/// m_speed * deltaTime);
46
/// }
47
///
48
/// private:
49
/// float m_speed;
50
/// Sleak::TransformComponent* m_transform = nullptr;
51
/// };
52
///
53
/// // Attach it; 90.0f is forwarded to the constructor
54
/// obj->AddComponent<SpinComponent>(90.0f);
55
/// @endcode
56
///
57
/// @see GameObject, TransformComponent, CameraController
58
/// @ingroup scene
59
class
Component
:
public
Object
{
60
public
:
61
Component
(
GameObject
*
object
) :
bIsInitialized
(false),
owner
(object) {}
62
virtual
~Component
() =
default
;
63
64
/// One-time setup, called after the component is attached and the owner is initialized.
65
virtual
bool
Initialize
() = 0;
66
67
virtual
void
Update
(
float
deltaTime) = 0;
68
virtual
void
FixedUpdate
(
float
fixedDeltaTime) {}
69
virtual
void
LateUpdate
(
float
deltaTime) {}
70
71
/// Called when the component is detached or the owner is destroyed.
72
virtual
void
OnDestroy
() {}
73
virtual
void
OnEnable
() {}
74
virtual
void
OnDisable
() {}
75
76
GameObject
*
GetOwner
() {
77
return
owner
;
78
}
79
80
/// True once Initialize() has run and succeeded.
81
bool
IsInitialized
()
const
{
return
bIsInitialized
; }
82
83
protected
:
84
GameObject
*
owner
;
85
bool
bIsInitialized
;
86
87
private
:
88
friend
class
GameObject
;
89
};
90
}
91
92
#endif
Logger.hpp
Object.hpp
RefPtr.hpp
Sleak::Component::GameObject
friend class GameObject
Definition
Component.hpp:88
Sleak::Component::bIsInitialized
bool bIsInitialized
Definition
Component.hpp:85
Sleak::Component::~Component
virtual ~Component()=default
Sleak::Component::owner
GameObject * owner
Definition
Component.hpp:84
Sleak::Component::OnEnable
virtual void OnEnable()
Definition
Component.hpp:73
Sleak::Component::IsInitialized
bool IsInitialized() const
True once Initialize() has run and succeeded.
Definition
Component.hpp:81
Sleak::Component::LateUpdate
virtual void LateUpdate(float deltaTime)
Definition
Component.hpp:69
Sleak::Component::FixedUpdate
virtual void FixedUpdate(float fixedDeltaTime)
Definition
Component.hpp:68
Sleak::Component::Component
Component(GameObject *object)
Definition
Component.hpp:61
Sleak::Component::OnDisable
virtual void OnDisable()
Definition
Component.hpp:74
Sleak::Component::Initialize
virtual bool Initialize()=0
One-time setup, called after the component is attached and the owner is initialized.
Sleak::Component::GetOwner
GameObject * GetOwner()
Definition
Component.hpp:76
Sleak::Component::OnDestroy
virtual void OnDestroy()
Called when the component is detached or the owner is destroyed.
Definition
Component.hpp:72
Sleak::Component::Update
virtual void Update(float deltaTime)=0
Sleak::GameObject
Definition
GameObject.hpp:62
Sleak::Object::Object
Object(const std::string &name="Object")
Definition
Object.hpp:14
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
ECS
Component.hpp
Generated on
for SleakEngine by
1.18.0