PlayerAnimation.cs 8.06 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:
Sebastian's avatar
Sebastian committed
77
				print("RESET");
Unknown's avatar
Unknown committed
78
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
79 80

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

Unknown's avatar
Unknown committed
86 87
				break;
			case InfusedElement.Air:
Sebastian's avatar
Sebastian committed
88
				print("AIR");
Unknown's avatar
Unknown committed
89
				m_CurrentAirCharges = r_State.GetCharges();
Sebastian's avatar
Sebastian committed
90 91 92 93
				if (m_ScalingActive)
				{
					ScalePlayer();
				}
Unknown's avatar
Unknown committed
94 95 96 97 98 99 100 101 102 103

				/*if(m_isRotating) {
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_AirSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
				}*/
Unknown's avatar
Unknown committed
104
				break;
105
			case InfusedElement.Fire:
Unknown's avatar
Unknown committed
106
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
107 108

				if (m_isRotating)
Unknown's avatar
Unknown committed
109
				{
Unknown's avatar
Unknown committed
110 111 112 113 114 115 116
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_FireSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
Unknown's avatar
Unknown committed
117 118
				}

Unknown's avatar
Unknown committed
119
				break;
120
			case InfusedElement.Earth:
Unknown's avatar
Unknown committed
121
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
122 123

				if (m_isRotating)
Unknown's avatar
Unknown committed
124
				{
Unknown's avatar
Unknown committed
125 126 127 128 129 130 131
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_EarthSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
Unknown's avatar
Unknown committed
132 133
				}

Unknown's avatar
Unknown committed
134
				break;
135
			case InfusedElement.Water:
Unknown's avatar
Unknown committed
136
				if(m_ScalingActive)ScaleReset();
Unknown's avatar
Unknown committed
137 138

				if (m_isRotating)
Unknown's avatar
Unknown committed
139
				{
Unknown's avatar
Unknown committed
140 141 142 143 144 145 146
					SetRotationOrbsActive(true, r_State.GetCharges());
					SetRotationOrbSprite(m_WaterSprite);
					if (!m_isRotating)
					{
						m_isRotating = true;
						StartCoroutine(RotatingElements());
					}
Unknown's avatar
Unknown committed
147 148
				}

Unknown's avatar
Unknown committed
149 150 151 152
				break;
		}
	}

153 154
	public void ScalePlayer()
	{
Sebastian's avatar
Sebastian committed
155 156 157 158 159
		m_CurrentAirCharges = r_State.GetCharges();
		var scaleFactor = 1.0f + m_CurrentAirCharges * 0.2f;
		var direction = Mathf.Sign(this.gameObject.transform.localScale.x);
		var newScale = new Vector3(direction * scaleFactor * m_OriginScale.x, scaleFactor * m_OriginScale.x, 0.0f);
		this.gameObject.transform.localScale = newScale;
Unknown's avatar
Unknown committed
160
		m_PreviousAirCharges = m_CurrentAirCharges;
Sebastian's avatar
Sebastian committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
//		m_CurrentAirCharges = r_State.GetCharges();
//		int diff = m_CurrentAirCharges - m_PreviousAirCharges;
//		//Debug.Log("current: " + currentCharges + " previous: " + m_PreviousCharges + " difference: " + diff);
//		if (!m_isAnimating || m_isAnimating)
//		{
//			if (diff > 0)
//			{
//				StartCoroutine(ScaleUp(m_PreviousAirCharges, m_CurrentAirCharges));
//			}
//			else
//			{
//				StartCoroutine(ScaleDown(m_PreviousAirCharges, m_CurrentAirCharges));
//			}
//		}
//
//		m_PreviousAirCharges = m_CurrentAirCharges;
Unknown's avatar
Unknown committed
177 178 179 180 181 182 183
	}

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

Sebastian's avatar
Sebastian committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
//	private IEnumerator ScaleUp(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 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;
//	}
Unknown's avatar
Unknown committed
203 204 205 206


	/*private IEnumerator ScaleUp(float from, float to)
	{
Unknown's avatar
Unknown committed
207 208 209 210

		m_isAnimating = true;
		float step = 1 / m_Time;
		float timer = 0.0f;
Unknown's avatar
Unknown committed
211
		Vector3 scale = transform.localScale;
Unknown's avatar
Unknown committed
212 213 214
		while (timer <= m_Time)
		{
			float newSize = m_SizePerCharge * Mathf.Abs((to - from));
Unknown's avatar
Unknown committed
215
			scale.x *= Mathf.Sign(transform.localScale.x);
Unknown's avatar
Unknown committed
216 217 218 219 220 221
			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();
		}

222
		m_isAnimating = false;
Unknown's avatar
Unknown committed
223
	}*/
224

Unknown's avatar
Unknown committed
225

Unknown's avatar
Unknown committed
226 227
	/*private IEnumerator ScaleDown(float from, float to)
	{
Unknown's avatar
Unknown committed
228 229 230 231

		m_isAnimating = true;
		float step = 1 / m_Time;
		float timer = 0.0f;
Unknown's avatar
Unknown committed
232
		Vector3 scale = transform.localScale;
Unknown's avatar
Unknown committed
233 234 235
		while (timer <= m_Time)
		{
			float newSize = m_SizePerCharge * Mathf.Abs((to - from));
Unknown's avatar
Unknown committed
236
			scale.x *= Math.Sign(transform.localScale.x);
Unknown's avatar
Unknown committed
237 238 239 240 241 242 243 244
			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
245 246
			//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
247 248 249 250
		}
		else
		{
			ScaleReset();
251 252
		}

Unknown's avatar
Unknown committed
253
		m_isAnimating = false;
Unknown's avatar
Unknown committed
254
	}*/
255

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

Unknown's avatar
Unknown committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	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;
		}
301 302
	}
}