using UnityEngine;
using UnityEngine.Events;

// GameEvent
// Scripts can register functions with the the GameEvent asset
// Scripts can call the GameEvent's Raise() method

// The CreateAssetMenu attribute allows us to create scriptable object assets in the editor
// In the Editor: Right Click > Create > ScriptableObjects > Events > GameEvent
[CreateAssetMenu(fileName = "New GameEvent", menuName = "ScriptableObjects/Events/GameEvent")]
public class GameEvent : ScriptableObject
{
    // We use a unity event as it already has the infrastructure in place to register and call functions
    // With this event contained inside a scriptable object we will always be able to have it 
    // accept listeners and be invoked.
    private UnityEvent gameEvent;

    public void Raise()
    {
        gameEvent.Invoke();
    }

    public void RegisterListener(UnityAction listener)
    {
        gameEvent.AddListener(listener);
    }

    public void UnregisterListener(UnityAction listener)
    {
        gameEvent.RemoveListener(listener);
    }
}
