PlayerAnimation.cs 6.8 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 43

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

46 47 48 49 50 51


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

Unknown's avatar
Unknown committed
54
		m_OriginScale = transform.localScale;
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 80
				if (m_ScalingActive) ScalePlayer();

				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 88 89 90 91 92 93 94 95 96 97 98 99
				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
100
				break;
101
			case InfusedElement.Fire:
Unknown's avatar
Unknown committed
102 103 104
				if(m_ScalingActive)transform.localScale = m_OriginScale;

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

Unknown's avatar
Unknown committed
115
				break;
116
			case InfusedElement.Earth:
Unknown's avatar
Unknown committed
117 118 119
				if(m_ScalingActive)transform.localScale = m_OriginScale;

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

Unknown's avatar
Unknown committed
130
				break;
131
			case InfusedElement.Water:
Unknown's avatar
Unknown committed
132 133 134
				if(m_ScalingActive)transform.localScale = m_OriginScale;

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

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

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

Unknown's avatar
Unknown committed
166
		m_PreviousAirCharges = currentCharges;
167 168
	}

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

Unknown's avatar
Unknown committed
187
		transform.localScale = s + new Vector3(x * Mathf.Sign(transform.localScale.x), x, x);
188 189 190
		m_isAnimating = false;
	}

Unknown's avatar
Unknown committed
191
	private IEnumerator ScaleDown(int from, int to)
192 193 194 195
	{
		m_isAnimating = true;
		m_isScaled = false;
		Vector3 s = transform.localScale;
196 197 198
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0;
Unknown's avatar
Unknown committed
199
		float diff = to - from;
200 201
		while (timer <= m_Time)
		{
Unknown's avatar
Unknown committed
202 203
			x = m_SizePerCharge * Mathf.Abs(diff);
			transform.localScale = Vector3.LerpUnclamped(s, s - new Vector3(x * Mathf.Sign(transform.localScale.x), x, x),
204 205 206 207 208 209
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
		}

		if (r_State.GetCharges() > 0)
210
		{
Unknown's avatar
Unknown committed
211
			transform.localScale = s - new Vector3(x * Mathf.Sign(transform.localScale.x), x, x);
212
		}
213 214
		else
		{
Unknown's avatar
Unknown committed
215
			transform.localScale = m_OriginScale;
216 217
		}

Unknown's avatar
Unknown committed
218 219
		m_isAnimating = false;
	}
220

Unknown's avatar
Unknown committed
221 222 223 224 225 226 227 228 229
	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
230 231
				if (r_State.GetCharges() > 0)
					pos = m_TimeCounter + i * Mathf.Deg2Rad * (360 / r_State.GetCharges());
Unknown's avatar
Unknown committed
232 233 234 235 236 237
				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
238 239
				m_ElementOrbTransforms[i].transform.position =
					m_RotationOrigin.position + offset + new Vector3(x, y, 0f);
Unknown's avatar
Unknown committed
240 241 242 243
				yield return new WaitForFixedUpdate();
			}
		}
	}
244

Unknown's avatar
Unknown committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
	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;
		}
266 267
	}
}