-
-
Save KonTrax/fe4c49fdde3a3cdf4bac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
public class GameEvent | |
{ | |
} | |
public class Events | |
{ | |
static Events instanceInternal = null; | |
public static Events instance | |
{ | |
get | |
{ | |
if (instanceInternal == null) | |
{ | |
instanceInternal = new Events(); | |
} | |
return instanceInternal; | |
} | |
} | |
public delegate void EventDelegate<T> (T e) where T : GameEvent; | |
private delegate void EventDelegate (GameEvent e); | |
private Dictionary<System.Type, EventDelegate> delegates = new Dictionary<System.Type, EventDelegate>(); | |
private Dictionary<System.Delegate, EventDelegate> delegateLookup = new Dictionary<System.Delegate, EventDelegate>(); | |
public void AddListener<T> (EventDelegate<T> del) where T : GameEvent | |
{ | |
// Early-out if we've already registered this delegate | |
if (delegateLookup.ContainsKey(del)) | |
return; | |
// Create a new non-generic delegate which calls our generic one. | |
// This is the delegate we actually invoke. | |
EventDelegate internalDelegate = (e) => del((T)e); | |
delegateLookup[del] = internalDelegate; | |
EventDelegate tempDel; | |
if (delegates.TryGetValue(typeof(T), out tempDel)) | |
{ | |
delegates[typeof(T)] = tempDel += internalDelegate; | |
} | |
else | |
{ | |
delegates[typeof(T)] = internalDelegate; | |
} | |
} | |
public void RemoveListener<T> (EventDelegate<T> del) where T : GameEvent | |
{ | |
EventDelegate internalDelegate; | |
if (delegateLookup.TryGetValue(del, out internalDelegate)) | |
{ | |
EventDelegate tempDel; | |
if (delegates.TryGetValue(typeof(T), out tempDel)) | |
{ | |
tempDel -= internalDelegate; | |
if (tempDel == null) | |
{ | |
delegates.Remove(typeof(T)); | |
} | |
else | |
{ | |
delegates[typeof(T)] = tempDel; | |
} | |
} | |
delegateLookup.Remove(del); | |
} | |
} | |
public void Raise (GameEvent e) | |
{ | |
EventDelegate del; | |
if (delegates.TryGetValue(e.GetType(), out del)) | |
{ | |
del.Invoke(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment