Yobi_Gochida

Event Manager

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