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

public class HitAnimator : MonoBehaviour {

    SpriteRenderer [] sr;
    
    [SerializeField] HitAnimatorData data;
    readonly Color hitColor = Color.red;

    private void Awake() {
        sr = GetComponentsInChildren<SpriteRenderer>();
    }

    public void PlayHit() {
        StartCoroutine(PlayHitIE());
    }

    static Color colBuf;
    static Color whiteColor = Color.white;

    IEnumerator PlayHitIE() {
        int frame = 0;

        while(frame < data.totalFrames) {
            colBuf = Color.Lerp(hitColor, whiteColor, frame/data.totalFrames);

            for(int i = 0; i < sr.Length; i++) {
                if(sr[i] == null) continue;
                sr[i].color = colBuf;
            }

            frame++;
            yield return null;
        }

        for(int i = 0; i < sr.Length; i++) {
            if(sr[i] == null) continue;
            sr[i].color = whiteColor;
        }
    }
}
