From 97432b866687da2a4b5e67a1d25704298b3b6782 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 27 Oct 2022 12:46:42 +0800 Subject: [PATCH] Accounted for updating collider storage when a collider is removed --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 21 +++++++++++++++++++ SHADE_Engine/src/Scripting/SHScriptEngine.h | 2 ++ SHADE_Managed/src/Components/Collider.cxx | 14 +++++++++++++ SHADE_Managed/src/Components/Collider.hxx | 11 ++++++++++ 4 files changed, 48 insertions(+) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 3a2ea29f..0a687601 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -299,6 +299,14 @@ namespace SHADE return eventData->handle; } + SHEventHandle SHScriptEngine::onColliderComponentRemoved(SHEventPtr eventPtr) + { + auto eventData = reinterpret_cast*>(eventPtr.get()); + if (eventData->data->removedComponentType == ComponentFamily::GetID()) + csColliderOnRemoved(eventData->data->eid); + return eventData->handle; + } + /*-----------------------------------------------------------------------------------*/ /* Helper Functions */ /*-----------------------------------------------------------------------------------*/ @@ -405,6 +413,12 @@ namespace SHADE DEFAULT_CSHARP_NAMESPACE + ".Collider", "OnColliderBoundChanged" ); + csColliderOnRemoved = dotNet.GetFunctionPtr + ( + DEFAULT_CSHARP_LIB_NAME, + DEFAULT_CSHARP_NAMESPACE + ".Collider", + "OnColliderRemoved" + ); csEditorRenderScripts = dotNet.GetFunctionPtr ( DEFAULT_CSHARP_LIB_NAME, @@ -447,6 +461,13 @@ namespace SHADE std::make_shared>(this, &SHScriptEngine::onColliderRemoved) }; SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_REMOVED_EVENT, std::dynamic_pointer_cast(removedColliderEventReceiver)); + + // Register for collider component removed event + std::shared_ptr> removedColliderComponentEventReceiver + { + std::make_shared>(this, &SHScriptEngine::onColliderComponentRemoved) + }; + SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast(removedColliderComponentEventReceiver)); } void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index a909a334..c38e3618 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -253,6 +253,7 @@ namespace SHADE CsScriptDeserialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr; // - Events CsEventRelayFuncPtr csColliderOnListChanged = nullptr; + CsEventRelayFuncPtr csColliderOnRemoved = nullptr; // - Editor CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsFuncPtr csEditorUndo = nullptr; @@ -264,6 +265,7 @@ namespace SHADE SHEventHandle onEntityDestroyed(SHEventPtr eventPtr); SHEventHandle onColliderAdded(SHEventPtr eventPtr); SHEventHandle onColliderRemoved(SHEventPtr eventPtr); + SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr); /*-----------------------------------------------------------------------------*/ /* Helper Functions */ diff --git a/SHADE_Managed/src/Components/Collider.cxx b/SHADE_Managed/src/Components/Collider.cxx index 7f41ad44..6d2c2f01 100644 --- a/SHADE_Managed/src/Components/Collider.cxx +++ b/SHADE_Managed/src/Components/Collider.cxx @@ -180,8 +180,21 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ /* Event Handling Functions */ /*---------------------------------------------------------------------------------*/ + void Collider::OnColliderRemoved(EntityID entity) + { + SAFE_NATIVE_CALL_BEGIN + // Check if there are any colliders to update + if (colliders == nullptr || !colliders->ContainsKey(entity)) + return; + + // Remove the key + colliders->Remove(entity); + SAFE_NATIVE_CALL_END("Collider.OnColliderRemoved") + } + void Collider::OnColliderBoundChanged(EntityID entity) { + SAFE_NATIVE_CALL_BEGIN // Check if there are any colliders to update if (colliders == nullptr || !colliders->ContainsKey(entity)) return; @@ -204,6 +217,7 @@ namespace SHADE { collidersList->Remove(wr); } + SAFE_NATIVE_CALL_END("Collider.OnColliderBoundChanged") } void Collider::updateSubColliderList() diff --git a/SHADE_Managed/src/Components/Collider.hxx b/SHADE_Managed/src/Components/Collider.hxx index 8e63fc83..f827ab71 100644 --- a/SHADE_Managed/src/Components/Collider.hxx +++ b/SHADE_Managed/src/Components/Collider.hxx @@ -224,6 +224,17 @@ namespace SHADE /*-----------------------------------------------------------------------------*/ /* Event Handling Functions */ /*-----------------------------------------------------------------------------*/ + /// + /// To be called from native code when a collider has been removed. + /// + /// The entity which has it's collider removed. + static void OnColliderRemoved(EntityID entity); + /// + /// To be called from native code when a Collider bound has been removed. + /// + /// + /// The entity which has it's collider bounds changed. + /// static void OnColliderBoundChanged(EntityID entity); private: