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