aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorJAROWMR <jarorutjes07@gmail.com>2024-11-15 15:05:19 +0100
committerJAROWMR <jarorutjes07@gmail.com>2024-11-15 15:05:19 +0100
commite6bc46a30152a7585dda24fde66622575349d25e (patch)
tree1d96d73bec54d14b2e4c9eaa3d01f73f294087f4 /src/crepe
parent37c016f1d5902cf7bbfed08d13ee2bc6ef725add (diff)
added data struct for event
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Event.h6
-rw-r--r--src/crepe/system/CollisionSystem.cpp28
-rw-r--r--src/crepe/system/CollisionSystem.h15
3 files changed, 40 insertions, 9 deletions
diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h
index 701ecdf..3431f29 100644
--- a/src/crepe/api/Event.h
+++ b/src/crepe/api/Event.h
@@ -3,6 +3,7 @@
#include <iostream>
#include <string>
#include <typeindex>
+#include "system/CollisionSystem.h"
class Event {
public:
@@ -45,7 +46,10 @@ public:
};
class CollisionEvent : public Event {
public:
- //Collision collisionData;
+ crepe::CollisionSystem::CollisionInfo info;
+ CollisionEvent(const crepe::CollisionSystem::CollisionInfo& collisionInfo)
+ : info(collisionInfo) {}
+
};
class TextSubmitEvent : public Event {
public:
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 <cmath>
#include <algorithm>
#include <cstddef>
-#include <tuple>
#include <utility>
#include <variant>
#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<BoxCollider>(data1.collider)) {
if (std::holds_alternative<BoxCollider>(data2.collider)) {
const BoxCollider& box_collider1 = std::get<BoxCollider>(data1.collider);
const BoxCollider& box_collider2 = std::get<BoxCollider>(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<BoxCollider>(data1.collider);
const CircleCollider& circle_collider = std::get<CircleCollider>(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<CircleCollider>(data2.collider)) {
const CircleCollider& circle_collider1 = std::get<CircleCollider>(data1.collider);
const CircleCollider& circle_collider2 = std::get<CircleCollider>(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<CircleCollider>(data1.collider);
const BoxCollider& box_collider = std::get<BoxCollider>(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<CollisionEvent>(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<BoxCollider, CircleCollider>;
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