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

public class PlayerAnimation : MonoBehaviour
{
	public AnimationCurve m_AirScale;
	public float m_Time = 10f;
Unknown's avatar
Unknown committed
10 11 12 13 14 15
	public float m_SizePerCharge = 0.2f;

	[SerializeField]
	private PlayerState r_State;
	[SerializeField]
	private MovementController r_Controller;
16 17 18

	private bool m_isAnimating = false;
	private bool m_isScaled = false;
Unknown's avatar
Unknown committed
19 20
	private int m_PreviousCharges = 0;

21 22 23 24 25 26 27 28 29 30 31 32 33 34


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

	// Update is called once per frame
	void Update()
	{
		ScalePlayer();
	}

Unknown's avatar
Unknown committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
	public void OnChargesChange()
	{
		switch (r_State.GetCurrentElement())
		{
			case InfusedElement.None: ;
				break;
			case InfusedElement.Air: ;
				ScalePlayer();
				break;
			case InfusedElement.Fire: ;
				break;
			case InfusedElement.Earth: ;
				break;
			case InfusedElement.Water: ;
				break;
		}
	}

53 54
	public void ScalePlayer()
	{
Unknown's avatar
Unknown committed
55 56 57
		int currentCharges = r_State.GetCharges();
		int diff = currentCharges - m_PreviousCharges;
		if (m_PreviousCharges < currentCharges && !m_isAnimating)
58
		{
Unknown's avatar
Unknown committed
59 60
			//Debug.Log("Scale up");
			StartCoroutine(ScaleUp(m_Time,  diff));
61
		}
Unknown's avatar
Unknown committed
62 63 64 65 66 67 68
		else
		{
			Debug.Log("Scale down");
			StartCoroutine(ScaleDown(m_Time, diff));
		}

		m_PreviousCharges = currentCharges;
69 70
	}

Unknown's avatar
Unknown committed
71
	private IEnumerator ScaleUp(float t, int amount)
72 73 74 75 76 77 78
	{
		m_isAnimating = true;
		m_isScaled = true;
		Vector3 s = transform.localScale;
		float step = 1 / t;
		for (float i = 0; i <= 1; i += step)
		{
Unknown's avatar
Unknown committed
79
			float x = m_AirScale.Evaluate(i) * m_SizePerCharge * amount;
80
			transform.localScale = s + new Vector3(x * Mathf.Sign(s.x), x, x);
Unknown's avatar
Unknown committed
81
			//r_Controller.GroundCheckDistance += x/2;
82 83 84 85 86 87
			yield return new WaitForFixedUpdate();
		}

		m_isAnimating = false;
	}

Unknown's avatar
Unknown committed
88
	private IEnumerator ScaleDown(float t, int amount)
89 90 91 92 93 94 95
	{
		m_isAnimating = true;
		m_isScaled = false;
		Vector3 s = transform.localScale;
		float step = 1 / t;
		for (float i = 0; i <= 1; i += step)
		{
Unknown's avatar
Unknown committed
96
			float x = m_AirScale.Evaluate(i) * m_SizePerCharge * amount;
97
			transform.localScale = s - new Vector3(x * Mathf.Sign(s.x), x, x);
Unknown's avatar
Unknown committed
98
			//r_Controller.GroundCheckDistance -= x/2;
99 100 101 102 103 104
			yield return new WaitForFixedUpdate();
		}

		m_isAnimating = false;
	}
}