This commit is contained in:
PC-20230316NUNE\Administrator
2024-01-29 19:07:52 +08:00
parent 01293d9c30
commit 09db51f67b
62 changed files with 17325 additions and 1910 deletions

View File

@@ -9,6 +9,9 @@ namespace Plugins.JNGame.Util
/// </summary>
public class EventDispatcher
{
public static readonly EventDispatcher Event = new EventDispatcher();
public Dictionary<string, List<Delegate>> EventHandlers { get; private set; } = new();
/// <summary>
@@ -16,7 +19,7 @@ namespace Plugins.JNGame.Util
/// </summary>
/// <param name="eventId">事件标识符</param>
/// <param name="listener">事件监听器</param>
public void AddListener(string eventId, Delegate listener)
public void AddListener(string eventId, Action listener)
{
if (!EventHandlers.ContainsKey(eventId))
{
@@ -81,6 +84,21 @@ namespace Plugins.JNGame.Util
}
}
public void Dispatch(string eventId)
{
if (EventHandlers.ContainsKey(eventId) && EventHandlers[eventId] != null)
{
foreach(Delegate fun in EventHandlers[eventId])
{
// 确保 fun 实际上是指向一个 Action<T> 类型的函数
if (fun.Method.GetParameters().Length == 0)
{
((Action)fun)();
}
}
}
}
public void Reset()
{
EventHandlers = new();