using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

[RequireComponent(typeof(TextMeshPro))]
public class MoveAndFade : MonoBehaviour
{

    [SerializeField]
    private float AnimationTime;

    [SerializeField]
    [Tooltip("End point in local position")]
    private Vector3 EndPoint;

    [SerializeField]
    private AnimationCurve FadeCurve;


    private TextMeshPro text;


    private void Start()
    {
        text = GetComponent<TextMeshPro>();
        StartCoroutine(FadeAndMove());
    }

    private IEnumerator FadeAndMove()
    {

        Vector3 startPoint = transform.position;
        Vector3 endPoint = transform.position + EndPoint;
        float startAlpha = text.color.a;

        float elapsedTime = 0;

        while(elapsedTime < AnimationTime)
        {

            float ratio = elapsedTime / AnimationTime;

            float fadeValue = Mathf.Lerp(startAlpha,0.0f, FadeCurve.Evaluate(ratio));
            text.color = new Color(text.color.r, text.color.g, text.color.b, fadeValue);

            transform.position = Vector3.Lerp(startPoint, endPoint, ratio);

            yield return new WaitForEndOfFrame();
            elapsedTime += Time.deltaTime;



        }

        Destroy(gameObject);
    }
}
