using UnityEngine;

public sealed class TextEffect : MonoBehaviour
{
    [SerializeField]
    private TextMesh textLabel = null;
    [SerializeField]
    private float lifeTime = 1f;
    [SerializeField]
    private Color color = Color.white;
    [SerializeField]
    private Vector3 finishOffset = Vector3.up;

    private float timer = 0;

    private Vector3 startPosition;

    public void Show(string text, Vector3 position)
    {
        textLabel.text = text;
        timer = lifeTime;
        startPosition = position;
        transform.position = position;
        Destroy(gameObject, lifeTime);
    }

    private void Update()
    {
        timer -= Time.deltaTime;
        var normalizedTime = timer / lifeTime;
        transform.position = Vector3.Lerp(startPosition + finishOffset, startPosition, timer / lifeTime);
        textLabel.color = new Color(color.r, color.g, color.b, color.a * normalizedTime);
    }
}
