From e6bc46a30152a7585dda24fde66622575349d25e Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 15 Nov 2024 15:05:19 +0100 Subject: added data struct for event --- src/crepe/system/CollisionSystem.cpp | 28 ++++++++++++++++++++++------ src/crepe/system/CollisionSystem.h | 15 +++++++++++++-- 2 files changed, 35 insertions(+), 8 deletions(-) (limited to 'src/crepe/system') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index ac35026..56f07cc 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -1,11 +1,12 @@ #include #include #include -#include #include #include #include "CollisionSystem.h" +#include "../api/Event.h" +#include "../api/EventManager.h" #include "../ComponentManager.h" #include "../api/BoxCollider.h" @@ -38,19 +39,24 @@ void CollisionSystem::update() { } void CollisionSystem::call_collision_handler(const CollidedInfoStor& data1,const CollidedInfoStor& data2){ - + const Collider* collider1 = nullptr; + const Collider* collider2 = nullptr; // Check collision type and get values for handler game_object_id_t first = 0,second = 0; if (std::holds_alternative(data1.collider)) { if (std::holds_alternative(data2.collider)) { const BoxCollider& box_collider1 = std::get(data1.collider); const BoxCollider& box_collider2 = std::get(data2.collider); + collider1 = &box_collider1; + collider2 = &box_collider2; first = box_collider1.game_object_id; second = box_collider2.game_object_id; } else { const BoxCollider& box_collider = std::get(data1.collider); const CircleCollider& circle_collider = std::get(data2.collider); + collider1 = &box_collider; + collider2 = &circle_collider; first = box_collider.game_object_id; second = circle_collider.game_object_id; } @@ -59,12 +65,16 @@ void CollisionSystem::call_collision_handler(const CollidedInfoStor& data1,const if (std::holds_alternative(data2.collider)) { const CircleCollider& circle_collider1 = std::get(data1.collider); const CircleCollider& circle_collider2 = std::get(data2.collider); + collider1 = &circle_collider1; + collider2 = &circle_collider2; first = circle_collider1.game_object_id; second = circle_collider2.game_object_id; } else { const CircleCollider& circle_collider = std::get(data1.collider); const BoxCollider& box_collider = std::get(data2.collider); + collider1 = &circle_collider; + collider2 = &box_collider; first = circle_collider.game_object_id; second = box_collider.game_object_id; } @@ -74,10 +84,16 @@ void CollisionSystem::call_collision_handler(const CollidedInfoStor& data1,const if(data1.rigidbody.data.body_type != Rigidbody::BodyType::STATIC) { // If second body is static move back - if(data2.rigidbody.data.body_type == Rigidbody::BodyType::STATIC) return; - //call static handler (is bounce true?) - - // call script handler + if(data2.rigidbody.data.body_type == Rigidbody::BodyType::STATIC){ + //call static handler (is bounce true?) + }; + + crepe::CollisionSystem::CollisionInfo collision_info{ + { *collider1, data1.transform, data1.rigidbody }, + { *collider2, data2.transform, data2.rigidbody } + }; + CollisionEvent data(collision_info); + EventManager::get_instance().trigger_event(data, 0); } } diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 9a72948..c7e8e0b 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -17,8 +17,9 @@ private: using collider_stor = std::variant; private: struct CollidedInfoStor { - collider_stor collider; // Store either BoxCollider or CircleCollider - Transform transform; // Transform data + //! Store either BoxCollider or CircleCollider + collider_stor collider; + Transform transform; Rigidbody rigidbody; // Rigidbody data }; public: @@ -31,6 +32,16 @@ private: bool check_box_circle_collision(const BoxCollider& box1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); bool check_circle_circle_collision(const CircleCollider& circle1, const CircleCollider& circle2, const Transform& transform1, const Transform& transform2, const Rigidbody& rigidbody1, const Rigidbody& rigidbody2); Vector2 current_position(const Collider& collider, const Transform& transform, const Rigidbody& rigidbody); +public: + struct ColliderInfo { + Collider collider; + Transform transform; + Rigidbody rigidbody; + }; + struct CollisionInfo{ + ColliderInfo first; + ColliderInfo second; + }; }; } // namespace crepe -- cgit v1.2.3