Yobi_Gochida

Updated Event Manager

Sep 16th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.85 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace Game.Events
  4. {
  5.     public enum EventType
  6.     {
  7.         CREATURE_DEATH,
  8.         CREATURE_CREATED,
  9.  
  10.     }
  11.  
  12.     public struct EventObject
  13.     {
  14.         /// <summary>
  15.         /// Access contents using an 'as' cast, and check if the result is null. If not null, cast was successful.
  16.         /// </summary>
  17.         public object[] parameters;
  18.     }
  19.  
  20.     /// <summary>
  21.     /// Registers event listeners. Registering objects must supply a unique ObjectId and a method to be called.
  22.     /// <para>To call an event, use the "Event" method </para>
  23.     /// <para>Singleton</para>
  24.     /// </summary>
  25.     public class EventManager
  26.     {
  27.         /// <summary>
  28.         /// stores a dictonary for each event type. Each dictionary contains all the methods to be invoked on that event type, key'd by objectId.
  29.         /// </summary>
  30.         Dictionary<EventType, List<System.Action<EventObject> > > callbackDictionary;
  31.  
  32.         private static EventManager _instance = null;
  33.         public static EventManager Instance
  34.         {
  35.             get
  36.             {
  37.                 if (_instance == null)
  38.                     _instance = new EventManager();
  39.                 return _instance;
  40.             }
  41.         }
  42.  
  43.         private EventManager()
  44.         {
  45.             callbackDictionary = new Dictionary<EventType, List<System.Action<EventObject>>>();
  46.         }
  47.  
  48.         /// <summary>
  49.         /// Sends an immediate event.
  50.         /// </summary>
  51.         /// <param name="eventType">The event to be triggered</param>
  52.         /// <param name="callerId">ID of the object triggering the event</param>
  53.         /// <param name="parameters">objects to be included with the event</param>
  54.         public void TriggerEvent(EventType eventType, params object[] parameters)
  55.         {
  56.             EventObject eObj;
  57.             eObj.parameters = parameters;
  58.  
  59.             List<System.Action<EventObject>> cbList;
  60.             if(callbackDictionary.TryGetValue(eventType, out cbList))
  61.             {
  62.                 for(int i = cbList.Count-1; i >= 0; i--)
  63.                 { //iterate with a for loop to prevent errors if a callback removes itself from the list during iteration
  64.                     cbList[i](eObj);
  65.                 }
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Registers an objects method to the Event Manager, under a specified EventType
  71.         /// </summary>
  72.         /// <param name="eType">The type of event that must be called to invoke the callback method</param>
  73.         /// <param name="objectId">The unique Id of the object registering to Event Manager.</param>
  74.         /// <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>
  75.         public void Register(EventType eType, System.Action<EventObject> callback)
  76.         {
  77.             List<System.Action<EventObject>> cbList;
  78.             if (callbackDictionary.TryGetValue(eType, out cbList) == false)
  79.             {
  80.                 cbList = new List<System.Action<EventObject>>();
  81.                 callbackDictionary.Add(eType, cbList);
  82.             }
  83.  
  84.             if (cbList.Contains(callback))
  85.             {
  86.                 throw new EventManagerException("This callback method already exists for this event type.");
  87.             }
  88.  
  89.             cbList.Add(callback);
  90.  
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Any object that has registered with the event manager, should unregister when it's done listening for this event
  95.         /// </summary>
  96.         /// <param name="eType">the event type the object needs to be unregistered from</param>
  97.         /// <param name="objectId">the object that will be unregistered</param>
  98.         public void UnRegister(EventType eType, System.Action<EventObject> callback)
  99.         {
  100.             List<System.Action<EventObject>> cbList;
  101.             if (callbackDictionary.TryGetValue(eType, out cbList) == false)
  102.                 return; //there was nothing to unregister
  103.  
  104.             if (cbList.Remove(callback))
  105.                 UnityEngine.Debug.Log("Successfully removed a delegate!");
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Will unregister the object from all events. This should be called prior to the deletion of an object.
  110.         /// </summary>
  111.         /// <param name="objectId"></param>
  112.         public void UnRegisterAll(System.Action<EventObject> callback)
  113.         {
  114.             foreach (KeyValuePair<EventType, List<System.Action<EventObject>>> kvp in callbackDictionary)
  115.             {
  116.                 kvp.Value.Remove(callback);
  117.             }
  118.         }
  119.  
  120.     }
  121.  
  122.     public class EventManagerException : System.Exception
  123.     {
  124.         public EventManagerException()
  125.         {
  126.         }
  127.  
  128.         public EventManagerException(string message) : base(message)
  129.         {
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment