From 44611115fe5eeef45cfc8bac029d37e569aafa6a Mon Sep 17 00:00:00 2001 From: maverickdgg Date: Mon, 19 Sep 2022 15:41:52 +0800 Subject: [PATCH] Added Scene graph functionality (un tested) --- SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp | 43 +++++++++++++++---- SHADE_Engine/src/ECS_Base/Entity/SHEntity.h | 7 +++ .../ECS_Base/Events/SHEntityCreationEvent.h | 1 + .../src/ECS_Base/Managers/SHEntityManager.cpp | 20 ++++++++- .../src/ECS_Base/Managers/SHEntityManager.h | 14 ++++-- SHADE_Engine/src/Scene/SHScene.h | 7 ++- SHADE_Engine/src/Scene/SHSceneManager.cpp | 6 +++ SHADE_Engine/src/Scene/SHSceneManager.h | 9 ++++ 8 files changed, 93 insertions(+), 14 deletions(-) diff --git a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp index 2683a6e9..3ef138ac 100644 --- a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp +++ b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.cpp @@ -47,32 +47,57 @@ namespace SHADE void SHEntity::SetParent(SHEntity* newParent) noexcept { - (void)newParent; - //TODO + SHSceneManager::GetCurrentSceneGraph().SetParent(GetEID(), newParent->GetEID()); } void SHEntity::SetParent(EntityID newParentID) noexcept { - (void)newParentID; - //TODO + SHSceneManager::GetCurrentSceneGraph().SetParent(GetEID(), newParentID); } SHEntity* SHEntity::GetParent()const noexcept { - //TODO - return nullptr; + SHSceneNode* parent = SHSceneManager::GetCurrentSceneGraph().GetParent(GetEID()); + if (parent != nullptr) + return SHEntityManager::GetEntityByID(parent->GetEntityID()); + else + return nullptr; + } + + EntityID SHEntity::GetParentEID()const noexcept + { + SHSceneNode* parent = SHSceneManager::GetCurrentSceneGraph().GetParent(GetEID()); + if (parent != nullptr) + return parent->GetEntityID(); + else + return MAX_EID; } std::vectorconst& SHEntity::GetChildren()const noexcept { - //TODO - return std::vector{}; + std::vector childrenEntity; + + auto& childrenNodes = SHSceneManager::GetCurrentSceneGraph().GetChildren(GetEID()); + for (auto& childNode : childrenNodes) + { + childrenEntity.push_back(SHEntityManager::GetEntityByID(childNode->GetEntityID())); + } + + return childrenEntity; } std::vectorconst& SHEntity::GetChildrenID()const noexcept { - return std::vector{}; + std::vector childrenEntity; + + auto& childrenNodes = SHSceneManager::GetCurrentSceneGraph().GetChildren(GetEID()); + for (auto& childNode : childrenNodes) + { + childrenEntity.push_back(childNode->GetEntityID()); + } + + return childrenEntity; } } diff --git a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h index e499e260..690895e8 100644 --- a/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h +++ b/SHADE_Engine/src/ECS_Base/Entity/SHEntity.h @@ -127,6 +127,13 @@ namespace SHADE ***************************************************************************/ SHEntity* GetParent()const noexcept; + /******************************************************************** + * \brief + * Get the entity ID of the parent. + * \return + * return the entity ID of the parent + ********************************************************************/ + EntityID GetParentEID() const noexcept; /************************************************************************** * \brief diff --git a/SHADE_Engine/src/ECS_Base/Events/SHEntityCreationEvent.h b/SHADE_Engine/src/ECS_Base/Events/SHEntityCreationEvent.h index 728d80f6..709d5147 100644 --- a/SHADE_Engine/src/ECS_Base/Events/SHEntityCreationEvent.h +++ b/SHADE_Engine/src/ECS_Base/Events/SHEntityCreationEvent.h @@ -8,5 +8,6 @@ namespace SHADE { EntityID eid; std::vector componentTypeIDs; + EntityID parentEID; }; } diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp index fddd52cc..bbf8d400 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp +++ b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.cpp @@ -103,8 +103,16 @@ namespace SHADE SHComponentManager::AddComponent(eID, id); } + + //Link up with scene graph. + + if (parentEID != MAX_EID) + SHSceneManager::GetCurrentSceneGraph().AddNode(eID, SHSceneManager::GetCurrentSceneGraph().GetNode(parentEID)); + else + SHSceneManager::GetCurrentSceneGraph().AddNode(eID); + //set up event stuff - SHEntityCreationEvent event{ eID,componentTypeIDs }; + SHEntityCreationEvent event{ eID,componentTypeIDs, parentEID }; SHEventManager::BroadcastEvent(event, SH_ENTITY_CREATION_EVENT); //(SHComponentManager::AddComponent(eID), ...); @@ -138,6 +146,14 @@ namespace SHADE //Call all the children to Destroy themselves first before the parent is destroyed. if (entityVec[eIndex]) { + auto& children = SHSceneManager::GetCurrentSceneGraph().GetChildren(eID); + for (auto& child : children) + { + DestroyEntity(child->GetEntityID()); + } + + SHSceneManager::GetCurrentSceneGraph().RemoveNode(eID); + //auto& children = entityVec[eIndex]->GetChildrenID(); //while(!children.empty()) //{ @@ -159,6 +175,8 @@ namespace SHADE entityVec[eIndex].reset(nullptr); + + SHEntityDestroyedEvent event{eID}; SHEventManager::BroadcastEvent(event, SH_ENTITY_DESTROYED_EVENT); diff --git a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h index a9560fa3..7259c5e8 100644 --- a/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h +++ b/SHADE_Engine/src/ECS_Base/Managers/SHEntityManager.h @@ -23,6 +23,7 @@ #include "ECS_Base/Events/SHEntityCreationEvent.h" #include "ECS_Base/Events/SHEntityDestroyedEvent.h" #include "Events/SHEventManager.h" +#include "Scene/SHSceneManager.h" #include "SH_API.h" namespace SHADE @@ -132,10 +133,19 @@ namespace SHADE std::vector typeIDVec; (typeIDVec.push_back(ComponentFamily::GetID()), ...); - SHEntityCreationEvent event{ eID,typeIDVec }; + + //Link up with scene graph. + + if (parentEID != MAX_EID) + SHSceneManager::GetCurrentSceneGraph().AddNode(eID, SHSceneManager::GetCurrentSceneGraph().GetNode(parentEID)); + else + SHSceneManager::GetCurrentSceneGraph().AddNode(eID); + + SHEntityCreationEvent event{ eID,typeIDVec,parentEID }; SHEventManager::BroadcastEvent(event, SH_ENTITY_CREATION_EVENT); + /*if (entityHandle.IsValid(parentEID) == false) { entityVec[eIndex]->sceneNode.ConnectToRoot(); @@ -146,8 +156,6 @@ namespace SHADE }*/ - //Link up with scene graph. - return eID; } diff --git a/SHADE_Engine/src/Scene/SHScene.h b/SHADE_Engine/src/Scene/SHScene.h index 18aeffdc..372981a6 100644 --- a/SHADE_Engine/src/Scene/SHScene.h +++ b/SHADE_Engine/src/Scene/SHScene.h @@ -12,6 +12,7 @@ #define SH_SCENE_H #include +#include "SHSceneGraph.h" namespace SHADE { @@ -19,15 +20,19 @@ namespace SHADE class SHScene { private: + SHSceneGraph sceneGraph; + protected: SHScene() = default; public: + virtual ~SHScene() = default; + std::string sceneName; - virtual ~SHScene() = default; + SHSceneGraph& GetSceneGraph() noexcept { return sceneGraph; } virtual void Load() = 0; virtual void Init() = 0; diff --git a/SHADE_Engine/src/Scene/SHSceneManager.cpp b/SHADE_Engine/src/Scene/SHSceneManager.cpp index d5223af8..2baf0b9c 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.cpp +++ b/SHADE_Engine/src/Scene/SHSceneManager.cpp @@ -37,6 +37,12 @@ namespace SHADE std::function SHSceneManager::newScene = []() {}; //void (*SHSceneManager::prevSceneCreate)() = []() {}; + + SHSceneGraph& SHSceneManager::GetCurrentSceneGraph() noexcept + { + return currentScene->GetSceneGraph(); + } + void SHSceneManager::UpdateSceneManager() noexcept { if (sceneChanged == false) diff --git a/SHADE_Engine/src/Scene/SHSceneManager.h b/SHADE_Engine/src/Scene/SHSceneManager.h index 4139309d..bdd8f596 100644 --- a/SHADE_Engine/src/Scene/SHSceneManager.h +++ b/SHADE_Engine/src/Scene/SHSceneManager.h @@ -57,6 +57,15 @@ namespace SHADE //boolean to check if the programme has been terminated. static bool quit; + /******************************************************************** + * \brief + * Get the scene graph of the current scene. + * \return + * A reference to the scene graph of the current active scene. + ********************************************************************/ + static SHSceneGraph& GetCurrentSceneGraph() noexcept; + + /*!************************************************************************* * \brief * Initialize scene manager and loads a default scene