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

namespace Machete {
	public class Ticker : MonoBehaviour {

    // TODO: make ticker non singleton
    public static Ticker Instance {
      get { return instance; }
    }
    static Ticker instance;

    [SerializeField]
    private float secondsPerTick = 0.5f;

    private float timePassed = 0f;

    public delegate void TickEvent();
    public event TickEvent OnTick;

    private bool stopped = false;

    public void ToggleStop(bool stop) {
      stopped = stop;
    }

    public void NextStep() {
      stopped = true;
      Tick();
    }

    void Awake() {
      // Singletong it
      if (instance != null) {
        Destroy(gameObject);
      }
      instance = this;
    }

		// Update is called once per frame
		void Update () {

      if (stopped) {
        return;
      }
      // Wait until tick.
      timePassed += Time.deltaTime;
      if (timePassed > secondsPerTick) {
        Tick();
      }
		}

    void Tick() {
      timePassed -= secondsPerTick;
      if (OnTick != null) {
        OnTick();
      }
    }
	}
}
