using UnityEngine;

public class TurningVisible : ReactOnWaveHit
{
    [Range(0, 2)]
    public float fadeSpeed = .6f;

    private float visibility = .01f;
    private SpriteRenderer sr;
    private Color c;

    //void FixedUpdate()
    //{
    //	if(visibleCount > 0)
    //		visibleCount--;
    //	if(visibleCount == 0)
    //		gameObject.GetComponent<SpriteRenderer>().enabled = false;
    //}

    void Start()
    {
        sr = GetComponent<SpriteRenderer>();
        c = sr.color;
        //sr.color = new Color(0,0,0,0);
    }

    void FixedUpdate()
    {
        if (visibility != 0)
        {
            visibility -= Time.deltaTime * fadeSpeed;
            sr.color = new Color(visibility*c.r, visibility*c.g, visibility*c.b, visibility);
        }
    }
    
    public override void hitByWave(Wave wave, float strength)
    {
        if (visibility < .9f)
        {
            SetVisiblity(strength / 60 + 0.5f);

            gameObject.GetComponent<SpriteRenderer>().color = c;
        }
    }

    public void SetVisiblity(float v)
    {
        visibility = v;
    }
}