PlayerAnimation.cs 6.14 KB
Newer Older
Unknown's avatar
Unknown committed
1
2
using System;
using System.Collections;
3
using System.Collections.Generic;
Unknown's avatar
Unknown committed
4
using System.Timers;
5
using UnityEngine;
Unknown's avatar
Unknown committed
6
using _Game.Scripts.Scriptable_Object;
7
8
9

public class PlayerAnimation : MonoBehaviour
{
Unknown's avatar
Unknown committed
10
	[Header("Scaling")] public AnimationCurve m_AirScale;
11
	public float m_Time = 10f;
Unknown's avatar
Unknown committed
12
13
	public float m_SizePerCharge = 0.2f;

Unknown's avatar
Unknown committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	[Header("Rotation")] public float m_Speed = 3f;
	public float m_Radius = 1f;
	public float m_RadiusSinDifference = 0.3f;
	public float m_MaxScale = 0.3f;
	public GameObject m_ElementPrefab;
	public Transform m_RotationOrigin;
	public Vector2 m_RotationOriginOffset;
	public Sprite m_AirSprite;
	public Sprite m_EarthSprite;
	public Sprite m_FireSprite;
	public Sprite m_WaterSprite;
	private List<GameObject> m_ElementOrbTransforms;
	private List<SpriteRenderer> m_ElementOrbRenderer;
	private float m_TimeCounter = 0;


	[Header("References")] [SerializeField]
	private PlayerState r_State;

33
34
	[SerializeField] private MovementController r_Controller;
	[SerializeField] private CapsuleCollider2D r_Collider2D;
35
36
37

	private bool m_isAnimating = false;
	private bool m_isScaled = false;
Unknown's avatar
Unknown committed
38
	private bool m_isRotating = false;
Unknown's avatar
Unknown committed
39
40
	private int m_PreviousCharges = 0;

41
42
43
	private Vector3 m_OriginSize;
	private Vector2 m_OriginOffset;
	private float m_OriginCheckDistance;
44
45
46
47
48
49


	// Start is called before the first frame update
	void Start()
	{
		r_Controller = GetComponent<MovementController>();
50
51
52
53
54
		r_Collider2D = GetComponent<CapsuleCollider2D>();

		m_OriginSize = transform.localScale;
		m_OriginOffset = r_Controller.GroundCheckOffset;
		m_OriginCheckDistance = r_Controller.GroundCheckDistance;
55

Unknown's avatar
Unknown committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
		m_ElementOrbTransforms = new List<GameObject>();
		m_ElementOrbRenderer = new List<SpriteRenderer>();

		float dis = 360 / 5; //need fix later
		for (int i = 0; i < 5; i++)
		{
			GameObject obj = Instantiate(m_ElementPrefab, transform);
			obj.SetActive(false);
			m_ElementOrbTransforms.Add(obj);
			m_ElementOrbRenderer.Add(obj.GetComponent<SpriteRenderer>());
			float x = Mathf.Cos(i * Mathf.Deg2Rad * dis) * m_Radius;
			float y = Mathf.Sin(i * Mathf.Deg2Rad * dis) * m_Radius;
			Vector3 offset = m_RotationOriginOffset;
			m_ElementOrbTransforms[i].transform.position = (m_RotationOrigin.position + offset + new Vector3(x, y, 0f));
		}
71
72
	}

Unknown's avatar
Unknown committed
73
74
75
76
	public void OnChargesChange()
	{
		switch (r_State.GetCurrentElement())
		{
77
			case InfusedElement.None:
Unknown's avatar
Unknown committed
78
79
				m_isRotating = false;
				SetRotationOrbsActive(false, 5);
80
				ScalePlayer();
Unknown's avatar
Unknown committed
81
				break;
82
			case InfusedElement.Air:
Unknown's avatar
Unknown committed
83
				ScalePlayer();
Unknown's avatar
Unknown committed
84
85
86
87
88
89
90
91
				SetRotationOrbsActive(true, r_State.GetCharges());
				SetRotationOrbSprite(m_AirSprite);
				if (!m_isRotating)
				{
					m_isRotating = true;
					StartCoroutine(RotatingElements());
				}

Unknown's avatar
Unknown committed
92
				break;
93
			case InfusedElement.Fire:
Unknown's avatar
Unknown committed
94
95
96
97
98
99
100
101
				SetRotationOrbsActive(true, r_State.GetCharges());
				SetRotationOrbSprite(m_FireSprite);
				if (!m_isRotating)
				{
					m_isRotating = true;
					StartCoroutine(RotatingElements());
				}

Unknown's avatar
Unknown committed
102
				break;
103
			case InfusedElement.Earth:
Unknown's avatar
Unknown committed
104
105
106
107
108
109
110
111
				SetRotationOrbsActive(true, r_State.GetCharges());
				SetRotationOrbSprite(m_EarthSprite);
				if (!m_isRotating)
				{
					m_isRotating = true;
					StartCoroutine(RotatingElements());
				}

Unknown's avatar
Unknown committed
112
				break;
113
			case InfusedElement.Water:
Unknown's avatar
Unknown committed
114
115
116
117
118
119
120
121
				SetRotationOrbsActive(true, r_State.GetCharges());
				SetRotationOrbSprite(m_WaterSprite);
				if (!m_isRotating)
				{
					m_isRotating = true;
					StartCoroutine(RotatingElements());
				}

Unknown's avatar
Unknown committed
122
123
124
125
				break;
		}
	}

126
127
	public void ScalePlayer()
	{
Unknown's avatar
Unknown committed
128
129
		int currentCharges = r_State.GetCharges();
		int diff = currentCharges - m_PreviousCharges;
Unknown's avatar
Unknown committed
130
		//Debug.Log("current: " + currentCharges + " previous: " + m_PreviousCharges + " difference: " + diff);
131
		if (!m_isAnimating || m_isAnimating)
Unknown's avatar
Unknown committed
132
		{
133
134
135
136
137
138
139
140
			if (diff > 0)
			{
				StartCoroutine(ScaleUp(diff));
			}
			else
			{
				StartCoroutine(ScaleDown(diff));
			}
Unknown's avatar
Unknown committed
141
142
143
		}

		m_PreviousCharges = currentCharges;
144
145
	}

146
	private IEnumerator ScaleUp(int amount)
147
148
149
150
	{
		m_isAnimating = true;
		m_isScaled = true;
		Vector3 s = transform.localScale;
151
152
153
154
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0f;
		while (timer <= m_Time)
155
		{
156
157
158
159
160
			x = m_SizePerCharge * Mathf.Abs(amount);
			transform.localScale = Vector3.LerpUnclamped(s, s + new Vector3(x * Mathf.Sign(s.x), x, x),
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
161
162
		}

163
		transform.localScale = s + new Vector3(x * Mathf.Sign(s.x), x, x);
164
165
166
		m_isAnimating = false;
	}

167
	private IEnumerator ScaleDown(int amount)
168
169
170
171
	{
		m_isAnimating = true;
		m_isScaled = false;
		Vector3 s = transform.localScale;
172
173
174
175
176
177
178
179
180
181
182
183
184
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0;
		while (timer <= m_Time)
		{
			x = m_SizePerCharge * Mathf.Abs(amount);
			transform.localScale = Vector3.LerpUnclamped(s, s - new Vector3(x * Mathf.Sign(s.x), x, x),
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
		}

		if (r_State.GetCharges() > 0)
185
186
187
		{
			transform.localScale = s - new Vector3(x * Mathf.Sign(s.x), x, x);
		}
188
189
190
191
192
		else
		{
			transform.localScale = m_OriginSize;
		}

Unknown's avatar
Unknown committed
193
194
		m_isAnimating = false;
	}
195

Unknown's avatar
Unknown committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
	private IEnumerator RotatingElements()
	{
		while (m_isRotating)
		{
			m_TimeCounter += Time.deltaTime * m_Speed;
			float pos = 0;
			for (int i = 0; i < m_ElementOrbTransforms.Count; i++)
			{
				pos = m_TimeCounter;
				if(r_State.GetCharges() > 0)
				pos = m_TimeCounter + i * Mathf.Deg2Rad * (360 / r_State.GetCharges());
				float r = (m_Radius - m_RadiusSinDifference) + (m_RadiusSinDifference * Mathf.Sin(m_TimeCounter));
				float x = Mathf.Cos(pos) * r;
				float y = Mathf.Sin(pos) * r;

				Vector3 offset = m_RotationOriginOffset;
				//Transform elem = m_ElementOrbTransforms[i].transform;
				m_ElementOrbTransforms[i].transform.position = m_RotationOrigin.position + offset + new Vector3(x, y, 0f);
				yield return new WaitForFixedUpdate();
			}
		}
	}
218

Unknown's avatar
Unknown committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	private void SetRotationOrbsActive(bool value, int amount)
	{
		for (int i = 0; i < m_ElementOrbTransforms.Count; i++)
		{
			if (i < amount)
			{
				m_ElementOrbTransforms[i].SetActive(value);
			}
			else
			{
				m_ElementOrbTransforms[i].SetActive(!value);
			}
		}
	}

	private void SetRotationOrbSprite(Sprite s)
	{
		for (int i = 0; i < m_ElementOrbRenderer.Count; i++)
		{
			m_ElementOrbRenderer[i].sprite = s;

		}
241
242
	}
}