using UnityEngine;
using System.Collections;

public class Flicker : MonoBehaviour {
    private bool on;
    private Color orig;
    private int currColor;
    public int coolDown;
    private int ticksPassed;
    private SpriteRenderer spriteRenderer;
    public Color[] flash;
	// Use this for initialization
	void Start () {
        this.spriteRenderer = this.gameObject.GetComponent<SpriteRenderer>();
        if (this.spriteRenderer.color != null) {
            orig = this.spriteRenderer.color;
        }
        on = false;
        ticksPassed = 0;
        currColor = 0;
	}
	
	// Update is called once per frame
	void FixedUpdate () {
        if (on) {
            if (ticksPassed >= coolDown) {
                on = false;
                this.spriteRenderer.color = orig;
            } else {
                if (ticksPassed % 10 == 0) {
                    this.spriteRenderer.color = flash[currColor];
                    currColor = (currColor + 1) % flash.Length;
                }
                ticksPassed++;
            }
        }
	}
    void OnTriggerEnter2D(Collider2D coll) {
        if ( orig != null && flash.Length > 0 ) {
            on = true;
            ticksPassed = 0;
            currColor = 0;
        }
    }
}
