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

public class ButtonScript : MonoBehaviour
{

    public Sprite OnSprite;
    public Sprite OffSprite;

    public bool IsFlashing = false;

    [Range(0, 5)]
    public float FlashingRate = 1;
    private float flashingTicks = 0;

    public bool IsOn = false;
    SpriteRenderer _spriteRenderer;

    void Start()
    {
        _spriteRenderer = GetComponent<SpriteRenderer>();
        _spriteRenderer.sprite = (IsOn ? OnSprite : OffSprite);
    }

    public void changeState(bool newState)
    {
        if (newState != IsOn)
        {
            IsOn = newState;
            _spriteRenderer.sprite = (IsOn ? OnSprite : OffSprite);
        }
    }   

    public void setStatus(bool on)
    {
        changeState(on);
    }

    private void Update()
    {
        flashingTicks += Time.deltaTime;

        if (IsFlashing && flashingTicks > FlashingRate)
        {
            changeState(!IsOn);
            flashingTicks = 0;
        }
    }
}
