Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- namespace Events
- {
- public enum EventType
- {
- CREATURE_DEATH,
- CREATURE_CREATED,
- }
- public struct EventObject
- {
- /// <summary>
- /// This is the unique objectId of the caller.
- /// </summary>
- public int callerId;
- /// <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, Dictionary<int, 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, Dictionary<int, System.Action<EventObject>>>();
- }
- public void Event(EventType eventType, int callerId, params object[] parameters)
- {
- EventObject eObj;
- eObj.callerId = callerId;
- eObj.parameters = parameters;
- Dictionary<int, System.Action<EventObject>> subDict;
- if(callbackDictionary.TryGetValue(eventType, out subDict))
- {
- //For future reference, I'm getting keys and iterating in this fashion because
- //the dictionary of callbacks can't be changed during the foreach loops iteration.
- //So for example, if a callback were to try to unregister during the loop
- //it would cause an 'Out of Sync' error.
- //This gets around it by not doing a foreach on the dictionary, but on a list of keys instead.
- List<int> keys = new List<int>(subDict.Keys);
- foreach(int key in keys)
- {
- System.Action<EventObject> callback;
- if (subDict.TryGetValue(key, out callback))
- callback(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, int objectId, System.Action<EventObject> callback)
- {
- Dictionary<int, System.Action<EventObject>> subDict;
- if (callbackDictionary.TryGetValue(eType, out subDict) == false)
- {
- subDict = new Dictionary<int, System.Action<EventObject>>();
- callbackDictionary.Add(eType, subDict);
- }
- if (subDict.ContainsKey(objectId))
- return;
- subDict.Add(objectId, 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, int objectId)
- {
- Dictionary<int, System.Action<EventObject>> subDict;
- if (callbackDictionary.TryGetValue(eType, out subDict) == false)
- return; //there was nothing to unregister
- if(subDict.ContainsKey(objectId))
- subDict.Remove(objectId);
- }
- /// <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(int objectId)
- {
- foreach (KeyValuePair<EventType, Dictionary<int, System.Action<EventObject>>> kvp in callbackDictionary)
- {
- if (kvp.Value.ContainsKey(objectId))
- kvp.Value.Remove(objectId);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment