using TMPro;
using UnityEngine;

public class DamagePopup : MonoBehaviour
{
    [SerializeField] private float floatSpeed = 1f;
    [SerializeField] private float lifetime = 0.8f;
    [SerializeField] private float randX = 0.5f;
    [SerializeField] private float randY = 0.3f;
    private TextMeshPro text;
    private Color startColor;

    void Awake()
    {
        text = GetComponentInChildren<TextMeshPro>();
        startColor = text.color;
    }

    public void Init(int damage)
    {
        Vector3 randomPos = transform.position;
        randomPos += new Vector3(Random.Range(-randX, randX), Random.Range(-randY, randY), 0f);
        transform.position = randomPos;
        text.text = damage.ToString();
        Destroy(gameObject, lifetime);
    }

    void Update()
    {
        // float up
        transform.position += Vector3.up * floatSpeed * Time.deltaTime;

        // fade out
        float t = 1 - (Time.timeSinceLevelLoad % lifetime) / lifetime;
        text.color = new Color(startColor.r, startColor.g, startColor.b, t);
    }
}