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

View File

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