using System; using System.Collections.Generic; namespace Unity.FPS.Game { public class GameEvent { } // A simple Event System that can be used for remote systems communication public static class EventManager { static readonly Dictionary> s_Events = new Dictionary>(); static readonly Dictionary> s_EventLookups = new Dictionary>(); public static void AddListener(Action evt) where T : GameEvent { if (!s_EventLookups.ContainsKey(evt)) { Action newAction = (e) => evt((T) e); s_EventLookups[evt] = newAction; if (s_Events.TryGetValue(typeof(T), out Action internalAction)) s_Events[typeof(T)] = internalAction += newAction; else s_Events[typeof(T)] = newAction; } } public static void RemoveListener(Action evt) where T : GameEvent { if (s_EventLookups.TryGetValue(evt, out var action)) { if (s_Events.TryGetValue(typeof(T), out var tempAction)) { tempAction -= action; if (tempAction == null) s_Events.Remove(typeof(T)); else s_Events[typeof(T)] = tempAction; } s_EventLookups.Remove(evt); } } public static void Broadcast(GameEvent evt) { if (s_Events.TryGetValue(evt.GetType(), out var action)) action.Invoke(evt); } public static void Clear() { s_Events.Clear(); s_EventLookups.Clear(); } } }