From 294411535b75b6eb303b9e1b8407bdc73a56f184 Mon Sep 17 00:00:00 2001 From: Benualdo Date: Sat, 28 Sep 2024 11:41:07 +0200 Subject: [PATCH] "AttachToNode" component can now use any other GameObject, not only a parent of the Component's GameObject --- data/Prefabs/GJ.prefab | 16 ++++--- data/Worlds/Game.world | 9 +++- .../Animation/AttachToNodeComponent.cpp | 45 +++++++++++++++---- .../Animation/AttachToNodeComponent.h | 5 ++- 4 files changed, 58 insertions(+), 17 deletions(-) diff --git a/data/Prefabs/GJ.prefab b/data/Prefabs/GJ.prefab index 38edd0a89..d253eeb4d 100644 --- a/data/Prefabs/GJ.prefab +++ b/data/Prefabs/GJ.prefab @@ -19,7 +19,7 @@ - + @@ -143,7 +143,7 @@ - + @@ -191,7 +191,7 @@ - + @@ -245,7 +245,9 @@ - + + + @@ -254,7 +256,7 @@ - + @@ -308,7 +310,9 @@ - + + + diff --git a/data/Worlds/Game.world b/data/Worlds/Game.world index c8819d49c..32aac7443 100644 --- a/data/Worlds/Game.world +++ b/data/Worlds/Game.world @@ -38,11 +38,18 @@ - + + + + + + + + diff --git a/src/engine/Component/Animation/AttachToNodeComponent.cpp b/src/engine/Component/Animation/AttachToNodeComponent.cpp index 58c34e55c..1c81fd236 100644 --- a/src/engine/Component/Animation/AttachToNodeComponent.cpp +++ b/src/engine/Component/Animation/AttachToNodeComponent.cpp @@ -17,7 +17,11 @@ namespace vg::engine { super::registerProperties(_desc); - registerProperty(AttachToNodeComponent, m_NodeName, "Node"); + registerOptionalProperty(AttachToNodeComponent, m_useTarget, m_target, "Target"); + setPropertyDescription(AttachToNodeComponent, m_target, "Use to specify another GameObject to look for skeleton. Otherwise the first skeleton found in parents will be used."); + + registerProperty(AttachToNodeComponent, m_boneName, "Bone"); + setPropertyDescription(AttachToNodeComponent, m_boneName, "The bone to follow. Bone names can be found under \"MeshComponent>Skeleton>Nodes\"."); return true; } @@ -80,7 +84,7 @@ namespace vg::engine { super::SetPropertyValue(_prop, _previousValue, _newValue); - if (&m_NodeName == _previousValue) + if (&m_boneName == _previousValue) updateCache(); } @@ -89,21 +93,44 @@ namespace vg::engine { clearCache(); - if (const GameObject * go = getGameObject()) + const GameObject * gameObject = nullptr; + const renderer::ISkeleton * skeleton = nullptr; + + if (m_useTarget) { - if (const MeshComponent * mc = go->GetComponentInParentsT()) + if (const GameObject * go = m_target.get()) { - if (const renderer::IMeshInstance * mi = mc->getMeshInstance()) + if (const MeshComponent * mc = go->GetComponentT()) { - if (const renderer::ISkeleton * sk = mi->GetSkeleton()) + if (const renderer::IMeshInstance * mi = mc->getMeshInstance()) { - m_skeleton = sk; - m_nodeIndex = sk->FindNodeIndex(m_NodeName); - return true; + if (const renderer::ISkeleton * sk = mi->GetSkeleton()) + skeleton = sk; } } } } + else + { + if (const GameObject * go = getGameObject()) + { + if (const MeshComponent * mc = go->GetComponentInParentsT()) + { + if (const renderer::IMeshInstance * mi = mc->getMeshInstance()) + { + if (const renderer::ISkeleton * sk = mi->GetSkeleton()) + skeleton = sk; + } + } + } + } + + if (nullptr != skeleton) + { + m_skeleton = skeleton; + m_nodeIndex = skeleton->FindNodeIndex(m_boneName); + return true; + } return false; } diff --git a/src/engine/Component/Animation/AttachToNodeComponent.h b/src/engine/Component/Animation/AttachToNodeComponent.h index 9ee5401bb..1edc6ca8d 100644 --- a/src/engine/Component/Animation/AttachToNodeComponent.h +++ b/src/engine/Component/Animation/AttachToNodeComponent.h @@ -1,6 +1,7 @@ #pragma once #include "core/Component/Component.h" +#include "core/Object/ObjectHandle.h" namespace vg::renderer { @@ -30,7 +31,9 @@ namespace vg::engine void clearCache (); private: - core::string m_NodeName; + bool m_useTarget = false; + core::ObjectHandle m_target; + core::string m_boneName; const renderer::ISkeleton * m_skeleton; renderer::NodeIndex m_nodeIndex; };