PlayerAnimation.cs 2.95 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
	public float m_SizePerCharge = 0.2f;

12 13 14
	[SerializeField] private PlayerState r_State;
	[SerializeField] private MovementController r_Controller;
	[SerializeField] private CapsuleCollider2D r_Collider2D;
15 16 17

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

20 21 22
	private Vector3 m_OriginSize;
	private Vector2 m_OriginOffset;
	private float m_OriginCheckDistance;
23 24 25 26 27 28


	// Start is called before the first frame update
	void Start()
	{
		r_Controller = GetComponent<MovementController>();
29 30 31 32 33
		r_Collider2D = GetComponent<CapsuleCollider2D>();

		m_OriginSize = transform.localScale;
		m_OriginOffset = r_Controller.GroundCheckOffset;
		m_OriginCheckDistance = r_Controller.GroundCheckDistance;
34 35 36 37 38
	}

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

Unknown's avatar
Unknown committed
42 43 44 45
	public void OnChargesChange()
	{
		switch (r_State.GetCurrentElement())
		{
46 47
			case InfusedElement.None:
				ScalePlayer();
Unknown's avatar
Unknown committed
48
				break;
49
			case InfusedElement.Air:
Unknown's avatar
Unknown committed
50 51
				ScalePlayer();
				break;
52
			case InfusedElement.Fire:
Unknown's avatar
Unknown committed
53
				break;
54
			case InfusedElement.Earth:
Unknown's avatar
Unknown committed
55
				break;
56
			case InfusedElement.Water:
Unknown's avatar
Unknown committed
57 58 59 60
				break;
		}
	}

61 62
	public void ScalePlayer()
	{
Unknown's avatar
Unknown committed
63 64
		int currentCharges = r_State.GetCharges();
		int diff = currentCharges - m_PreviousCharges;
65 66
		Debug.Log("current: " + currentCharges + " previous: " + m_PreviousCharges + " difference: " + diff);
		if (!m_isAnimating || m_isAnimating)
Unknown's avatar
Unknown committed
67
		{
68 69 70 71 72 73 74 75
			if (diff > 0)
			{
				StartCoroutine(ScaleUp(diff));
			}
			else
			{
				StartCoroutine(ScaleDown(diff));
			}
Unknown's avatar
Unknown committed
76 77 78
		}

		m_PreviousCharges = currentCharges;
79 80
	}

81
	private IEnumerator ScaleUp(int amount)
82 83 84 85
	{
		m_isAnimating = true;
		m_isScaled = true;
		Vector3 s = transform.localScale;
86 87 88 89
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0f;
		while (timer <= m_Time)
90
		{
91 92 93 94 95
			x = m_SizePerCharge * Mathf.Abs(amount);
			transform.localScale = Vector3.LerpUnclamped(s, s + new Vector3(x * Mathf.Sign(s.x), x, x),
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
96 97
		}

98
		transform.localScale = s + new Vector3(x * Mathf.Sign(s.x), x, x);
99
		m_isAnimating = false;
100

101 102
	}

103 104

	private IEnumerator ScaleDown(int amount)
105 106 107 108
	{
		m_isAnimating = true;
		m_isScaled = false;
		Vector3 s = transform.localScale;
109 110 111 112 113 114 115 116 117 118 119 120 121
		float step = 1 / m_Time;
		float timer = 0.0f;
		float x = 0;
		while (timer <= m_Time)
		{
			x = m_SizePerCharge * Mathf.Abs(amount);
			transform.localScale = Vector3.LerpUnclamped(s, s - new Vector3(x * Mathf.Sign(s.x), x, x),
				m_AirScale.Evaluate(timer / m_Time));
			timer += Time.deltaTime;
			yield return null;
		}

		if (r_State.GetCharges() > 0)
122 123 124
		{
			transform.localScale = s - new Vector3(x * Mathf.Sign(s.x), x, x);
		}
125 126 127 128 129 130
		else
		{
			transform.localScale = m_OriginSize;
		}


131 132 133 134

		m_isAnimating = false;
	}
}