diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus.meta b/Assets/_DDD/_Scripts/GameFramework/EventBus.meta new file mode 100644 index 000000000..29eb753fb --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d9d81b77c0e15e5459af3bbbacf9c8b3 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus/EventBus.cs b/Assets/_DDD/_Scripts/GameFramework/EventBus/EventBus.cs new file mode 100644 index 000000000..15a8f5d2c --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus/EventBus.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace DDD +{ + public static class EventBus + { + private static readonly Dictionary> HandlerDictionary = new(); + + public static void Register(IEventHandler handler) where T : IEvent + { + var type = typeof(T); + if (!HandlerDictionary.ContainsKey(type)) + { + HandlerDictionary[type] = new List(); + } + + HandlerDictionary[type].Add(handler); + } + + public static void Unregister(IEventHandler handler) where T : IEvent + { + var type = typeof(T); + if (HandlerDictionary.TryGetValue(type, out var list)) + { + list.Remove(handler); + if (list.Count == 0) + { + HandlerDictionary.Remove(type); + } + } + } + + public static void Publish(T evt) where T : IEvent + { + var type = typeof(T); + if (HandlerDictionary.TryGetValue(type, out var list)) + { + foreach (var handler in list.Cast>()) + { + handler.Handle(evt); + } + } + } + + public static void ClearAll() + { + HandlerDictionary.Clear(); + } + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus/EventBus.cs.meta b/Assets/_DDD/_Scripts/GameFramework/EventBus/EventBus.cs.meta new file mode 100644 index 000000000..a745c2e40 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus/EventBus.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: e57263a6fcda2cb43a03e0b4efd4848e \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus/IEvent.cs b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEvent.cs new file mode 100644 index 000000000..b09e7bc5d --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEvent.cs @@ -0,0 +1,7 @@ +namespace DDD +{ + public interface IEvent + { + + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus/IEvent.cs.meta b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEvent.cs.meta new file mode 100644 index 000000000..f9181e193 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEvent.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: ffb2216f8e1a979449b7fb4caa575033 \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus/IEventHandler.cs b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEventHandler.cs new file mode 100644 index 000000000..eb26c0f31 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEventHandler.cs @@ -0,0 +1,7 @@ +namespace DDD +{ + public interface IEventHandler where T : IEvent + { + void Handle(T evt); + } +} \ No newline at end of file diff --git a/Assets/_DDD/_Scripts/GameFramework/EventBus/IEventHandler.cs.meta b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEventHandler.cs.meta new file mode 100644 index 000000000..74fdac7c0 --- /dev/null +++ b/Assets/_DDD/_Scripts/GameFramework/EventBus/IEventHandler.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: 0dd9940e2db491b4fae6d77773427b36 \ No newline at end of file