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

public class SpriteColorChanger : MonoBehaviour {

    private Coroutine coroutine;

    public void ChangeColor(Color color){
        this.SafeStopCoroutine(coroutine);
        transform.FindAllChildrensComponent<SpriteRenderer>(spriteRenderer => spriteRenderer.color = color);
    }
    public void ChangeColorFade(Color color, float duration){
        ChangeColorFade(color, duration, ActionExtensions.EmptyAction);
    }
    public void ChangeColorFade(Color color, float duration, Action callback){
        this.SafeStopCoroutine(coroutine);
        var list = new List<(SpriteRenderer, Color)>();
        transform.FindAllChildrensComponent<SpriteRenderer>(spriteRenderer => list.Add((spriteRenderer, spriteRenderer.color)));
        coroutine = this.CallLerp(duration, t => {
            foreach(var tuple in list){
                tuple.Item1.color = Color.Lerp(tuple.Item2, color, t);
            }
        }, callback);
    }
}
