using System; using System.Collections.Generic; using RPGCore.Core; using RPGCore.Core.Objects; namespace RPGCore.ObjectModules.EventObjectModule { [Serializable] public class EventModule : ObjectModule { // TODO: tutaj dodać serializowane eventy w postaci SerializableDictionary // TODO: dodatkowo mozna zmienić preventableEvents na dwa osobne słowniki before i after internal readonly Dictionary events = new(); internal readonly Dictionary preventableEvents = new(); public void Invoke(T baseEvent) where T : BaseEvent { events.TryAdd(typeof(T), null); (events[typeof(T)] as Action)?.Invoke(baseEvent); } public void Register(Action action) where T : BaseEvent { events.TryAdd(typeof(T), null); var temp = (Action)events[typeof(T)]; temp += action; events[typeof(T)] = temp; } public void Unregister(Action action) where T : BaseEvent { events.TryAdd(typeof(T), null); var temp = (Action)events[typeof(T)]; temp -= action; events[typeof(T)] = temp; } public void InvokeBefore(T basePreventableEvent) where T : BasePreventableEvent { preventableEvents.TryAdd(typeof(T), new Delegate[2]); (preventableEvents[typeof(T)][0] as Action)?.Invoke(basePreventableEvent); } public void RegisterBefore(Action action) where T : BasePreventableEvent { preventableEvents.TryAdd(typeof(T), new Delegate[2]); var temp = (Action)preventableEvents[typeof(T)][0]; temp += action; preventableEvents[typeof(T)][0] = temp; } public void UnregisterBefore(Action action) where T : BasePreventableEvent { preventableEvents.TryAdd(typeof(T), new Delegate[2]); var temp = (Action)preventableEvents[typeof(T)][0]; temp -= action; preventableEvents[typeof(T)][0] = temp; } public void InvokeAfter(T basePreventableEvent) where T : BasePreventableEvent { if (basePreventableEvent.isPrevented) throw new EventPreventedException("Event is prevented and can't be invoked!"); preventableEvents.TryAdd(typeof(T), new Delegate[2]); (preventableEvents[typeof(T)][1] as Action)?.Invoke(basePreventableEvent); } public void RegisterAfter(Action action) where T : BasePreventableEvent { preventableEvents.TryAdd(typeof(T), new Delegate[2]); var temp = (Action)preventableEvents[typeof(T)][1]; temp += action; preventableEvents[typeof(T)][1] = temp; } public void UnregisterAfter(Action action) where T : BasePreventableEvent { preventableEvents.TryAdd(typeof(T), new Delegate[2]); var temp = (Action)preventableEvents[typeof(T)][1]; temp -= action; preventableEvents[typeof(T)][1] = temp; } } }