Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / event / EventManager.cs
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using Godot;
  5.  
  6. /// <summary>
  7. /// 事件管理器
  8. /// </summary>
  9. public static class EventManager
  10. {
  11.  
  12. private static readonly Dictionary<EventEnum, List<EventBinder>> _eventMap =
  13. new Dictionary<EventEnum, List<EventBinder>>();
  14.  
  15. /// <summary>
  16. /// 添加监听事件, 并返回事件绑定对象
  17. /// </summary>
  18. /// <param name="eventType">监听事件类型</param>
  19. /// <param name="callback">回调函数</param>
  20. public static EventBinder AddEventListener(EventEnum eventType, Action<object> callback)
  21. {
  22. EventBinder binder;
  23. if (!_eventMap.TryGetValue(eventType, out var list))
  24. {
  25. list = new List<EventBinder>();
  26. _eventMap.Add(eventType, list);
  27. binder = new EventBinder(eventType, callback);
  28. list.Add(binder);
  29. return binder;
  30. }
  31.  
  32. for (var i = 0; i < list.Count; i++)
  33. {
  34. var item = list[i];
  35. if (item.Callback == callback)
  36. {
  37. return item;
  38. }
  39. }
  40.  
  41. binder = new EventBinder(eventType, callback);
  42. list.Add(binder);
  43. return binder;
  44. }
  45.  
  46. /// <summary>
  47. /// 派发事件
  48. /// </summary>
  49. /// <param name="eventType">事件类型</param>
  50. /// <param name="arg">传递参数</param>
  51. public static void EmitEvent(EventEnum eventType, object arg = null)
  52. {
  53. if (_eventMap.TryGetValue(eventType, out var list))
  54. {
  55. var binders = list.ToArray();
  56. for (var i = 0; i < binders.Length; i++)
  57. {
  58. var binder = binders[i];
  59. if (!binder.IsDiscard)
  60. {
  61. try
  62. {
  63. binder.Callback(arg);
  64. }
  65. catch (Exception e)
  66. {
  67. GD.PrintErr($"EventManager 派发事件: '{eventType}' 发生异常: \n" + e.Message + "\n" + e.StackTrace);
  68. }
  69. }
  70. }
  71. }
  72. }
  73.  
  74. /// <summary>
  75. /// 根据事件绑定对象移除一个监听事件
  76. /// </summary>
  77. public static void RemoveEventListener(EventBinder binder)
  78. {
  79. if (_eventMap.TryGetValue(binder.EventType, out var list))
  80. {
  81. if (list.Remove(binder))
  82. {
  83. binder.IsDiscard = true;
  84. if (list.Count == 0)
  85. {
  86. _eventMap.Remove(binder.EventType);
  87. }
  88. }
  89. }
  90. }
  91.  
  92. /// <summary>
  93. /// 移除指定类型的所有事件
  94. /// </summary>
  95. public static void RemoveAllEventListener(EventEnum eventType)
  96. {
  97. if (_eventMap.TryGetValue(eventType, out var list))
  98. {
  99. foreach (var binder in list)
  100. {
  101. binder.IsDiscard = true;
  102. }
  103.  
  104. _eventMap.Remove(eventType);
  105. }
  106. }
  107.  
  108. /// <summary>
  109. /// 移除所有监听事件
  110. /// </summary>
  111. public static void ClearAllEventListener()
  112. {
  113. foreach (var kv in _eventMap)
  114. {
  115. foreach (var binder in kv.Value)
  116. {
  117. binder.IsDiscard = true;
  118. }
  119. }
  120.  
  121. _eventMap.Clear();
  122. }
  123.  
  124. /// <summary>
  125. /// 创建一个事件工厂
  126. /// </summary>
  127. public static EventFactory CreateEventFactory()
  128. {
  129. return new EventFactory();
  130. }
  131. }