Capycore Engine  0.1.0
A simple and lightweight game engine written in C++ based on the Unity API architecture.
Loading...
Searching...
No Matches
component.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
3#include <functional>
4#include <memory>
5#include <optional>
6#include <string>
7#include <typeinfo>
8#include <vector>
9
10class GameObject;
11
24class Component {
25 private:
26 std::optional<std::reference_wrapper<GameObject>> parent_;
27 bool active_{true};
28 bool marked_for_deletion_{false};
29
30 std::vector<std::function<void(Component&)>> on_detach_actions_;
31 std::vector<std::function<void(Component&)>> on_attach_actions_;
32
33 public:
34 explicit Component();
35 virtual ~Component() = default;
36
41 [[nodiscard]] bool active() const noexcept;
42 Component& active(bool value) noexcept;
43
48 [[nodiscard]] bool marked_for_deletion() const noexcept;
50
51 virtual void update(float dt) = 0;
52
54 virtual void on_attach();
56 virtual void on_detach();
57
64 virtual void on_serialize(std::vector<uint8_t>& /*out*/) const;
65
73 virtual void on_deserialize(const std::vector<uint8_t>& /*data*/,
74 size_t& /*offset*/);
75
82 virtual std::string type_name() const = 0;
83
88 const std::optional<std::reference_wrapper<GameObject>>& parent()
89 const noexcept; // NOLINT
90
96 std::optional<std::reference_wrapper<GameObject>>&
97 parent() noexcept; // NOLINT
98
100 Component& parent(std::nullopt_t nullopt);
101
108 size_t add_on_attach(const std::function<void(Component&)>& action);
109 void remove_on_attach(size_t index);
110
117 size_t add_on_detach(const std::function<void(Component&)>& action);
118 void remove_on_detach(size_t index);
119};
120
121template <typename T>
122concept IsComponent = std::is_base_of_v<Component, T>;
Base class for all components that can be attached to GameObjects.
Definition component.h:24
virtual void on_deserialize(const std::vector< uint8_t > &, size_t &)
Deserializes the component's state from a byte array.
const std::optional< std::reference_wrapper< GameObject > > & parent() const noexcept
Gets the parent GameObject of this component.
void remove_on_attach(size_t index)
virtual std::string type_name() const =0
Provides a consistent type name for the component.
virtual void on_serialize(std::vector< uint8_t > &) const
Serializes the component's state into a byte array.
size_t add_on_detach(const std::function< void(Component &)> &action)
Adds an action to be performed when the component is attached.
virtual void on_detach()
Called when the component is detached from a GameObject.
bool marked_for_deletion() const noexcept
Activates the component.
void remove_on_detach(size_t index)
size_t add_on_attach(const std::function< void(Component &)> &action)
Retrieves the type information of the component.
Component & mark_for_deletion() noexcept
virtual void update(float dt)=0
bool active() const noexcept
Checks if the component is active.
virtual void on_attach()
Called when the component is attached to a GameObject.
virtual ~Component()=default
Concept to constrain types to be derived from Component.
Definition gameObject.h:33
Definition component.h:122