91 lines
3.5 KiB
C#
91 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using RPGCore.Core;
|
|
using RPGCore.Core.Objects;
|
|
using UnityEngine;
|
|
|
|
namespace RPGCore.ObjectModules.EventObjectModule
|
|
{
|
|
[Serializable]
|
|
[ObjectModule(
|
|
name: "[Core] Data",
|
|
description: "[Built-in module] Manages all implementations of <b>BaseEvent</b> and <b>BasePreventableEvent</b>. " +
|
|
"Allows for registering and invoking events by its type.",
|
|
required: true
|
|
)]
|
|
public class EventModule : ObjectModule<BaseObject>
|
|
{
|
|
// TODO: 1. tutaj dodać serializowane eventy w postaci SerializableDictionary<SerializableType, UnityAction>
|
|
// TODO: w Awake() zrobić bridge to uruchamiania serializowanych eventów jak uruchamia się ten runtimeowy
|
|
// TODO: 2. 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();
|
|
|
|
/***** INVOKE *****/
|
|
|
|
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);
|
|
events[typeof(T)] = (Action<T>)events[typeof(T)] + action;
|
|
}
|
|
|
|
public void Unregister<T>(Action<T> action) where T : BaseEvent
|
|
{
|
|
events.TryAdd(typeof(T), null);
|
|
events[typeof(T)] = (Action<T>)events[typeof(T)] - action;
|
|
}
|
|
|
|
/***** INVOKE BEFORE *****/
|
|
|
|
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]);
|
|
preventableEvents[typeof(T)][0] = (Action<T>)preventableEvents[typeof(T)][0] + action;
|
|
}
|
|
|
|
public void UnregisterBefore<T>(Action<T> action) where T : BasePreventableEvent
|
|
{
|
|
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
|
preventableEvents[typeof(T)][0] = (Action<T>)preventableEvents[typeof(T)][0] - action;
|
|
}
|
|
|
|
/***** INVOKE AFTER *****/
|
|
|
|
public void InvokeAfter<T>(T basePreventableEvent) where T : BasePreventableEvent
|
|
{
|
|
if (basePreventableEvent.isPrevented)
|
|
{
|
|
Debug.LogWarning("Event is prevented and can't be invoked!");
|
|
return;
|
|
}
|
|
|
|
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]);
|
|
preventableEvents[typeof(T)][1] = (Action<T>)preventableEvents[typeof(T)][1] + action;
|
|
}
|
|
|
|
public void UnregisterAfter<T>(Action<T> action) where T : BasePreventableEvent
|
|
{
|
|
preventableEvents.TryAdd(typeof(T), new Delegate[2]);
|
|
preventableEvents[typeof(T)][1] = (Action<T>)preventableEvents[typeof(T)][1] - action;
|
|
}
|
|
}
|
|
} |