diff options
Diffstat (limited to 'Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AnimationEffect.cs')
-rw-r--r-- | Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AnimationEffect.cs | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AnimationEffect.cs b/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AnimationEffect.cs new file mode 100644 index 0000000..f77f3b2 --- /dev/null +++ b/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AnimationEffect.cs @@ -0,0 +1,102 @@ +using System.Collections.Generic; +using UnityEngine; + +public class AnimationEffect : MonoBehaviour +{ + public Transform m_effectRoot; + + private Animator m_animator; + + private List<GameObject> m_attachments; + + private int m_attachStateHash; + + private void Start() + { + m_animator = GetComponent<Animator>(); + } + + public void Effect(AnimationEvent e) + { + string stringParameter = e.stringParameter; + GameObject original = e.objectReferenceParameter as GameObject; + Transform transform = null; + if (stringParameter.Length > 0) + { + transform = Utils.FindChild(base.transform, stringParameter); + } + if (transform == null) + { + transform = (m_effectRoot ? m_effectRoot : base.transform); + } + Object.Instantiate(original, transform.position, transform.rotation); + } + + public void Attach(AnimationEvent e) + { + string stringParameter = e.stringParameter; + GameObject original = e.objectReferenceParameter as GameObject; + Transform transform = Utils.FindChild(base.transform, stringParameter); + if (transform == null) + { + ZLog.LogWarning("Failed to find attach joint " + stringParameter); + return; + } + ClearAttachment(transform); + GameObject gameObject = Object.Instantiate(original, transform.position, transform.rotation); + gameObject.transform.SetParent(transform, worldPositionStays: true); + if (m_attachments == null) + { + m_attachments = new List<GameObject>(); + } + m_attachments.Add(gameObject); + m_attachStateHash = e.animatorStateInfo.fullPathHash; + CancelInvoke("UpdateAttachments"); + InvokeRepeating("UpdateAttachments", 0.1f, 0.1f); + } + + private void ClearAttachment(Transform parent) + { + if (m_attachments == null) + { + return; + } + foreach (GameObject attachment in m_attachments) + { + if ((bool)attachment && attachment.transform.parent == parent) + { + m_attachments.Remove(attachment); + Object.Destroy(attachment); + break; + } + } + } + + public void RemoveAttachments() + { + if (m_attachments == null) + { + return; + } + foreach (GameObject attachment in m_attachments) + { + Object.Destroy(attachment); + } + m_attachments.Clear(); + } + + private void UpdateAttachments() + { + if (m_attachments != null && m_attachments.Count > 0) + { + if (m_attachStateHash != m_animator.GetCurrentAnimatorStateInfo(0).fullPathHash && m_attachStateHash != m_animator.GetNextAnimatorStateInfo(0).fullPathHash) + { + RemoveAttachments(); + } + } + else + { + CancelInvoke("UpdateAttachments"); + } + } +} |