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

public class Flickerer : MonoBehaviour {

    public float amplitude = .3f;
    public float frequency = 2f;
    private float speed;
    public float origin = .6f;
    private float phase = 0f;
    public Color originalColor;
    private SpriteRenderer mr;
    public System.Action callback;

    private void Awake()
    {
        mr = GetComponent<SpriteRenderer>();
        originalColor = mr.material.color;
        speed = Mathf.PI * 2f * frequency;
    }

    void Update()
    {
        phase += speed * Time.deltaTime;
        mr.color = new Color(originalColor.r, originalColor.g, originalColor.b, origin + amplitude * (Mathf.Sin(phase)));
    }

    void OnDestroy()
    {
        if(callback != null)
            callback();
    }
}
