Added IsChildOf method to SceneGraph

This commit is contained in:
Diren D Bharwani 2022-11-10 10:20:54 +08:00
parent ac217ffe97
commit 08b3cbafcd
2 changed files with 156 additions and 5 deletions

View File

@ -263,6 +263,7 @@ namespace SHADE
{
////////////////////////////////////////
// Error Handling
if (!SHEntityManager::IsValidEID(entityID))
{
SHLOG_ERROR("Entity {} is invalid! Unable to set parent of an invalid entity!", entityID)
@ -374,6 +375,8 @@ namespace SHADE
bool SHSceneGraph::RemoveNode(EntityID entityID) noexcept
{
////////////////////////////////////////
// Error Handling
if (!SHEntityManager::IsValidEID(entityID))
{
SHLOG_ERROR("Entity {} is invalid!", entityID)
@ -386,6 +389,7 @@ namespace SHADE
SHLOG_ERROR("Entity {} does not exist in the scene!", entityID)
return false;
}
////////////////////////////////////////
// Remove reference of current node from parent
SHSceneNode* currentNode = NODE_ITER->second;
@ -412,6 +416,149 @@ namespace SHADE
ReleaseNode(node);
}
bool SHSceneGraph::IsChildOf(EntityID entityID, SHSceneNode* targetNode) noexcept
{
////////////////////////////////////////
// Error Handling
if (!SHEntityManager::IsValidEID(entityID))
{
SHLOG_ERROR("Entity {} is invalid!", entityID)
return false;
}
auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to check child!", entityID)
return false;
}
////////////////////////////////////////
// Handle self-checks
if (NODE_ITER->second == targetNode)
{
SHLOG_WARNING("Entity {} cannot be a child of itself!", entityID)
return false;
}
// Search for a matching target until the root
const SHSceneNode* CURRENT_TARGET = NODE_ITER->second->parent;
while (CURRENT_TARGET != root)
{
if (CURRENT_TARGET == targetNode)
return true;
CURRENT_TARGET = CURRENT_TARGET->parent;
}
return false;
}
bool SHSceneGraph::IsChildOf(SHSceneNode* node, SHSceneNode* targetNode) noexcept
{
if (!node || !targetNode)
{
SHLOG_ERROR("Attempting to check for invalid scene nodes!")
return false;
}
if (node == targetNode)
{
SHLOG_WARNING("Entity {} cannot be a child of itself!", node->entityID)
return false;
}
const SHSceneNode* CURRENT_TARGET = node->parent;
while (CURRENT_TARGET != root)
{
if (CURRENT_TARGET == targetNode)
return true;
CURRENT_TARGET = CURRENT_TARGET->parent;
}
return false;
}
bool SHSceneGraph::IsChildOf(EntityID entityID, EntityID targetID) noexcept
{
////////////////////////////////////////
// Error Handling
if (!SHEntityManager::IsValidEID(entityID))
{
SHLOG_ERROR("Entity {} is invalid!", entityID)
return false;
}
if (!SHEntityManager::IsValidEID(targetID))
{
SHLOG_ERROR("Entity {} is invalid!", targetID)
return false;
}
auto NODE_ITER = entityNodeMap.find(entityID);
if (NODE_ITER == entityNodeMap.end())
{
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to check child!", entityID)
return false;
}
auto TARGET_ITER = entityNodeMap.find(targetID);
if (TARGET_ITER == entityNodeMap.end())
{
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to check child!", targetID)
return false;
}
////////////////////////////////////////
const SHSceneNode* CURRENT_TARGET = NODE_ITER->second->parent;
while (CURRENT_TARGET != root)
{
if (CURRENT_TARGET == TARGET_ITER->second)
return true;
CURRENT_TARGET = CURRENT_TARGET->parent;
}
return false;
}
bool SHSceneGraph::IsChildOf(SHSceneNode* node, EntityID targetID) noexcept
{
////////////////////////////////////////
// Error Handling
if (!SHEntityManager::IsValidEID(targetID))
{
SHLOG_ERROR("Entity {} is invalid!", targetID)
return false;
}
auto TARGET_ITER = entityNodeMap.find(targetID);
if (TARGET_ITER == entityNodeMap.end())
{
SHLOG_ERROR("Entity {} cannot be found in the scene! Unable to check child!", targetID)
return false;
}
////////////////////////////////////////
const SHSceneNode* CURRENT_TARGET = node->parent;
while (CURRENT_TARGET != root)
{
if (CURRENT_TARGET == TARGET_ITER->second)
return true;
CURRENT_TARGET = CURRENT_TARGET->parent;
}
return false;
}
void SHSceneGraph::Traverse (const UnaryFunction& function) const
{
TraverseAndInvokeFunction(root, function);

View File

@ -66,7 +66,6 @@ namespace SHADE
void SetParent (EntityID entityID, SHSceneNode* newParent) noexcept;
void SetParent (SHSceneNode* node, SHSceneNode* newParent) noexcept;
void SetParent (EntityID entityID, EntityID newParent) noexcept;
void SetParent (SHSceneNode* node, EntityID newParent) noexcept;
@ -79,6 +78,11 @@ namespace SHADE
bool RemoveNode (SHSceneNode* nodeToRemove) noexcept;
void Reset () noexcept;
bool IsChildOf (EntityID entityID, SHSceneNode* targetNode) noexcept;
bool IsChildOf (SHSceneNode* node, SHSceneNode* targetNode) noexcept;
bool IsChildOf (EntityID entityID, EntityID targetID) noexcept;
bool IsChildOf (SHSceneNode* node, EntityID targetID) noexcept;
void Traverse (const UnaryFunction& function) const;
private: