Accounted for updating collider storage when a collider is removed

This commit is contained in:
Kah Wei 2022-10-27 12:46:42 +08:00
parent d45d621701
commit 97432b8666
4 changed files with 48 additions and 0 deletions

View File

@ -299,6 +299,14 @@ namespace SHADE
return eventData->handle; return eventData->handle;
} }
SHEventHandle SHScriptEngine::onColliderComponentRemoved(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHComponentRemovedEvent>*>(eventPtr.get());
if (eventData->data->removedComponentType == ComponentFamily::GetID<SHColliderComponent>())
csColliderOnRemoved(eventData->data->eid);
return eventData->handle;
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -405,6 +413,12 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".Collider", DEFAULT_CSHARP_NAMESPACE + ".Collider",
"OnColliderBoundChanged" "OnColliderBoundChanged"
); );
csColliderOnRemoved = dotNet.GetFunctionPtr<CsEventRelayFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".Collider",
"OnColliderRemoved"
);
csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr> csEditorRenderScripts = dotNet.GetFunctionPtr<CsScriptEditorFuncPtr>
( (
DEFAULT_CSHARP_LIB_NAME, DEFAULT_CSHARP_LIB_NAME,
@ -447,6 +461,13 @@ namespace SHADE
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onColliderRemoved) std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onColliderRemoved)
}; };
SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_REMOVED_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(removedColliderEventReceiver)); SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_REMOVED_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(removedColliderEventReceiver));
// Register for collider component removed event
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> removedColliderComponentEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onColliderComponentRemoved)
};
SHEventManager::SubscribeTo(SH_COMPONENT_REMOVED_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(removedColliderComponentEventReceiver));
} }
void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath) void SHScriptEngine::dumpBuildLog(const std::string_view& buildLogPath)

View File

@ -253,6 +253,7 @@ namespace SHADE
CsScriptDeserialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr; CsScriptDeserialiseYamlFuncPtr csScriptsDeserialiseYaml = nullptr;
// - Events // - Events
CsEventRelayFuncPtr csColliderOnListChanged = nullptr; CsEventRelayFuncPtr csColliderOnListChanged = nullptr;
CsEventRelayFuncPtr csColliderOnRemoved = nullptr;
// - Editor // - Editor
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr; CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
CsFuncPtr csEditorUndo = nullptr; CsFuncPtr csEditorUndo = nullptr;
@ -264,6 +265,7 @@ namespace SHADE
SHEventHandle onEntityDestroyed(SHEventPtr eventPtr); SHEventHandle onEntityDestroyed(SHEventPtr eventPtr);
SHEventHandle onColliderAdded(SHEventPtr eventPtr); SHEventHandle onColliderAdded(SHEventPtr eventPtr);
SHEventHandle onColliderRemoved(SHEventPtr eventPtr); SHEventHandle onColliderRemoved(SHEventPtr eventPtr);
SHEventHandle onColliderComponentRemoved(SHEventPtr eventPtr);
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Helper Functions */ /* Helper Functions */

View File

@ -180,8 +180,21 @@ namespace SHADE
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Event Handling Functions */ /* 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) void Collider::OnColliderBoundChanged(EntityID entity)
{ {
SAFE_NATIVE_CALL_BEGIN
// Check if there are any colliders to update // Check if there are any colliders to update
if (colliders == nullptr || !colliders->ContainsKey(entity)) if (colliders == nullptr || !colliders->ContainsKey(entity))
return; return;
@ -204,6 +217,7 @@ namespace SHADE
{ {
collidersList->Remove(wr); collidersList->Remove(wr);
} }
SAFE_NATIVE_CALL_END("Collider.OnColliderBoundChanged")
} }
void Collider::updateSubColliderList() void Collider::updateSubColliderList()

View File

@ -224,6 +224,17 @@ namespace SHADE
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/* Event Handling Functions */ /* Event Handling Functions */
/*-----------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------*/
/// <summary>
/// To be called from native code when a collider has been removed.
/// </summary>
/// <param name="entity">The entity which has it's collider removed.</param>
static void OnColliderRemoved(EntityID entity);
/// <summary>
/// To be called from native code when a Collider bound has been removed.
/// </summary>
/// <param name="entity">
/// The entity which has it's collider bounds changed.
/// </param>
static void OnColliderBoundChanged(EntityID entity); static void OnColliderBoundChanged(EntityID entity);
private: private: