init
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RPGCore.Core;
|
||||
using RPGCore.Core.Objects;
|
||||
|
||||
namespace RPGCore.ObjectModules.EventObjectModule
|
||||
{
|
||||
[Serializable]
|
||||
public class EventModule : ObjectModule<BaseObject>
|
||||
{
|
||||
// TODO: tutaj dodać serializowane eventy w postaci SerializableDictionary<SerializableType, UnityAction>
|
||||
// TODO: dodatkowo mozna zmienić preventableEvents na dwa osobne słowniki before i after
|
||||
|
||||
internal readonly Dictionary<Type, Delegate> events = new();
|
||||
internal readonly Dictionary<Type, Delegate[]> preventableEvents = new();
|
||||
|
||||
public void Invoke<T>(T baseEvent) where T : BaseEvent
|
||||
{
|
||||
events.TryAdd(typeof(T), null);
|
||||
(events[typeof(T)] as Action<T>)?.Invoke(baseEvent);
|
||||
}
|
||||
|
||||
public void Register<T>(Action<T> action) where T : BaseEvent
|
||||
{
|
||||
events.TryAdd(typeof(T), null);
|
||||
var temp = (Action<T>)events[typeof(T)];
|
||||
temp += action;
|
||||
events[typeof(T)] = temp;
|
||||
}
|
||||
|
||||
public void Unregister<T>(Action<T> action) where T : BaseEvent
|
||||
{
|
||||
events.TryAdd(typeof(T), null);
|
||||
var temp = (Action<T>)events[typeof(T)];
|
||||
temp -= action;
|
||||
events[typeof(T)] = temp;
|
||||
}
|
||||
|
||||
public void InvokeBefore<T>(T basePreventableEvent) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
(preventableEvents[typeof(T)][0] as Action<T>)?.Invoke(basePreventableEvent);
|
||||
}
|
||||
|
||||
public void RegisterBefore<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][0];
|
||||
temp += action;
|
||||
preventableEvents[typeof(T)][0] = temp;
|
||||
}
|
||||
|
||||
public void UnregisterBefore<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][0];
|
||||
temp -= action;
|
||||
preventableEvents[typeof(T)][0] = temp;
|
||||
}
|
||||
|
||||
public void InvokeAfter<T>(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<T>)?.Invoke(basePreventableEvent);
|
||||
}
|
||||
|
||||
public void RegisterAfter<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][1];
|
||||
temp += action;
|
||||
preventableEvents[typeof(T)][1] = temp;
|
||||
}
|
||||
|
||||
public void UnregisterAfter<T>(Action<T> action) where T : BasePreventableEvent
|
||||
{
|
||||
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
||||
var temp = (Action<T>)preventableEvents[typeof(T)][1];
|
||||
temp -= action;
|
||||
preventableEvents[typeof(T)][1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user