using ScrambleKit.Hits;
using UnityEngine;

public class PlayerHurtFlash : MonoBehaviour 
{
    public Sprite FlashSprite;
    public float FlashDuration = 0.450f;
    public float FlashInterval = 0.11f;

    private Sprite normSprite;
    private SpriteRenderer rend;

    private float flashUntil;

	void Start () 
	{
        rend = GetComponent<SpriteRenderer>();
        normSprite = rend.sprite;
	}

    void TakeHit(HitInfo hit)
    {
        if(hit.Damage > 0)
        {
            flashUntil = Time.time + FlashDuration;
        }
    }
	
	void Update () 
	{
        if (Time.time > flashUntil)
        {
            if(rend.sprite == FlashSprite)
                rend.sprite = normSprite;
        }
        else
        {
            rend.sprite = (Mathf.RoundToInt(Time.time / FlashInterval) % 2 == 0) ?
                FlashSprite : normSprite;
        }
	}
}
