From 19ceab84dfd169b7d2b8382533db426e8a2c785e Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Wed, 26 Oct 2022 18:35:41 +0800 Subject: [PATCH] Added events for adding and removing colliders --- SHADE_Engine/src/Events/SHEventDefines.h | 3 +++ SHADE_Engine/src/Physics/SHPhysicsSystem.cpp | 18 +++++++++++++++++- SHADE_Engine/src/Physics/SHPhysicsSystem.h | 16 ++++++++++++++++ SHADE_Engine/src/Scene/SHSceneGraph.h | 6 +++++- 4 files changed, 41 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Events/SHEventDefines.h b/SHADE_Engine/src/Events/SHEventDefines.h index fc9f41e1..804fbfec 100644 --- a/SHADE_Engine/src/Events/SHEventDefines.h +++ b/SHADE_Engine/src/Events/SHEventDefines.h @@ -11,3 +11,6 @@ constexpr SHEventIdentifier SH_ENTITY_CREATION_EVENT { 2 }; constexpr SHEventIdentifier SH_COMPONENT_ADDED_EVENT { 3 }; constexpr SHEventIdentifier SH_COMPONENT_REMOVED_EVENT { 4 }; constexpr SHEventIdentifier SH_SCENEGRAPH_CHANGE_PARENT_EVENT { 5 }; +constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_ADDED_EVENT { 6 }; +constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_REMOVED_EVENT { 7 }; + diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp index 832f9688..1c6e79d7 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.cpp @@ -210,13 +210,29 @@ namespace SHADE void SHPhysicsSystem::AddCollisionShape(EntityID entityID, SHCollider* collider) { auto* physicsObject = GetPhysicsObject(entityID); - physicsObject->AddCollider(collider); + + const SHPhysicsColliderAddedEvent COLLIDER_ADDED_EVENT_DATA + { + .entityID = entityID + , .colliderType = collider->GetType() + , .colliderIndex = physicsObject->AddCollider(collider) + }; + + SHEventManager::BroadcastEvent(COLLIDER_ADDED_EVENT_DATA, SH_PHYSICS_COLLIDER_ADDED_EVENT); } void SHPhysicsSystem::RemoveCollisionShape(EntityID entityID, int index) { auto* physicsObject = GetPhysicsObject(entityID); physicsObject->RemoveCollider(index); + + const SHPhysicsColliderRemovedEvent COLLIDER_REMOVED_EVENT_DATA + { + .entityID = entityID + , .colliderIndex = index + }; + + SHEventManager::BroadcastEvent(COLLIDER_REMOVED_EVENT_DATA, SH_PHYSICS_COLLIDER_REMOVED_EVENT); } void SHPhysicsSystem::PhysicsPreUpdate::Execute(double) noexcept diff --git a/SHADE_Engine/src/Physics/SHPhysicsSystem.h b/SHADE_Engine/src/Physics/SHPhysicsSystem.h index 0af22e7a..a3c3bea1 100644 --- a/SHADE_Engine/src/Physics/SHPhysicsSystem.h +++ b/SHADE_Engine/src/Physics/SHPhysicsSystem.h @@ -184,5 +184,21 @@ namespace SHADE SHEventHandle RemovePhysicsComponent (SHEventPtr removeComponentEvent); }; + /*-----------------------------------------------------------------------------------*/ + /* Event Data Definitions */ + /*-----------------------------------------------------------------------------------*/ + + struct SHPhysicsColliderAddedEvent + { + EntityID entityID; + SHCollider::Type colliderType; + int colliderIndex; + }; + + struct SHPhysicsColliderRemovedEvent + { + EntityID entityID; + int colliderIndex; + }; } // namespace SHADE \ No newline at end of file diff --git a/SHADE_Engine/src/Scene/SHSceneGraph.h b/SHADE_Engine/src/Scene/SHSceneGraph.h index a4cf45eb..b52fd1ff 100644 --- a/SHADE_Engine/src/Scene/SHSceneGraph.h +++ b/SHADE_Engine/src/Scene/SHSceneGraph.h @@ -158,9 +158,13 @@ namespace SHADE SHSceneNode* AllocateNode (EntityID entityID); void ReleaseNode (SHSceneNode* node) noexcept; - static void TraverseAndInvokePredicate (const SHSceneNode* node, const UnaryFunction& predicate); + static void TraverseAndInvokeFunction (const SHSceneNode* node, const UnaryFunction& function); }; + /*-----------------------------------------------------------------------------------*/ + /* Event Data Definitions */ + /*-----------------------------------------------------------------------------------*/ + struct SHSceneGraphChangeParentEvent { SHSceneNode* node;