Unravel Engine C++ Reference
Loading...
Searching...
No Matches
basic_component.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
4#include "../ecs.h"
6
7namespace unravel
8{
9
15{
17 bool eto{};
18
22 void touch()
23 {
24 }
25};
26
32{
33public:
34
35 template<typename T>
36 static void on_create_component(entt::registry& r, entt::entity e)
37 {
38 entt::handle entity(r, e);
39
40 auto& component = entity.get<T>();
41 component.set_owner(entity);
42 }
43
44 template<typename T>
45 static void on_destroy_component(entt::registry& r, entt::entity e)
46 {
47 }
48
49 auto operator=(const owned_component& other) -> owned_component& = default;
50
55 void set_owner(entt::handle owner)
56 {
57 owner_ = owner;
58 }
59
64 [[nodiscard]] auto get_owner() const noexcept -> entt::const_handle
65 {
66 return owner_;
67 }
68
73 [[nodiscard]] auto get_owner() noexcept -> entt::handle
74 {
75 return owner_;
76 }
77
78private:
80 entt::handle owner_{};
81};
82
89template<typename T, typename Base = basic_component>
90struct component_crtp : Base
91{
93 static constexpr bool in_place_delete = true;
94
95 using base = Base;
96};
97
98
99template<typename T>
101{
102 static auto exists(entt::handle entity) -> bool
103 {
104 return entity.all_of<T>();
105 }
106
107 static auto add(entt::handle entity) -> bool
108 {
109 bool has_component = entity.all_of<T>();
110 if(has_component)
111 {
112 return false;
113 }
114 entity.emplace<T>();
115 return true;
116 }
117
118 static auto remove(entt::handle entity) -> bool
119 {
120 size_t removed = entity.remove<T>();
121 return removed > 0;
122 }
123
124 static auto save(entt::handle entity, std::stringstream& stream) -> bool
125 {
126 try
127 {
128 auto ar = ser20::create_oarchive_associative(stream);
129 ar(ser20::make_nvp("component", entity.get<T>()));
130 return true;
131 }
132 catch(const ser20::Exception& e)
133 {
134 APPLOG_ERROR("Failed to save component to stream: {}", e.what());
135 return false;
136 }
137 }
138
139 static auto load(entt::handle entity, std::stringstream& stream) -> bool
140 {
141 try
142 {
143 auto ar = ser20::create_iarchive_associative(stream);
144 ar(ser20::make_nvp("component", entity.get<T>()));
145 return true;
146 }
147 catch(const ser20::Exception& e)
148 {
149 APPLOG_ERROR("Failed to load component from stream: {}", e.what());
150 return false;
151 }
152 return false;
153 }
154};
155
156} // namespace unravel
Component that is owned by an entity.
auto operator=(const owned_component &other) -> owned_component &=default
static void on_destroy_component(entt::registry &r, entt::entity e)
static void on_create_component(entt::registry &r, entt::entity e)
auto get_owner() const noexcept -> entt::const_handle
Gets the owner of the component.
void set_owner(entt::handle owner)
Sets the owner of the component.
auto get_owner() noexcept -> entt::handle
Gets the owner of the component.
#define APPLOG_ERROR(...)
Definition logging.h:20
auto create_oarchive_associative(std::ostream &stream)
auto create_iarchive_associative(std::istream &stream)
entt::entity entity
Basic component structure that other components can inherit from.
bool eto
Disable empty type optimizations.
void touch()
Marks the component as 'touched'.
CRTP (Curiously Recurring Template Pattern) base structure for components.
static constexpr bool in_place_delete
Indicates if the component can be deleted in place.
static auto add(entt::handle entity) -> bool
static auto remove(entt::handle entity) -> bool
static auto save(entt::handle entity, std::stringstream &stream) -> bool
static auto exists(entt::handle entity) -> bool
static auto load(entt::handle entity, std::stringstream &stream) -> bool
gfx::uniform_handle handle
Definition uniform.cpp:9