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

[RequireComponent(typeof(CircleCollider2D))]
public class SwitchTrigger : MonoBehaviour
{
    CircleCollider2D m_switchTrigger;
    int m_numTimesTriggered = 0;

    public delegate void DispatchOnTriggerStart();
    public DispatchOnTriggerStart disptachOnTriggerStart;

    public delegate void DispatchOnTriggerStop();
    public DispatchOnTriggerStop disptachOnTriggerStop;

    void Awake()
    {
        m_switchTrigger = GetComponent<CircleCollider2D>();
        m_switchTrigger.isTrigger = true;
    }

	// Use this for initialization
	void Start ()
    {
		
	}
	
	void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "SwitchTrigger")
        {
            m_numTimesTriggered++;
            if (m_numTimesTriggered == 1)
            {
                disptachOnTriggerStart();
            }
        }
    }

	void OnTriggerExit2D(Collider2D other)
    {
        if(other.tag == "SwitchTrigger")
        {
            m_numTimesTriggered--;
            if(m_numTimesTriggered == 0)
            {
                disptachOnTriggerStop();
            }
        }
    }

    public bool IsTriggered()
    {
        return m_numTimesTriggered > 0;
    }
}
