2#include <monopp/mono_property.h>
3#include <monopp/mono_property_invoker.h>
20struct managed_contact_point
55 safe_foreach(script_components_,
58 auto& obj =
script.scoped->object;
67 safe_foreach(script_components_,
70 auto& obj =
script.scoped->object;
77 safe_foreach(script_components_,
80 auto& obj =
script.scoped->object;
87 safe_foreach(script_components_,
90 auto& obj =
script.scoped->object;
97 safe_foreach(script_components_,
100 auto& obj =
script.scoped->object;
107 safe_foreach(script_components_,
110 auto& obj =
script.scoped->object;
117 safe_foreach(script_components_,
120 auto& obj =
script.scoped->object;
127 if(script_obj.is_enabled() || script_obj.is_marked_for_destroy())
132 script_obj.state->active =
true;
136 if(!script_obj.is_create_called())
142 auto& obj = script_obj.scoped->object;
144 auto method = mono::make_method_invoker<void()>(obj,
"internal_n2m_on_enable");
150 if(script_obj.is_disabled() || script_obj.is_marked_for_destroy())
155 script_obj.state->active = 0;
159 if(!script_obj.is_create_called())
164 auto& obj = script_obj.scoped->object;
168 auto method = mono::make_method_invoker<void()>(obj,
"internal_n2m_on_disable");
171 catch(
const mono::mono_exception& e)
179 if(script_obj.is_create_called())
184 script_obj.state->create_called =
true;
185 auto& obj = script_obj.scoped->object;
189 auto method = mono::make_method_invoker<void()>(obj,
"internal_n2m_on_create");
192 catch(
const mono::mono_exception& e)
199 if(script_obj.is_start_called())
203 script_obj.state->start_called =
true;
204 auto& obj = script_obj.scoped->object;
208 auto method = mono::make_method_invoker<void()>(obj,
"internal_n2m_on_start");
211 catch(
const mono::mono_exception& e)
219 auto& obj = script_obj.scoped->object;
222 auto method = mono::make_method_invoker<void()>(obj,
"internal_n2m_on_destroy");
225 catch(
const mono::mono_exception& e)
231void script_component::set_entity(
const mono::mono_object& obj, entt::handle e)
235 auto method = mono::make_method_invoker<void(entt::entity)>(obj,
"internal_n2m_set_entity");
236 method(obj,
e.entity());
238 catch(
const mono::mono_exception& e)
248 auto method = mono::make_method_invoker<void(entt::entity)>(obj,
"internal_n2m_on_sensor_enter");
249 method(obj, other.entity());
251 catch(
const mono::mono_exception& e)
261 auto method = mono::make_method_invoker<void(entt::entity)>(obj,
"internal_n2m_on_sensor_exit");
262 method(obj, other.entity());
264 catch(
const mono::mono_exception& e)
272 const std::vector<manifold_point>& manifolds,
275 std::vector<managed_contact_point> points;
276 points.reserve(manifolds.size());
277 for(
const auto& manifold : manifolds)
279 auto&
point = points.emplace_back();
282 point.
point = {manifold.b.x, manifold.b.y, manifold.b.z};
283 point.normal = {manifold.normal_on_b.x, manifold.normal_on_b.y, manifold.normal_on_b.z};
287 point.
point = {manifold.a.x, manifold.a.y, manifold.a.z};
288 point.normal = {manifold.normal_on_a.x, manifold.normal_on_a.y, manifold.normal_on_a.z};
290 point.distance = manifold.distance;
291 point.impulse = manifold.impulse;
296 auto method = mono::make_method_invoker<void(entt::entity,
const std::vector<managed_contact_point>&)>(
298 "internal_n2m_on_collision_enter");
299 method(obj,
b.entity(), points);
301 catch(
const mono::mono_exception& e)
309 const std::vector<manifold_point>& manifolds,
312 std::vector<managed_contact_point> points;
313 points.reserve(manifolds.size());
314 for(
const auto& manifold : manifolds)
316 auto&
point = points.emplace_back();
319 point.
point = {manifold.b.x, manifold.b.y, manifold.b.z};
320 point.normal = {manifold.normal_on_b.x, manifold.normal_on_b.y, manifold.normal_on_b.z};
324 point.
point = {manifold.a.x, manifold.a.y, manifold.a.z};
325 point.normal = {manifold.normal_on_a.x, manifold.normal_on_a.y, manifold.normal_on_a.z};
327 point.distance = manifold.distance;
328 point.impulse = manifold.impulse;
333 auto method = mono::make_method_invoker<void(entt::entity,
const std::vector<managed_contact_point>&)>(
335 "internal_n2m_on_collision_exit");
336 method(obj,
b.entity(), points);
338 catch(
const mono::mono_exception& e)
347 auto& ev = ctx.get_cached<
events>();
349 size_t erased = std::erase_if(script_components_,
352 bool marked = rhs.is_marked_for_destroy();
353 if(marked && ev.is_playing)
361 erased += std::erase_if(native_components_,
364 return rhs.is_marked_for_destroy();
370 while(!script_components_to_create_.empty())
372 auto comps = std::move(script_components_to_create_);
373 script_components_to_create_.clear();
377 if(!
script.is_marked_for_destroy())
387 while(!script_components_to_start_.empty())
389 auto comps = std::move(script_components_to_start_);
390 script_components_to_start_.clear();
394 if(!
script.is_marked_for_destroy())
411 auto& ev = ctx.get_cached<
events>();
417 if(
get_owner().all_of<active_component>())
419 enable(script_obj,
false);
431 auto obj =
type.new_instance();
432 return add_script_component(obj);
438 return add_script_component(script_obj);
443 script_components_.emplace_back(script_obj);
444 script_components_to_create_.emplace_back(script_obj);
445 script_components_to_start_.emplace_back(script_obj);
447 auto& obj = script_obj.scoped->object;
449 set_entity(obj, get_owner());
451 if(process_callbacks)
453 process_pending_actions(script_obj);
461 for(
auto& comp : comps)
472 for(
auto& comp : comps)
476 auto& obj = comp.scoped->object;
477 const auto&
type = obj.get_type();
490 auto obj =
type.new_instance();
491 auto& script_obj = native_components_.emplace_back(obj);
493 set_entity(obj, get_owner());
500 std::vector<mono::mono_object> result;
501 for(
const auto& component : script_components_)
503 const auto& comp_type = component.scoped->object.get_type();
505 if(comp_type.get_internal_ptr() ==
type.get_internal_ptr() || comp_type.is_derived_from(
type))
507 result.emplace_back(
static_cast<mono::mono_object&
>(component.scoped->object));
516 auto it = std::find_if(std::begin(script_components_),
517 std::end(script_components_),
518 [&](
const auto& component)
520 const auto& comp_type = component.scoped->object.get_type();
521 return comp_type.get_internal_ptr() ==
type.get_internal_ptr() ||
522 comp_type.is_derived_from(
type);
525 if(it != std::end(script_components_))
535 auto it = std::find_if(std::begin(native_components_),
536 std::end(native_components_),
537 [&](
const auto& component)
539 const auto& comp_type = component.scoped->object.get_type();
540 return comp_type.get_internal_ptr() ==
type.get_internal_ptr() ||
541 comp_type.is_derived_from(
type);
544 if(it != std::end(native_components_))
554 auto checker = [&](
const auto& rhs)
556 return rhs.scoped->object.get_internal_ptr() == obj.get_internal_ptr();
558 std::erase_if(script_components_to_create_, checker);
560 std::erase_if(script_components_to_start_, checker);
562 auto it = std::find_if(std::begin(script_components_), std::end(script_components_), checker);
564 if(it != std::end(script_components_))
566 auto& script_obj = *it;
570 script_obj.state->marked_for_destroy =
true;
579 auto checker = [&](
const auto& rhs)
581 return rhs.scoped->object.get_type().get_internal_ptr() ==
type.get_internal_ptr();
583 std::erase_if(script_components_to_create_, checker);
585 std::erase_if(script_components_to_start_, checker);
587 auto it = std::find_if(std::begin(script_components_), std::end(script_components_), checker);
589 if(it != std::end(script_components_))
591 auto& script_obj = *it;
592 auto& obj = script_obj.scoped->object;
595 script_obj.state->marked_for_destroy =
true;
604 auto it = std::find_if(std::begin(native_components_),
605 std::end(native_components_),
608 return rhs.scoped->object.get_internal_ptr() == obj.get_internal_ptr();
611 if(it != std::end(native_components_))
613 auto& script_obj = *it;
617 script_obj.state->marked_for_destroy =
true;
626 auto it = std::find_if(std::begin(native_components_),
627 std::end(native_components_),
630 return rhs.scoped->object.get_type().get_internal_ptr() ==
type.get_internal_ptr();
633 if(it != std::end(native_components_))
635 auto& script_obj = *it;
637 auto& obj = script_obj.scoped->object;
640 script_obj.state->marked_for_destroy =
true;
649 return script_components_;
654 return !script_components_.empty();
659 return std::any_of(std::begin(script_components_), std::end(script_components_), [&](
const auto& component)
661 return component.scoped->object.get_type().get_name() == type_name;
672 const auto&
object = obj.scoped->object;
673 const auto&
type =
object.get_type();
676 auto attrs =
type.get_attributes();
677 for(
auto& attr : attrs)
679 if(attr.get_type().get_fullname() ==
"Unravel.Core.ScriptSourceFileAttribute")
681 auto invoker = mono::make_property_invoker<std::string>(attr.get_type(),
"Path");
682 return invoker.get_value(attr);
685 auto prop =
type.get_property(
"SourceFilePath");
686 auto invoker = mono::make_property_invoker<std::string>(prop);
687 return invoker.get_value(
object);
689 catch(
const mono::mono_exception& 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.
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 process_pending_actions()
void on_sensor_exit(entt::handle other)
auto add_script_component(const mono::mono_type &type) -> script_object
void process_pending_starts()
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.
void process_pending_deletions()
void process_pending_creates()
static auto context() -> rtti::context &
auto is_create_called() const -> bool
static void log_exception(const mono::mono_exception &e, const hpp::source_location &loc=hpp::source_location::current())