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

public class RocketScaler : MonoBehaviour
{
    #region Fields
    [SerializeField] private AnimationCurve _lerpingCurve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
    private float _targetScale = 1.0f;
    #endregion Fields

    #region Methods
    private void Start()
    {
        _targetScale = transform.localScale.x;
    }

    public void Rescale(float multiplier)
    {
        StopAllCoroutines();
        transform.localScale = Vector3.one * _targetScale;
        StartCoroutine(Scaling(transform.localScale.x, transform.localScale.x*multiplier));
    }

    IEnumerator Scaling(float start, float target)
    {
        _targetScale = target;
        float resolution = 0.1f;
        float duration = _lerpingCurve[_lerpingCurve.length - 1].time;
        for (float t = 0; t < duration; t+= Time.deltaTime)
        {
            transform.localScale = Vector3.one * Mathf.Lerp(start, target, _lerpingCurve.Evaluate(t));
            yield return null;
        }
        transform.localScale = Vector3.one * _targetScale;
    }
    #endregion Methods
}
