summaryrefslogtreecommitdiff
path: root/Valheim_v0.141.2_r202102/Valheim/assembly_valheim/AnimationEffect.cs
blob: f77f3b2a73135e8b0f4d487327bd6759fc16a7f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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");
		}
	}
}