Fixed bug where changing scenes would not clear drawn colliders

This commit is contained in:
Diren D Bharwani 2023-03-01 04:34:20 +08:00
parent 3a454953ce
commit 0e3a84a06b
2 changed files with 28 additions and 7 deletions

View File

@ -38,6 +38,9 @@ namespace SHADE
: flags { 0 } : flags { 0 }
{ {
collidersToDraw.clear(); collidersToDraw.clear();
eventFunctions[0] = { &SHPhysicsDebugDrawSystem::onColliderDraw , SH_PHYSICS_COLLIDER_DRAW_EVENT };
eventFunctions[1] = { &SHPhysicsDebugDrawSystem::onSceneExit , SH_SCENE_EXIT_PRE };
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -78,10 +81,13 @@ namespace SHADE
SystemFamily::GetID<SHPhysicsDebugDrawSystem>(); SystemFamily::GetID<SHPhysicsDebugDrawSystem>();
// Register collider draw event // Register collider draw event
const std::shared_ptr EVENT_RECEIVER = std::make_shared<SHEventReceiverSpec<SHPhysicsDebugDrawSystem>>(this, &SHPhysicsDebugDrawSystem::onColliderDraw); for (int i = 0; i < NUM_EVENT_FUNCTIONS; ++i)
const ReceiverPtr EVENT_RECEIVER_PTR = std::dynamic_pointer_cast<SHEventReceiver>(EVENT_RECEIVER); {
const std::shared_ptr EVENT_RECEIVER = std::make_shared<SHEventReceiverSpec<SHPhysicsDebugDrawSystem>>(this, eventFunctions[i].first);
const ReceiverPtr EVENT_RECEIVER_PTR = std::dynamic_pointer_cast<SHEventReceiver>(EVENT_RECEIVER);
SHEventManager::SubscribeTo(SH_PHYSICS_COLLIDER_DRAW_EVENT, EVENT_RECEIVER_PTR); SHEventManager::SubscribeTo(eventFunctions[i].second, EVENT_RECEIVER_PTR);
}
} }
void SHPhysicsDebugDrawSystem::Exit() void SHPhysicsDebugDrawSystem::Exit()
@ -122,6 +128,14 @@ namespace SHADE
return onColliderDrawEvent.get()->handle; return onColliderDrawEvent.get()->handle;
} }
SHEventHandle SHPhysicsDebugDrawSystem::onSceneExit(SHEventPtr onSceneExitEvent)
{
// Empty list of colliders to draw
collidersToDraw.clear();
return onSceneExitEvent.get()->handle;
}
void SHPhysicsDebugDrawSystem::drawCollider(SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept void SHPhysicsDebugDrawSystem::drawCollider(SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept
{ {
for (const auto* SHAPE : collider.GetCollisionShapes()) for (const auto* SHAPE : collider.GetCollisionShapes())

View File

@ -89,6 +89,8 @@ namespace SHADE
/* Type Definitions */ /* Type Definitions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
using EventFunctionPair = std::pair<SHEventHandle(SHPhysicsDebugDrawSystem::*)(SHEventPtr), SHEventIdentifier>;
union DebugDrawInfo union DebugDrawInfo
{ {
struct Contact struct Contact
@ -123,20 +125,25 @@ namespace SHADE
/* Data Members */ /* Data Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
static constexpr uint8_t ACTIVE_FLAG = 0x01; static constexpr uint8_t ACTIVE_FLAG = 0x01;
static constexpr int NUM_EVENT_FUNCTIONS = 4;
static const SHColour DEBUG_DRAW_COLOURS[static_cast<int>(Colours::COUNT)]; static const SHColour DEBUG_DRAW_COLOURS[static_cast<int>(Colours::COUNT)];
// Event function container for cleanly registering to events
EventFunctionPair eventFunctions[NUM_EVENT_FUNCTIONS];
// 0 0 0 drawBroadphase drawRaycasts drawContacts drawAllColliders debugDrawActive // 0 0 0 drawBroadphase drawRaycasts drawContacts drawAllColliders debugDrawActive
uint8_t flags; uint8_t flags;
Colliders collidersToDraw; Colliders collidersToDraw;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Member Functions */ /* Member Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHEventHandle onColliderDraw(SHEventPtr onColliderDrawEvent); SHEventHandle onColliderDraw(SHEventPtr onColliderDrawEvent);
SHEventHandle onSceneExit (SHEventPtr onSceneExitEvent);
static void drawCollider (SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept; static void drawCollider (SHDebugDrawSystem* debugDrawSystem, const SHCollider& collider) noexcept;
static void drawRaycast (SHDebugDrawSystem* debugDrawSystem, const DebugDrawInfo::Raycast& raycastInfo) noexcept; static void drawRaycast (SHDebugDrawSystem* debugDrawSystem, const DebugDrawInfo::Raycast& raycastInfo) noexcept;