PlayerAnimation.cs 7.58 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;
Unknown's avatar
Unknown committed
11
	public bool m_ScalingActive = true;
12
	public float m_Time = 10f;
Unknown's avatar
Unknown committed
13
	public float m_SizePerCharge = 0.2f;
Unknown's avatar
Unknown committed
14
15
16
	private Vector3 m_OriginScale;
	private int m_PreviousAirCharges = 0;
	private int m_CurrentAirCharges = 0;
Unknown's avatar
Unknown committed
17

Unknown's avatar
Unknown committed
18
	[Header("Rotation")] public float m_Speed = 3f;
Unknown's avatar
Unknown committed
19
	public bool m_RotationActive = false;
Unknown's avatar
Unknown committed
20
21
22
23
	public float m_Radius = 1f;
	public float m_RadiusSinDifference = 0.3f;
	public float m_MaxScale = 0.3f;
	public GameObject m_ElementPrefab;
Unknown's avatar
Unknown committed
24
	public Transform m_ElementOrbParent;
Unknown's avatar
Unknown committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	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;

39
40
	[SerializeField] private MovementController r_Controller;
	[SerializeField] private CapsuleCollider2D r_Collider2D;
41
42

	private bool m_isAnimating = false;
Unknown's avatar
Unknown committed
43
	private bool m_isRotating = false;
Unknown's avatar
Unknown committed
44

45
46
47
48
49
50


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

Unknown's avatar
Unknown committed
53
		m_OriginScale = transform.localScale;
54

Unknown's avatar
Unknown committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
		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));
		}
70
71
	}

Unknown's avatar
Unknown committed
72
73
74
75
	public void OnChargesChange()
	{
		switch (r_State.GetCurrentElement())
		{
76
			case InfusedElement.None:
Unknown's avatar
Unknown committed
77
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
78
79

				if (m_RotationActive)
Unknown's avatar
Unknown committed
80
				{
Unknown's avatar
Unknown committed
81
82
					m_isRotating = false;
					SetRotationOrbsActive(false, 5);
Unknown's avatar
Unknown committed
83
84
				}

Unknown's avatar
Unknown committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
				break;
			case InfusedElement.Air:
				m_CurrentAirCharges = r_State.GetCharges();
				if (m_ScalingActive) ScalePlayer();

				/*if(m_isRotating) {
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_AirSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
				}*/
Unknown's avatar
Unknown committed
99
				break;
100
			case InfusedElement.Fire:
Unknown's avatar
Unknown committed
101
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
102
103

				if (m_isRotating)
Unknown's avatar
Unknown committed
104
				{
Unknown's avatar
Unknown committed
105
106
107
108
109
110
111
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_FireSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
Unknown's avatar
Unknown committed
112
113
				}

Unknown's avatar
Unknown committed
114
				break;
115
			case InfusedElement.Earth:
Unknown's avatar
Unknown committed
116
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
117
118

				if (m_isRotating)
Unknown's avatar
Unknown committed
119
				{
Unknown's avatar
Unknown committed
120
121
122
123
124
125
126
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_EarthSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
Unknown's avatar
Unknown committed
127
128
				}

Unknown's avatar
Unknown committed
129
				break;
130
			case InfusedElement.Water:
Unknown's avatar
Unknown committed
131
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
132
133

				if (m_isRotating)
Unknown's avatar
Unknown committed
134
				{
Unknown's avatar
Unknown committed
135
136
137
138
139
140
141
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_WaterSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
Unknown's avatar
Unknown committed
142
143
				}

Unknown's avatar
Unknown committed
144
145
146
147
				break;
		}
	}

148
149
	public void ScalePlayer()
	{
Unknown's avatar
Unknown committed
150
151
		 m_CurrentAirCharges = r_State.GetCharges();
		int diff = m_CurrentAirCharges - m_PreviousAirCharges;
Unknown's avatar
Unknown committed
152
		//Debug.Log("current: " + currentCharges + " previous: " + m_PreviousCharges + " difference: " + diff);
153
		if (!m_isAnimating || m_isAnimating)
Unknown's avatar
Unknown committed
154
		{
155
156
			if (diff > 0)
			{
Unknown's avatar
Unknown committed
157
				StartCoroutine(ScaleUp(m_PreviousAirCharges, m_CurrentAirCharges));
158
159
160
			}
			else
			{
Unknown's avatar
Unknown committed
161
				StartCoroutine(ScaleDown(m_PreviousAirCharges, m_CurrentAirCharges));
162
			}
Unknown's avatar
Unknown committed
163
164
		}

Unknown's avatar
Unknown committed
165
166
167
168
169
170
171
172
		m_PreviousAirCharges = m_CurrentAirCharges;
	}

	private void ScaleReset()
	{
		Vector3 s = m_OriginScale;
		s.x *= Mathf.Sign(transform.localScale.x);
		transform.localScale = s;
173
174
	}

Unknown's avatar
Unknown committed
175
	private IEnumerator ScaleUp(float from, float to)
176
	{
Unknown's avatar
Unknown committed
177
178
179
180
181
182
		m_isAnimating = true;
		float newSize = m_SizePerCharge * Mathf.Abs((to - from));
		transform.localScale += new Vector3(newSize * Mathf.Sign(transform.localScale.x), newSize, newSize);
		yield return null;
		m_isAnimating = false;
	}
183

Unknown's avatar
Unknown committed
184
185
186
187
188
189
190
191
192
193
194
195
	private IEnumerator ScaleDown(float from, float to)
	{
		m_isAnimating = true;
		float newSize = m_SizePerCharge * Mathf.Abs((to - from));
		transform.localScale -= new Vector3(newSize * Mathf.Sign(transform.localScale.x), newSize, newSize);
		yield return null;
		m_isAnimating = false;
	}


	/*private IEnumerator ScaleUp(float from, float to)
	{
Unknown's avatar
Unknown committed
196
197
198
199

		m_isAnimating = true;
		float step = 1 / m_Time;
		float timer = 0.0f;
Unknown's avatar
Unknown committed
200
		Vector3 scale = transform.localScale;
Unknown's avatar
Unknown committed
201
202
203
		while (timer <= m_Time)
		{
			float newSize = m_SizePerCharge * Mathf.Abs((to - from));
Unknown's avatar
Unknown committed
204
			scale.x *= Mathf.Sign(transform.localScale.x);
Unknown's avatar
Unknown committed
205
206
207
208
209
210
			transform.localScale = Vector3.Lerp(transform.localScale, transform.localScale + new Vector3(newSize * Mathf.Sign(transform.localScale.x), newSize, newSize),
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return new WaitForFixedUpdate();
		}

211
		m_isAnimating = false;
Unknown's avatar
Unknown committed
212
	}*/
213

Unknown's avatar
Unknown committed
214

Unknown's avatar
Unknown committed
215
216
	/*private IEnumerator ScaleDown(float from, float to)
	{
Unknown's avatar
Unknown committed
217
218
219
220

		m_isAnimating = true;
		float step = 1 / m_Time;
		float timer = 0.0f;
Unknown's avatar
Unknown committed
221
		Vector3 scale = transform.localScale;
Unknown's avatar
Unknown committed
222
223
224
		while (timer <= m_Time)
		{
			float newSize = m_SizePerCharge * Mathf.Abs((to - from));
Unknown's avatar
Unknown committed
225
			scale.x *= Math.Sign(transform.localScale.x);
Unknown's avatar
Unknown committed
226
227
228
229
230
231
232
233
			transform.localScale = Vector3.Lerp(transform.localScale, transform.localScale - new Vector3(newSize * Mathf.Sign(transform.localScale.x), newSize, newSize),
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return new WaitForFixedUpdate();
		}

		if (r_State.GetCharges() > 0)
		{
Unknown's avatar
Unknown committed
234
235
			//float  newSize = m_SizePerCharge * Mathf.Abs((to - from));
			//transform.localScale = transform.localScale - new Vector3(newSize * Mathf.Sign(transform.localScale.x), newSize, newSize);
Unknown's avatar
Unknown committed
236
237
238
239
		}
		else
		{
			ScaleReset();
240
241
		}

Unknown's avatar
Unknown committed
242
		m_isAnimating = false;
Unknown's avatar
Unknown committed
243
	}*/
244

Unknown's avatar
Unknown committed
245
246
247
248
249
250
251
252
253
	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;
Unknown's avatar
Unknown committed
254
255
				if (r_State.GetCharges() > 0)
					pos = m_TimeCounter + i * Mathf.Deg2Rad * (360 / r_State.GetCharges());
Unknown's avatar
Unknown committed
256
257
258
259
260
261
				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;
Unknown's avatar
Unknown committed
262
263
				m_ElementOrbTransforms[i].transform.position =
					m_RotationOrigin.position + offset + new Vector3(x, y, 0f);
Unknown's avatar
Unknown committed
264
265
266
267
				yield return new WaitForFixedUpdate();
			}
		}
	}
268

Unknown's avatar
Unknown committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
	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;
		}
290
291
	}
}