From a69ae733dae7996be41f54d8f8d9c1c5e5879b92 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 1 Dec 2024 22:49:15 +0100 Subject: moved function --- src/crepe/system/CollisionSystem.cpp | 38 +++++++++++++++++------------------- src/crepe/system/CollisionSystem.h | 19 ++++++++++++++++++ 2 files changed, 37 insertions(+), 20 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 6a621fa..c7fbff9 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -217,26 +217,6 @@ std::vector std::optional, std::reference_wrapper>> { - RefVector transforms = this->component_manager.get_components_by_id(game_object_id); - if (transforms.empty()) return std::nullopt; - - RefVector rigidbodies = this->component_manager.get_components_by_id(game_object_id); - if (rigidbodies.empty()) return std::nullopt; - - Transform& transform = transforms.front().get(); - if (!transform.active) return std::nullopt; - - Rigidbody& rigidbody = rigidbodies.front().get(); - if (!rigidbody.active) return std::nullopt; - - // Return the active components - return std::make_pair(std::ref(transform), std::ref(rigidbody)); - }; - - std::vector> collisions_ret; for (size_t i = 0; i < colliders.size(); ++i) { std::visit([&](auto& inner_collider_ref) { @@ -263,6 +243,24 @@ std::vector, std::reference_wrapper>> +CollisionSystem::get_active_transform_and_rigidbody(game_object_id_t game_object_id) { + RefVector transforms = this->component_manager.get_components_by_id(game_object_id); + if (transforms.empty()) return std::nullopt; + + RefVector rigidbodies = this->component_manager.get_components_by_id(game_object_id); + if (rigidbodies.empty()) return std::nullopt; + + Transform& transform = transforms.front().get(); + if (!transform.active) return std::nullopt; + + Rigidbody& rigidbody = rigidbodies.front().get(); + if (!rigidbody.active) return std::nullopt; + + // Return the active components + return std::make_pair(std::ref(transform), std::ref(rigidbody)); +} + CollisionSystem::CollisionInternalType CollisionSystem::check_collider_type(const collider_variant& collider1,const collider_variant& collider2){ if(std::holds_alternative>(collider1)){ if(std::holds_alternative>(collider2)) diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 056a7f1..39ee83d 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -2,6 +2,7 @@ #include #include +#include #include "api/Rigidbody.h" #include "api/Transform.h" @@ -12,6 +13,7 @@ #include "Collider.h" #include "System.h" + namespace crepe { @@ -184,6 +186,23 @@ private: */ bool check_collision(const collider_variant& collider1,std::pair, std::reference_wrapper> components1,const collider_variant& collider2,std::pair, std::reference_wrapper> components2,CollisionSystem::CollisionInternalType type); + /** + * \brief Retrieves the active Transform and Rigidbody components for a given game object. + * + * This function looks up the Transform and Rigidbody components associated with the specified + * game object ID. It checks if both components are present and active. If both are found + * to be active, they are returned wrapped in reference wrappers; otherwise, an empty + * optional is returned. + * + * \param game_object_id The ID of the game object for which to retrieve the components. + * + * \return A std::optional containing a pair of reference wrappers to the active Transform + * and Rigidbody components, or std::nullopt if either component is not found + * or not active. + */ + std::optional, std::reference_wrapper>> get_active_transform_and_rigidbody(game_object_id_t game_object_id); + + /** * \brief Detects collisions between two BoxColliders. * -- cgit v1.2.3