Added Scene Init Exit events #291

Merged
maverickdgg merged 6 commits from SP3-12-SceneGraph into main 2022-12-06 13:39:58 +08:00
5 changed files with 59 additions and 7 deletions

View File

@ -18,4 +18,8 @@ constexpr SHEventIdentifier SH_PHYSICS_COLLIDER_REMOVED_EVENT { 9 };
constexpr SHEventIdentifier SH_EDITOR_ON_PLAY_EVENT { 10 }; constexpr SHEventIdentifier SH_EDITOR_ON_PLAY_EVENT { 10 };
constexpr SHEventIdentifier SH_EDITOR_ON_PAUSE_EVENT { 11 }; constexpr SHEventIdentifier SH_EDITOR_ON_PAUSE_EVENT { 11 };
constexpr SHEventIdentifier SH_EDITOR_ON_STOP_EVENT { 12 }; constexpr SHEventIdentifier SH_EDITOR_ON_STOP_EVENT { 12 };
constexpr SHEventIdentifier SH_SCENE_INIT_PRE { 13 };
constexpr SHEventIdentifier SH_SCENE_INIT_POST { 14 };
constexpr SHEventIdentifier SH_SCENE_EXIT_PRE { 15 };
constexpr SHEventIdentifier SH_SCENE_EXIT_POST { 16 };

View File

@ -12,6 +12,7 @@
// Project Headers // Project Headers
#include "SHSceneNode.h" #include "SHSceneNode.h"
#include "Assets/SHAssetMacros.h"
namespace SHADE namespace SHADE
{ {
@ -38,4 +39,10 @@ namespace SHADE
SHSceneNode* childRemoved = nullptr; SHSceneNode* childRemoved = nullptr;
}; };
struct SHSceneInitExitEvent
{
AssetID sceneAssetID;
};
} // namespace SHADE } // namespace SHADE

View File

@ -204,7 +204,24 @@ namespace SHADE
} }
//////////////////////////////////////// ////////////////////////////////////////
return NODE_ITER->second->IsActive(); // Recurse up the tree until the root. If any parent is inactive, this node is inactive in the hierarchy.
const SHSceneNode* PARENT_NODE = NODE_ITER->second->parent;
while (PARENT_NODE->GetEntityID() != root->GetEntityID())
{
if (!PARENT_NODE->IsActive())
return false;
if (!PARENT_NODE->parent)
{
SHLOGV_ERROR("Entity {}'s node that is not the root has no parent!", PARENT_NODE->GetEntityID())
return false;
}
PARENT_NODE = PARENT_NODE->parent;
}
return true;
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/

View File

@ -15,6 +15,8 @@
//#include "Rendering/Window/SHRenderingWindow.h" //#include "Rendering/Window/SHRenderingWindow.h"
#include "ECS_Base/Managers/SHEntityManager.h" #include "ECS_Base/Managers/SHEntityManager.h"
#include "ECS_Base/Managers/SHSystemManager.h" #include "ECS_Base/Managers/SHSystemManager.h"
#include "SHSceneEvents.h"
#include "Events/SHEventManager.hpp"
//#include "FRC/SHFrameRateController.h" //#include "FRC/SHFrameRateController.h"
//#include "ECS_Base/System/SHApplication.h" //#include "ECS_Base/System/SHApplication.h"
@ -54,9 +56,15 @@ namespace SHADE
{ {
if (currentScene) if (currentScene)
{ {
SHSceneInitExitEvent exitEvent;
exitEvent.sceneAssetID = currentScene->sceneAssetID;
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(exitEvent, SH_SCENE_EXIT_PRE);
currentScene->Free(); currentScene->Free();
currentScene->Unload(); currentScene->Unload();
SHEntityManager::DestroyAllEntity(); SHEntityManager::DestroyAllEntity();
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(exitEvent, SH_SCENE_EXIT_POST);
delete currentScene; delete currentScene;
} }
@ -82,27 +90,48 @@ namespace SHADE
nextSceneID = UINT32_MAX; nextSceneID = UINT32_MAX;
if (currentScene != nullptr) if (currentScene != nullptr)
{ {
currentScene->Load(); currentScene->Load();
SHSceneInitExitEvent initEvent;
initEvent.sceneAssetID = currentScene->sceneAssetID;
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(initEvent, SH_SCENE_INIT_PRE);
currentScene->Init(); currentScene->Init();
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(initEvent, SH_SCENE_INIT_POST);
} }
} }
else // restarting scene else // restarting scene
{ {
nextSceneID = UINT32_MAX; nextSceneID = UINT32_MAX;
SHSceneInitExitEvent exitEvent;
exitEvent.sceneAssetID = currentScene->sceneAssetID;
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(exitEvent, SH_SCENE_EXIT_PRE);
currentScene->Free(); currentScene->Free();
if (cleanReload == true) if (cleanReload == true)
{ {
cleanReload = false; cleanReload = false;
currentScene->Unload(); currentScene->Unload();
SHEntityManager::DestroyAllEntity(); SHEntityManager::DestroyAllEntity();
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(exitEvent, SH_SCENE_EXIT_POST);
currentScene->sceneName = newSceneName; currentScene->sceneName = newSceneName;
currentScene->Load(); currentScene->Load();
} }
else else
{
SHEntityManager::DestroyAllEntity(); SHEntityManager::DestroyAllEntity();
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(exitEvent, SH_SCENE_EXIT_POST);
}
SHSceneInitExitEvent initEvent;
initEvent.sceneAssetID = currentScene->sceneAssetID;
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(initEvent, SH_SCENE_INIT_PRE);
currentScene->Init(); currentScene->Init();
SHEventManager::BroadcastEvent<SHSceneInitExitEvent>(initEvent, SH_SCENE_INIT_POST);
} }
} }

View File

@ -133,11 +133,6 @@ namespace SHADE
void SHSceneNode::SetActive(bool newActiveState) noexcept void SHSceneNode::SetActive(bool newActiveState) noexcept
{ {
active = newActiveState; active = newActiveState;
for (auto* child : children)
{
child->SetActive(newActiveState);
}
} }
} // namespace SHADE } // namespace SHADE