Unravel Engine C++ Reference
Loading...
Searching...
No Matches
script_component.h
Go to the documentation of this file.
1#pragma once
2#include <engine/engine_export.h>
3
7
9#include <monort/monort.h>
10
11#include <math/math.h>
12
13namespace unravel
14{
15
21class script_component : public component_crtp<script_component, owned_component>
22{
23public:
24 using scoped_object_ptr = std::shared_ptr<mono::mono_scoped_object>;
25
27 {
28 int active{-1};
32 };
33
35 {
36 script_object() = default;
37 script_object(const mono::mono_object& obj)
38 {
39 scoped = std::make_shared<mono::mono_scoped_object>(obj);
40 state = std::make_shared<script_component::script_object_state>();
41 }
42
43 auto is_marked_for_destroy() const -> bool
44 {
45 if(!state)
46 {
47 return false;
48 }
49 return state->marked_for_destroy;
50 }
51
52 auto is_create_called() const -> bool
53 {
54 if(!state)
55 {
56 return false;
57 }
58 return state->create_called;
59 }
60
61 auto is_start_called() const -> bool
62 {
63 if(!state)
64 {
65 return false;
66 }
67 return state->start_called;
68 }
69
70 auto is_enabled() const -> bool
71 {
72 if(!state)
73 {
74 return false;
75 }
76
77 if(state->active == -1)
78 {
79 return false;
80 }
81 return state->active != 0;
82 }
83
84 auto is_disabled() const -> bool
85 {
86 if(!state)
87 {
88 return false;
89 }
90
91 if(state->active == -1)
92 {
93 return false;
94 }
95 return state->active == 0;
96 }
97
99 std::shared_ptr<script_object_state> state;
100 };
101
102 using script_components_t = std::vector<script_object>;
108 static void on_create_component(entt::registry& r, entt::entity e);
109
115 static void on_destroy_component(entt::registry& r, entt::entity e);
116
121 void process_pending_actions(script_object script_obj);
123
124 auto add_script_component(const mono::mono_type& type) -> script_object;
125 auto add_script_component(const mono::mono_object& obj) -> script_object;
126 auto add_script_component(const script_object& obj, bool process_callbacks = true) -> script_object;
129
130 auto add_native_component(const mono::mono_type& type) -> script_object;
131
132 auto remove_script_component(const mono::mono_object& obj) -> bool;
133 auto remove_script_component(const mono::mono_type& type) -> bool;
134
135 auto remove_native_component(const mono::mono_object& obj) -> bool;
136 auto remove_native_component(const mono::mono_type& type) -> bool;
137
138 auto get_script_components(const mono::mono_type& type) -> std::vector<mono::mono_object>;
139 auto get_script_component(const mono::mono_type& type) -> script_object;
140 auto get_native_component(const mono::mono_type& type) -> script_object;
141
142 auto get_script_components() const -> const script_components_t&;
143 auto has_script_components() const -> bool;
144 auto has_script_components(const std::string& type_name) const -> bool;
145
146 auto get_script_source_location(const script_object& obj) const -> std::string;
147
148 void create();
149 void start();
150 void destroy();
151 void enable();
152 void disable();
153
154 void on_sensor_enter(entt::handle other);
155 void on_sensor_exit(entt::handle other);
156
157 void on_collision_enter(entt::handle other, const std::vector<manifold_point>& manifolds, bool use_b);
158 void on_collision_exit(entt::handle other, const std::vector<manifold_point>& manifolds, bool use_b);
159
160private:
161 void enable(script_object& script_obj, bool check_order);
162 void disable(script_object& script_obj, bool check_order);
163 void create(script_object& script_obj);
164 void start(script_object& script_obj);
165 void destroy(script_object& script_obj);
166 void set_entity(const mono::mono_object& obj, entt::handle e);
167 void on_sensor_enter(const mono::mono_object& obj, entt::handle other);
168 void on_sensor_exit(const mono::mono_object& obj, entt::handle other);
169
170 void on_collision_enter(const mono::mono_object& obj,
171 entt::handle other,
172 const std::vector<manifold_point>& manifolds,
173 bool use_b);
174 void on_collision_exit(const mono::mono_object& obj,
175 entt::handle other,
176 const std::vector<manifold_point>& manifolds,
177 bool use_b);
178
179 template<typename F>
180 auto safe_foreach(script_components_t& components, F&& f)
181 {
182 auto comps = components;
183
184 for(auto& script : comps)
185 {
186 f(script);
187 }
188 }
189
190 script_components_t script_components_;
191 script_components_t script_components_to_create_;
192 script_components_t script_components_to_start_;
193
194 script_components_t native_components_;
195};
196
197} // namespace unravel
manifold_type type
Class that contains core data for audio listeners. There can only be one instance of it per scene.
auto get_script_source_location(const script_object &obj) const -> std::string
void on_collision_enter(entt::handle other, const std::vector< manifold_point > &manifolds, bool use_b)
auto add_native_component(const mono::mono_type &type) -> script_object
void add_missing_script_components(const script_components_t &comps)
auto remove_script_component(const mono::mono_object &obj) -> bool
void on_collision_exit(entt::handle other, const std::vector< manifold_point > &manifolds, bool use_b)
auto get_script_component(const mono::mono_type &type) -> script_object
auto get_native_component(const mono::mono_type &type) -> script_object
void process_pending_actions_create(script_object script_obj)
void add_script_components(const script_components_t &comps)
void on_sensor_enter(entt::handle other)
static void on_destroy_component(entt::registry &r, entt::entity e)
Called when the component is destroyed.
auto get_script_components() const -> const script_components_t &
void on_sensor_exit(entt::handle other)
auto add_script_component(const mono::mono_type &type) -> script_object
auto remove_native_component(const mono::mono_object &obj) -> bool
std::vector< script_object > script_components_t
auto has_script_components() const -> bool
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
std::shared_ptr< mono::mono_scoped_object > scoped_object_ptr
CRTP (Curiously Recurring Template Pattern) base structure for components.
std::shared_ptr< script_object_state > state
script_object(const mono::mono_object &obj)
gfx::uniform_handle handle
Definition uniform.cpp:9