Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace Game.Events
- {
- public enum EventType
- {
- CREATURE_DEATH,
- CREATURE_CREATED,
- }
- public struct EventObject
- {
- /// <summary>
- /// Access contents using an 'as' cast, and check if the result is null. If not null, cast was successful.
- /// </summary>
- public object[] parameters;
- }
- /// <summary>
- /// Registers event listeners. Registering objects must supply a unique ObjectId and a method to be called.
- /// <para>To call an event, use the "Event" method </para>
- /// <para>Singleton</para>
- /// </summary>
- public class EventManager
- {
- /// <summary>
- /// stores a dictonary for each event type. Each dictionary contains all the methods to be invoked on that event type, key'd by objectId.
- /// </summary>
- Dictionary<EventType, List<System.Action<EventObject> > > callbackDictionary;
- private static EventManager _instance = null;
- public static EventManager Instance
- {
- get
- {
- if (_instance == null)
- _instance = new EventManager();
- return _instance;
- }
- }
- private EventManager()
- {
- callbackDictionary = new Dictionary<EventType, List<System.Action<EventObject>>>();
- }
- /// <summary>
- /// Sends an immediate event.
- /// </summary>
- /// <param name="eventType">The event to be triggered</param>
- /// <param name="callerId">ID of the object triggering the event</param>
- /// <param name="parameters">objects to be included with the event</param>
- public void TriggerEvent(EventType eventType, params object[] parameters)
- {
- EventObject eObj;
- eObj.parameters = parameters;
- List<System.Action<EventObject>> cbList;
- if(callbackDictionary.TryGetValue(eventType, out cbList))
- {
- for(int i = cbList.Count-1; i >= 0; i--)
- { //iterate with a for loop to prevent errors if a callback removes itself from the list during iteration
- cbList[i](eObj);
- }
- }
- }
- /// <summary>
- /// Registers an objects method to the Event Manager, under a specified EventType
- /// </summary>
- /// <param name="eType">The type of event that must be called to invoke the callback method</param>
- /// <param name="objectId">The unique Id of the object registering to Event Manager.</param>
- /// <param name="callback">The method which will be invoked when an event of type eType is created. Must accept a single parameter of EventObject and return void.</param>
- public void Register(EventType eType, System.Action<EventObject> callback)
- {
- List<System.Action<EventObject>> cbList;
- if (callbackDictionary.TryGetValue(eType, out cbList) == false)
- {
- cbList = new List<System.Action<EventObject>>();
- callbackDictionary.Add(eType, cbList);
- }
- if (cbList.Contains(callback))
- {
- throw new EventManagerException("This callback method already exists for this event type.");
- }
- cbList.Add(callback);
- }
- /// <summary>
- /// Any object that has registered with the event manager, should unregister when it's done listening for this event
- /// </summary>
- /// <param name="eType">the event type the object needs to be unregistered from</param>
- /// <param name="objectId">the object that will be unregistered</param>
- public void UnRegister(EventType eType, System.Action<EventObject> callback)
- {
- List<System.Action<EventObject>> cbList;
- if (callbackDictionary.TryGetValue(eType, out cbList) == false)
- return; //there was nothing to unregister
- if (cbList.Remove(callback))
- UnityEngine.Debug.Log("Successfully removed a delegate!");
- }
- /// <summary>
- /// Will unregister the object from all events. This should be called prior to the deletion of an object.
- /// </summary>
- /// <param name="objectId"></param>
- public void UnRegisterAll(System.Action<EventObject> callback)
- {
- foreach (KeyValuePair<EventType, List<System.Action<EventObject>>> kvp in callbackDictionary)
- {
- kvp.Value.Remove(callback);
- }
- }
- }
- public class EventManagerException : System.Exception
- {
- public EventManagerException()
- {
- }
- public EventManagerException(string message) : base(message)
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment