Utility: Add a better way to attach objects to joints

This commit is contained in:
SirLynix
2022-08-30 18:31:04 +02:00
parent 45c947faf1
commit 7949c57f16
9 changed files with 67 additions and 20 deletions

View File

@@ -3,6 +3,8 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/Components/NodeComponent.hpp>
#include <Nazara/Utility/Components/SharedSkeletonComponent.hpp>
#include <Nazara/Utility/Components/SkeletonComponent.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Debug.hpp>
@@ -15,4 +17,32 @@ namespace Nz
Node::SetParent(nodeComponent, keepDerived);
}
void NodeComponent::SetParentJoint(entt::handle entity, const std::string& jointName, bool keepDerived)
{
SkeletonComponentBase* skeletonComponent = entity.try_get<SkeletonComponent>();
if (!skeletonComponent)
skeletonComponent = entity.try_get<SharedSkeletonComponent>();
NazaraAssert(skeletonComponent, "entity doesn't have a SkeletonComponent nor a SharedSkeletonComponent");
std::size_t jointIndex = skeletonComponent->FindJointByName(jointName);
if (jointIndex == Skeleton::InvalidJointIndex)
{
NazaraError("skeleton has no joint \"" + jointName + "\"");
return;
}
Node::SetParent(skeletonComponent->GetAttachedJoint(jointIndex), keepDerived);
}
void NodeComponent::SetParentJoint(entt::handle entity, std::size_t jointIndex, bool keepDerived)
{
SkeletonComponentBase* skeletonComponent = entity.try_get<SkeletonComponent>();
if (!skeletonComponent)
skeletonComponent = entity.try_get<SharedSkeletonComponent>();
NazaraAssert(skeletonComponent, "entity doesn't have a SkeletonComponent nor a SharedSkeletonComponent");
Node::SetParent(skeletonComponent->GetAttachedJoint(jointIndex), keepDerived);
}
}

View File

@@ -31,11 +31,6 @@ namespace Nz
SetupSkeleton();
}
const Joint& SharedSkeletonComponent::GetAttachedJoint(std::size_t jointIndex) const
{
return *m_attachedSkeleton.GetJoint(jointIndex);
}
SharedSkeletonComponent& SharedSkeletonComponent::operator=(const SharedSkeletonComponent& sharedSkeletalComponent)
{
SkeletonComponentBase::operator=(sharedSkeletalComponent);
@@ -58,6 +53,11 @@ namespace Nz
return *this;
}
const Skeleton& SharedSkeletonComponent::GetAttachedSkeleton() const
{
return m_attachedSkeleton;
}
void SharedSkeletonComponent::OnReferenceJointsInvalidated(const Skeleton* skeleton)
{
m_skeletonJointInvalidated = true;

View File

@@ -12,13 +12,13 @@ namespace Nz
{
}
const Joint& SkeletonComponent::GetAttachedJoint(std::size_t jointIndex) const
{
return *m_referenceSkeleton->GetJoint(jointIndex);
}
Node* SkeletonComponent::GetRootNode()
{
return m_referenceSkeleton->GetRootJoint();
}
const Skeleton& SkeletonComponent::GetAttachedSkeleton() const
{
return *m_referenceSkeleton;
}
}