Merge pull request #36 from SHADE-DP/SP3-12-SceneGraph

SP3-12 More Scene Graph Fixes

BUGFIXES

Fixed bug where parenting an object will spawn infinite children
Fixed bug where parents held on to a child after the child left
This commit is contained in:
XiaoQiDigipen 2022-09-21 09:06:01 +08:00 committed by GitHub
commit e7eac229ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 18 deletions

View File

@ -155,7 +155,7 @@ namespace SHADE
return root; return root;
SHLOG_WARNING("Scene has no root object!") SHLOG_WARNING("Scene has no root object!")
return nullptr; return nullptr;
} }
SHSceneNode* SHSceneGraph::GetNode(EntityID entityID) const noexcept SHSceneNode* SHSceneGraph::GetNode(EntityID entityID) const noexcept
@ -284,12 +284,17 @@ namespace SHADE
void SHSceneNode::SetParent(SHSceneNode* parentNode) noexcept void SHSceneNode::SetParent(SHSceneNode* parentNode) noexcept
{ {
if (parentNode == nullptr) if (parentNode == nullptr)
{
SHLOG_WARNING("Removing Entity {}'s parent", entityID) SHLOG_WARNING("Removing Entity {}'s parent", entityID)
}
// Handle self assignment // Handle self assignment
if (parentNode == parent) if (parentNode == parent)
return; return;
if (parent)
parent->RemoveChild(this);
parent = parentNode; parent = parentNode;
// Update parent's children // Update parent's children
parent->AddChild(this); parent->AddChild(this);
@ -339,7 +344,7 @@ namespace SHADE
return; return;
} }
auto PARENT_ITER = entityNodeMap.find(entityID); auto PARENT_ITER = entityNodeMap.find(parent);
if (PARENT_ITER == entityNodeMap.end()) if (PARENT_ITER == entityNodeMap.end())
{ {
SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID) SHLOG_WARNING("Entity {} cannot be found in the scene! Unable to parent to Entity {}", parent, entityID)
@ -384,24 +389,21 @@ namespace SHADE
} }
//////////////////////////////////////// ////////////////////////////////////////
SHSceneNode* removedChild = nullptr; auto childIter = std::find_if(children.begin(), children.end(), [&](SHSceneNode* node)
const auto ENTITY_MATCH = [&](SHSceneNode* node)
{ {
if (node->GetEntityID() == childID) return node->GetEntityID() == childID;
{ });
removedChild = node;
return true;
}
if (childIter == children.end())
{
SHLOG_WARNING("Unable to remove Entity {} from Entity {} since it is not it's child!", childID, entityID)
return false; return false;
}; }
children.end() = std::remove_if(children.begin(), children.end(), ENTITY_MATCH); (*childIter)->parent = nullptr;
childIter = children.erase(childIter);
if (removedChild) return true;
removedChild->parent = nullptr;
return removedChild != nullptr;
} }
bool SHSceneNode::RemoveChild(SHSceneNode* childToRemove) noexcept bool SHSceneNode::RemoveChild(SHSceneNode* childToRemove) noexcept
@ -415,7 +417,14 @@ namespace SHADE
} }
//////////////////////////////////////// ////////////////////////////////////////
children.end() = std::remove(children.begin(), children.end(), childToRemove); auto childIter = std::find(children.begin(), children.end(), childToRemove);
if (childIter == children.end())
{
SHLOG_WARNING("Unable to remove Entity {} from Entity {} since it is not it's child!", childToRemove->entityID, entityID)
return false;
}
childIter = children.erase(childIter);
childToRemove->parent = nullptr; childToRemove->parent = nullptr;
return true; return true;
@ -429,7 +438,6 @@ namespace SHADE
children.clear(); children.clear();
} }
SHSceneNode* SHSceneGraph::AddNode(EntityID entityID, SHSceneNode* parent) SHSceneNode* SHSceneGraph::AddNode(EntityID entityID, SHSceneNode* parent)
{ {
//////////////////////////////////////// ////////////////////////////////////////

View File

@ -20,7 +20,7 @@
namespace SHADE namespace SHADE
{ {
class SHSceneManager class SH_API SHSceneManager
{ {
private: private:
//boolean to check if the scene has been changed //boolean to check if the scene has been changed