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

public class Saturator : MonoBehaviour
{
    public float speed = 1;
    private Material[] materials;
    private Health _health;
    private float saturation = 0;
    void Start()
    {
        materials = GetComponent<Renderer>().materials;
        _health = GetComponentInParent<Health>();
        _health.hit.AddListener(delegate{StartCoroutine(Saturate());});
    }

    private IEnumerator Saturate(){
        float prcnt = (_health.maxHealth-_health.GetCurrentHealth())/_health.maxHealth;
        while(saturation < prcnt){
            saturation = Mathf.MoveTowards(saturation, prcnt, speed);
            for(int i = 0; i < materials.Length; i++){
                materials[i].SetFloat("_Progress", saturation);
            }
            yield return null;
        }
    }
}
