diff options
-rw-r--r-- | src/crepe/system/CollisionSystem.cpp | 38 | ||||
-rw-r--r-- | src/crepe/system/CollisionSystem.h | 19 |
2 files changed, 37 insertions, 20 deletions
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::pair<CollisionSystem::CollisionInternal,CollisionSystem::Collis // Quadtree code // Quadtree is placed over the input vector - // Function to retrieve active transform and rigidbody components for a given game_object_id - auto get_active_transform_and_rigidbody = [&](game_object_id_t game_object_id) - -> std::optional<std::pair<std::reference_wrapper<Transform>, std::reference_wrapper<Rigidbody>>> { - RefVector<Transform> transforms = this->component_manager.get_components_by_id<Transform>(game_object_id); - if (transforms.empty()) return std::nullopt; - - RefVector<Rigidbody> rigidbodies = this->component_manager.get_components_by_id<Rigidbody>(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<std::pair<CollisionInternal,CollisionInternal>> collisions_ret; for (size_t i = 0; i < colliders.size(); ++i) { std::visit([&](auto& inner_collider_ref) { @@ -263,6 +243,24 @@ std::vector<std::pair<CollisionSystem::CollisionInternal,CollisionSystem::Collis return collisions_ret; } +std::optional<std::pair<std::reference_wrapper<Transform>, std::reference_wrapper<Rigidbody>>> +CollisionSystem::get_active_transform_and_rigidbody(game_object_id_t game_object_id) { + RefVector<Transform> transforms = this->component_manager.get_components_by_id<Transform>(game_object_id); + if (transforms.empty()) return std::nullopt; + + RefVector<Rigidbody> rigidbodies = this->component_manager.get_components_by_id<Rigidbody>(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<std::reference_wrapper<CircleCollider>>(collider1)){ if(std::holds_alternative<std::reference_wrapper<CircleCollider>>(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 <vector> #include <variant> +#include <optional> #include "api/Rigidbody.h" #include "api/Transform.h" @@ -12,6 +13,7 @@ #include "Collider.h" #include "System.h" + namespace crepe { @@ -185,6 +187,23 @@ private: bool check_collision(const collider_variant& collider1,std::pair<std::reference_wrapper<Transform>, std::reference_wrapper<Rigidbody>> components1,const collider_variant& collider2,std::pair<std::reference_wrapper<Transform>, std::reference_wrapper<Rigidbody>> 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::pair<std::reference_wrapper<Transform>, std::reference_wrapper<Rigidbody>>> get_active_transform_and_rigidbody(game_object_id_t game_object_id); + + + /** * \brief Detects collisions between two BoxColliders. * * \param box1 The first BoxCollider. |