SleakEngine 1.0.0
C++23 multi-backend game engine
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
8namespace 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
77 return owner;
78 }
79
80 protected:
83
84 };
85}
86
87#endif
virtual ~Component()=default
GameObject * owner
Definition Component.hpp:81
virtual void OnEnable()
Definition Component.hpp:73
virtual void LateUpdate(float deltaTime)
Definition Component.hpp:69
virtual void FixedUpdate(float fixedDeltaTime)
Definition Component.hpp:68
Component(GameObject *object)
Definition Component.hpp:61
virtual void OnDisable()
Definition Component.hpp:74
virtual bool Initialize()=0
One-time setup, called after the component is attached and the owner is initialized.
GameObject * GetOwner()
Definition Component.hpp:76
virtual void OnDestroy()
Called when the component is detached or the owner is destroyed.
Definition Component.hpp:72
virtual void Update(float deltaTime)=0
Object(const std::string &name="Object")
Definition Object.hpp:14
Root namespace for everything the engine exposes.
Definition Camera.hpp:10