mirror of
https://gitee.com/jisol/jisol-game/
synced 2025-09-27 02:36:14 +00:00
提交
This commit is contained in:
@@ -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();
|
||||
|
Reference in New Issue
Block a user