﻿using System.Collections;
using UnityEngine;

public static class Utilities {

    public static void DestroyChidren(this Transform transform)
    {
        if (transform == null)
            return;
        if (transform.childCount > 0)
            for (int i = transform.childCount - 1; i >= 0; i--)
                Object.DestroyImmediate(transform.GetChild(i).gameObject);
    }


    #region ProgressionAnim -------------------------------
    public static void MakeProgressionAnim(this MonoBehaviour caller, float duration, System.Action<float> progressionHandler)
    {
        caller.StartCoroutine(ProgressionRoutine(duration, progressionHandler, delegate { }));
    }
    public static void MakeProgressionAnim(this MonoBehaviour caller, float duration, System.Action<float> progressionHandler, System.Action endActionHandler)
    {
        caller.StartCoroutine(ProgressionRoutine(duration, progressionHandler, endActionHandler));
    }

    static IEnumerator ProgressionRoutine(float duration, System.Action<float> progressionHandler, System.Action endActionHandler)
    {
        float progression = 0;
        while (progression < 1)
        {
            progressionHandler(progression);
            progression += Time.deltaTime / duration;
            yield return null;
        }
        endActionHandler();
    }
    #endregion --------------------------------------------
}
