using UnityEngine;
using System.Collections;
using UnityEngine.Events;

public class TriggerEvent : MonoBehaviour
{
    public bool oneTimeUse;
    public bool shouldPressKey = true;
    public KeyCode key = KeyCode.E;

    public bool useTag;
    public string[] tags;

    public UnityEvent onEnter;
    public UnityEvent onStay;
    public UnityEvent onExit;

    bool triggeredEvent = false;

    void Start()
    {

    }

    void OnTriggerEnter2D(Collider2D other) { DoEvent(other.tag, onEnter); }
    void OnTriggerStay2D (Collider2D other) { DoEvent(other.tag, onStay);  }
    void OnTriggerExit2D (Collider2D other) { DoEvent(other.tag, onExit);  }

    void DoEvent(string tag, UnityEvent unityEvent)
    {
        if (oneTimeUse && !triggeredEvent ||!oneTimeUse)
        {
            if (!shouldPressKey || Input.GetKeyDown(key))
            {
                triggeredEvent = true;
                if (useTag)
                {
                    for (int i = 0; i < tags.Length; i++)
                    {
                        if (tags[i] == tag)
                        {
                            if (unityEvent != null) unityEvent.Invoke();
                            break;
                        }
                    }
                }
                else
                {
                    if (unityEvent != null) unityEvent.Invoke();
                }
                print("triggered event");
            }
        }
    }
}
