PlayerAnimation.cs 7.94 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(int from, int to)
176
	{
Unknown's avatar
Unknown committed
177
		/*m_isAnimating = true;
178
		Vector3 s = transform.localScale;
179 180 181
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0f;
Unknown's avatar
Unknown committed
182
		float diff = to - from;
183
		while (timer <= m_Time)
184
		{
Unknown's avatar
Unknown committed
185
			x = m_SizePerCharge * Mathf.Abs(diff);
Unknown's avatar
Unknown committed
186
			transform.localScale = Vector3.Lerp(s, s + new Vector3(x * Mathf.Sign(transform.localScale.x), x, x),
187 188 189
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
190 191
		}

Unknown's avatar
Unknown committed
192
		transform.localScale = s + new Vector3(x * Mathf.Sign(transform.localScale.x), x, x);
Unknown's avatar
Unknown committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206
		m_isAnimating = false;*/

		m_isAnimating = true;
		float step = 1 / m_Time;
		float timer = 0.0f;
		while (timer <= m_Time)
		{
			float newSize = m_SizePerCharge * Mathf.Abs((to - from));
			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();
		}

207 208 209
		m_isAnimating = false;
	}

Unknown's avatar
Unknown committed
210
	private IEnumerator ScaleDown(int from, int to)
211
	{
Unknown's avatar
Unknown committed
212
		/*m_isAnimating = true;
213
		Vector3 s = transform.localScale;
214 215 216
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0;
Unknown's avatar
Unknown committed
217
		float diff = to - from;
218 219
		while (timer <= m_Time)
		{
Unknown's avatar
Unknown committed
220
			x = m_SizePerCharge * Mathf.Abs(diff);
Unknown's avatar
Unknown committed
221
			transform.localScale = Vector3.Lerp(s, s - new Vector3(x * Mathf.Sign(transform.localScale.x), x, x),
222 223 224 225 226 227
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
		}

		if (r_State.GetCharges() > 0)
228
		{
Unknown's avatar
Unknown committed
229
			transform.localScale = s - new Vector3(x * Mathf.Sign(transform.localScale.x), x, x);
230
		}
231 232
		else
		{
Unknown's avatar
Unknown committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
			ScaleReset();
		}

		m_isAnimating = false;*/

		m_isAnimating = true;
		float step = 1 / m_Time;
		float timer = 0.0f;
		while (timer <= m_Time)
		{
			float newSize = m_SizePerCharge * Mathf.Abs((to - from));
			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)
		{
			float  newSize = m_SizePerCharge * Mathf.Abs((to - from));
			transform.localScale = transform.localScale - new Vector3(newSize * Mathf.Sign(transform.localScale.x), newSize, newSize);
		}
		else
		{
			ScaleReset();
258 259
		}

Unknown's avatar
Unknown committed
260 261
		m_isAnimating = false;
	}
262

Unknown's avatar
Unknown committed
263 264 265 266 267 268 269 270 271
	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
272 273
				if (r_State.GetCharges() > 0)
					pos = m_TimeCounter + i * Mathf.Deg2Rad * (360 / r_State.GetCharges());
Unknown's avatar
Unknown committed
274 275 276 277 278 279
				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
280 281
				m_ElementOrbTransforms[i].transform.position =
					m_RotationOrigin.position + offset + new Vector3(x, y, 0f);
Unknown's avatar
Unknown committed
282 283 284 285
				yield return new WaitForFixedUpdate();
			}
		}
	}
286

Unknown's avatar
Unknown committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
	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;
		}
308 309
	}
}