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
10#include <dotnetpp/dotnetpp.h>
11#include <math/math.h>
12
13namespace unravel
14{
15
21class script_component : public component_crtp<script_component, owned_component>
22{
23public:
24
26 {
27 int active{-1};
31 };
32
34 {
35 script_object() = default;
36 script_object(const dotnet::object& obj)
37 {
38 pinned = dotnet::make_object_pinned(obj);
39 state = std::make_shared<script_component::script_object_state>();
40 }
41
42 auto is_marked_for_destroy() const -> bool
43 {
44 if(!state)
45 {
46 return false;
47 }
48 return state->marked_for_destroy;
49 }
50
51 auto is_create_called() const -> bool
52 {
53 if(!state)
54 {
55 return false;
56 }
57 return state->create_called;
58 }
59
60 auto is_start_called() const -> bool
61 {
62 if(!state)
63 {
64 return false;
65 }
66 return state->start_called;
67 }
68
69 auto is_enabled() const -> bool
70 {
71 if(!state)
72 {
73 return false;
74 }
75
76 if(state->active == -1)
77 {
78 return false;
79 }
80 return state->active != 0;
81 }
82
83 auto is_disabled() const -> bool
84 {
85 if(!state)
86 {
87 return false;
88 }
89
90 if(state->active == -1)
91 {
92 return false;
93 }
94 return state->active == 0;
95 }
96
97 dotnet::object_pinned_ptr pinned;
98 std::shared_ptr<script_object_state> state;
99 };
100
101 using script_components_t = std::vector<script_object>;
107 static void on_create_component(entt::registry& r, entt::entity e);
108
114 static void on_destroy_component(entt::registry& r, entt::entity e);
115
120 void process_pending_actions(script_object script_obj);
122
123 auto add_script_component(const dotnet::type& type) -> script_object;
124 auto add_script_component(const dotnet::object& obj) -> script_object;
125 auto add_script_component(const script_object& obj, bool process_callbacks = true) -> script_object;
128
129 auto add_native_component(const dotnet::type& type) -> script_object;
130
131 auto remove_script_component(const dotnet::object& obj) -> bool;
132 auto remove_script_component(const dotnet::type& type) -> bool;
133
134 auto remove_native_component(const dotnet::object& obj) -> bool;
135 auto remove_native_component(const dotnet::type& type) -> bool;
136
137 auto get_script_components(const dotnet::type& type) -> std::vector<dotnet::object>;
138 auto get_script_component(const dotnet::type& type) -> script_object;
139 auto get_native_component(const dotnet::type& type) -> script_object;
140
141 auto get_script_components() const -> const script_components_t&;
142 auto has_script_components() const -> bool;
143 auto has_script_components(const std::string& type_name) const -> bool;
144
145 auto get_script_source_location(const script_object& obj) const -> std::string;
146
147 void create();
148 void start();
149 void destroy();
150 void enable();
151 void disable();
152
153 void on_sensor_enter(entt::handle other, const std::vector<manifold_point>& manifolds);
154 void on_sensor_exit(entt::handle other, const std::vector<manifold_point>& manifolds);
155
156 void on_collision_enter(entt::handle other, const std::vector<manifold_point>& manifolds, bool use_b);
157 void on_collision_exit(entt::handle other, const std::vector<manifold_point>& manifolds, bool use_b);
158
159private:
160 void enable(script_object& script_obj, bool check_order);
161 void disable(script_object& script_obj, bool check_order);
162 void create(script_object& script_obj);
163 void start(script_object& script_obj);
164 void destroy(script_object& script_obj);
165 void set_entity(const dotnet::object& obj, entt::handle e);
166 void on_sensor_enter(const dotnet::object& obj, entt::handle other, const std::vector<dotnetpp_backend::managed_interface::manifold_point>& manifolds);
167 void on_sensor_exit(const dotnet::object& obj, entt::handle other, const std::vector<dotnetpp_backend::managed_interface::manifold_point>& manifolds);
168
169 void on_collision_enter(const dotnet::object& obj,
170 entt::handle other,
171 const std::vector<dotnetpp_backend::managed_interface::manifold_point>& manifolds);
172 void on_collision_exit(const dotnet::object& obj,
173 entt::handle other,
174 const std::vector<dotnetpp_backend::managed_interface::manifold_point>& manifolds);
175
176 template<typename F>
177 auto safe_foreach(script_components_t& components, F&& f)
178 {
179 auto comps = components;
180
181 for(auto& script : comps)
182 {
183 f(script);
184 }
185 }
186
187 script_components_t script_components_;
188 script_components_t script_components_to_create_;
189 script_components_t script_components_to_start_;
190
191 script_components_t native_components_;
192};
193
194} // namespace unravel
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 get_script_component(const dotnet::type &type) -> script_object
void add_missing_script_components(const script_components_t &comps)
void on_collision_exit(entt::handle other, const std::vector< manifold_point > &manifolds, bool use_b)
void on_sensor_enter(entt::handle other, const std::vector< manifold_point > &manifolds)
auto remove_native_component(const dotnet::object &obj) -> bool
auto remove_script_component(const dotnet::object &obj) -> bool
void process_pending_actions_create(script_object script_obj)
void add_script_components(const script_components_t &comps)
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, const std::vector< manifold_point > &manifolds)
std::vector< script_object > script_components_t
auto add_script_component(const dotnet::type &type) -> script_object
auto has_script_components() const -> bool
static void on_create_component(entt::registry &r, entt::entity e)
Called when the component is created.
auto add_native_component(const dotnet::type &type) -> script_object
auto get_native_component(const dotnet::type &type) -> script_object
texture_job_type type
Hash specialization for batch_key to enable use in std::unordered_map.
CRTP (Curiously Recurring Template Pattern) base structure for components.
std::shared_ptr< script_object_state > state
script_object(const dotnet::object &obj)
gfx::uniform_handle handle
Definition uniform.cpp:9