diff options
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools')
15 files changed, 394 insertions, 44 deletions
diff --git a/WorldlineKeepers/Assets/Scripts/Tools/FlagManager.cs b/WorldlineKeepers/Assets/Scripts/Tools/FlagManager.cs new file mode 100644 index 0000000..b9a9454 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/FlagManager.cs @@ -0,0 +1,44 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WK +{ + + /// <summary> + /// 全局标志管理 + /// </summary> + public class FlagManager : Singleton<FlagManager> + { + public Dictionary<string, bool> m_Flags; + + public void AddFlag(string flag, bool value = false) + { + if(!HasFlag(flag)) + { + m_Flags.Add(flag, value); + } + } + + public bool HasFlag(string flag) + { + return m_Flags.ContainsKey(flag); + } + + public bool IsFlag(string flag) + { + if(m_Flags.ContainsKey(flag)) return false; + return m_Flags[flag]; + } + + public void RemoveFlag(string flag) + { + if(HasFlag(flag)) + { + m_Flags.Remove(flag); + } + } + + } + +} diff --git a/WorldlineKeepers/Assets/Scripts/Tools/FlagManager.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/FlagManager.cs.meta new file mode 100644 index 0000000..1738634 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/FlagManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2451904bd85094c40a69d59807d51c5e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs b/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs index 47f3990..c9cde83 100644 --- a/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs +++ b/WorldlineKeepers/Assets/Scripts/Tools/GlobalEventManager.cs @@ -6,7 +6,7 @@ using UnityEngine.UIElements; namespace WK { /// <summary> - /// 全局事件 + /// 不指定发送者的全局事件 /// </summary> public class GlobalEventManager : Singleton<GlobalEventManager> { diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification.meta b/WorldlineKeepers/Assets/Scripts/Tools/Notification.meta new file mode 100644 index 0000000..b20b283 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 12f4a3b409f294746a9546c44272660c +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationCenter.cs index b3f299a..bb4e08e 100644 --- a/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationCenter.cs @@ -5,24 +5,23 @@ using UnityEngine; namespace WK { - public class NotificationCenter + public class NotificationCenter : Singleton<NotificationCenter> { - private Dictionary<string, Dictionary<object, List<Action<object, object>>>> _table = new Dictionary<string, Dictionary<object, List<Action<object, object>>>>(); + public delegate void NotificatonHandler(params object[] args); - private HashSet<List<Action<object, object>>> _invoking = new HashSet<List<Action<object, object>>>(); + private Dictionary<string/*eventName*/, Dictionary<object/*publisher*/, List<NotificatonHandler>>> m_EventListeners = new Dictionary<string, Dictionary<object, List<NotificatonHandler>>>(); - public static readonly NotificationCenter instance = new NotificationCenter(); + /// <summary> + /// 当前在调用中的回调 + /// </summary> + private HashSet<List<NotificatonHandler>> m_CurInvokingCallbacks = new HashSet<List<NotificatonHandler>>(); - private NotificationCenter() + public void AddObserver(string notificationName, NotificatonHandler handler) { + AddObserver(null, notificationName, handler); } - public void AddObserver(Action<object, object> handler, string notificationName) - { - AddObserver(handler, notificationName, null); - } - - public void AddObserver(Action<object, object> handler, string notificationName, object sender) + public void AddObserver(object sender, string notificationName, NotificatonHandler handler) { if (handler == null) { @@ -34,30 +33,30 @@ namespace WK Debug.LogError("Can't observe an unnamed notification"); return; } - if (!_table.ContainsKey(notificationName)) + if (!m_EventListeners.ContainsKey(notificationName)) { - _table.Add(notificationName, new Dictionary<object, List<Action<object, object>>>()); + m_EventListeners.Add(notificationName, new Dictionary<object, List<NotificatonHandler>>()); } - Dictionary<object, List<Action<object, object>>> dictionary = _table[notificationName]; + Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[notificationName]; object key = ((sender != null) ? sender : this); if (!dictionary.ContainsKey(key)) { - dictionary.Add(key, new List<Action<object, object>>()); + dictionary.Add(key, new List<NotificatonHandler>()); } - List<Action<object, object>> list = dictionary[key]; - if (_invoking.Contains(list)) + List<NotificatonHandler> list = dictionary[key]; + if (m_CurInvokingCallbacks.Contains(list)) { - list = (dictionary[key] = new List<Action<object, object>>(list)); + list = (dictionary[key] = new List<NotificatonHandler>(list)); } list.Add(handler); } - public void RemoveObserver(Action<object, object> handler, string notificationName) + public void RemoveObserver(string notificationName, NotificatonHandler handler) { - RemoveObserver(handler, notificationName, null); + RemoveObserver(null, notificationName, handler); } - public void RemoveObserver(Action<object, object> handler, string notificationName, object sender) + public void RemoveObserver(object sender, string notificationName, NotificatonHandler handler) { if (handler == null) { @@ -69,23 +68,23 @@ namespace WK } else { - if (!_table.ContainsKey(notificationName)) + if (!m_EventListeners.ContainsKey(notificationName)) { return; } - Dictionary<object, List<Action<object, object>>> dictionary = _table[notificationName]; + Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[notificationName]; object key = ((sender != null) ? sender : this); if (!dictionary.ContainsKey(key)) { return; } - List<Action<object, object>> list = dictionary[key]; + List<NotificatonHandler> list = dictionary[key]; int num = list.IndexOf(handler); if (num != -1) { - if (_invoking.Contains(list)) + if (m_CurInvokingCallbacks.Contains(list)) { - list = (dictionary[key] = new List<Action<object, object>>(list)); + list = (dictionary[key] = new List<NotificatonHandler>(list)); } list.RemoveAt(num); } @@ -94,12 +93,12 @@ namespace WK public void Clean() { - string[] array = new string[_table.Keys.Count]; - _table.Keys.CopyTo(array, 0); + string[] array = new string[m_EventListeners.Keys.Count]; + m_EventListeners.Keys.CopyTo(array, 0); for (int num = array.Length - 1; num >= 0; num--) { string key = array[num]; - Dictionary<object, List<Action<object, object>>> dictionary = _table[key]; + Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[key]; object[] array2 = new object[dictionary.Keys.Count]; dictionary.Keys.CopyTo(array2, 0); for (int num2 = array2.Length - 1; num2 >= 0; num2--) @@ -112,7 +111,7 @@ namespace WK } if (dictionary.Count == 0) { - _table.Remove(key); + m_EventListeners.Remove(key); } } } @@ -122,12 +121,12 @@ namespace WK PostNotification(notificationName, null); } - public void PostNotification(string notificationName, object sender) + public void PostNotification(object sender, string notificationName) { - PostNotification(notificationName, sender, null); + PostNotification(sender, notificationName, null); } - public void PostNotification(string notificationName, object sender, object e) + public void PostNotification(object sender, string notificationName, params object[] p) { if (string.IsNullOrEmpty(notificationName)) { @@ -135,33 +134,34 @@ namespace WK } else { - if (!_table.ContainsKey(notificationName)) + if (!m_EventListeners.ContainsKey(notificationName)) { return; } - Dictionary<object, List<Action<object, object>>> dictionary = _table[notificationName]; + Dictionary<object, List<NotificatonHandler>> dictionary = m_EventListeners[notificationName]; if (sender != null && dictionary.ContainsKey(sender)) { - List<Action<object, object>> list = dictionary[sender]; - _invoking.Add(list); + List<NotificatonHandler> list = dictionary[sender]; + m_CurInvokingCallbacks.Add(list); for (int i = 0; i < list.Count; i++) { - list[i](sender, e); + list[i](p); } - _invoking.Remove(list); + m_CurInvokingCallbacks.Remove(list); } if (dictionary.ContainsKey(this)) { - List<Action<object, object>> list2 = dictionary[this]; - _invoking.Add(list2); + List<NotificatonHandler> list2 = dictionary[this]; + m_CurInvokingCallbacks.Add(list2); for (int j = 0; j < list2.Count; j++) { - list2[j](sender, e); + list2[j](p); } - _invoking.Remove(list2); + m_CurInvokingCallbacks.Remove(list2); } } } + } } diff --git a/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationCenter.cs.meta index 13d54ee..13d54ee 100644 --- a/WorldlineKeepers/Assets/Scripts/Tools/NotificationCenter.cs.meta +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationCenter.cs.meta diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationExtensions.cs b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationExtensions.cs new file mode 100644 index 0000000..8313f7b --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationExtensions.cs @@ -0,0 +1,17 @@ +using System; + +namespace WK +{ + + public interface INotification + { + } + + public static class NotificationExtensions + { + public static void AddObserver(this INotification _this, string msg, NotificationCenter.NotificatonHandler handler) { NotificationCenter.Instance.AddObserver(_this, msg, handler); } + public static void RemoveObserver(this INotification _this, string msg, NotificationCenter.NotificatonHandler handler) { NotificationCenter.Instance.RemoveObserver(_this, msg, handler); } + public static void PostNotification(this INotification _this, string msg, params object[] p) { NotificationCenter.Instance.PostNotification(_this, msg, p); } + + } +} diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationExtensions.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationExtensions.cs.meta new file mode 100644 index 0000000..a805d3a --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/NotificationExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 75fb0f8661eca5c4faf394f140fcae88 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs new file mode 100644 index 0000000..0002f14 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs @@ -0,0 +1,89 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WK +{ + + public class ScopedNotification + { + public delegate void NotificatonHandler(params object[] args); + + private Dictionary<string, List<NotificatonHandler>> m_EventListeners = new Dictionary<string, List<NotificatonHandler>>(); + + public void AddObserver(string eventName, NotificatonHandler handler) + { + if (handler == null) + { + return; + } + + if (string.IsNullOrEmpty(eventName)) + { + return; + } + + List<NotificatonHandler> handlers; + if (!m_EventListeners.ContainsKey(eventName)) + { + m_EventListeners.Add(eventName, new List<NotificatonHandler>()); + } + + handlers = m_EventListeners[eventName]; + handlers.Add(handler); + } + + public void RemoveObserver(string eventName, NotificatonHandler handler) + { + if(handler == null) { return; } + + if(string.IsNullOrEmpty(eventName)) { return; } + + if (!m_EventListeners.ContainsKey(eventName)) + return; + + List<NotificatonHandler> handlers = m_EventListeners[eventName]; + if(handlers.Contains(handler)) + handlers.Remove(handler); + } + + public void RemoveEvent(string eventName) + { + if (string.IsNullOrEmpty(eventName)) { return; } + if(m_EventListeners.ContainsKey(eventName)) + { + m_EventListeners.Remove(eventName); + } + } + + public void Clean() + { + m_EventListeners.Clear(); + } + + public void PostNotification(string eventName, params object[] args) + { + if (string.IsNullOrEmpty(eventName)) { return; } + + if (!m_EventListeners.ContainsKey(eventName)) + return; + + List<NotificatonHandler> handlers = m_EventListeners[eventName]; + for(int i = 0; i < handlers.Count; i++) + { + var handler = handlers[i]; + if(handler != null) + { + handler(args); + } + } + } + + public void PostNotification(string eventName) + { + PostNotification(eventName, null); + } + + } + +}
\ No newline at end of file diff --git a/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs.meta new file mode 100644 index 0000000..4ec5672 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/Notification/ScopedNotification.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 66ddfe88aa791154ea0663b56917da4a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem.meta b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem.meta new file mode 100644 index 0000000..a6922dc --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 035c400d50feb2747bb551b48ff42f8f +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/ActiveTrigger.cs b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/ActiveTrigger.cs new file mode 100644 index 0000000..1a4e988 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/ActiveTrigger.cs @@ -0,0 +1,19 @@ +using Newtonsoft.Json.Serialization; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace WK +{ + + /// <summary> + /// 定期轮训条件的主动式触发器。不安全 + /// </summary> + public class ActiveTrigger + { + + + + } + +} diff --git a/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/ActiveTrigger.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/ActiveTrigger.cs.meta new file mode 100644 index 0000000..1fa37dc --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/ActiveTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6e06c17e867b36f4bb0b6796ed74b89b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs new file mode 100644 index 0000000..ed69284 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs @@ -0,0 +1,110 @@ +using System.Collections.Generic; +using System.Diagnostics.Tracing; + +namespace WK +{ + + /// <summary> + /// 事件发生后检测条件的被动触发器 + /// </summary> + public class Trigger + { + public class ConditionBase + { + public virtual bool Evaluate(params object[] args) { return false; } + + public static ConditionBase Always = new ConditionAlways(); + public static ConditionBase AlwaysNot = new ConditionAlwaysNot(); + } + + public class ConditionAlways : ConditionBase + { + public override bool Evaluate(params object[] args) + { + return true; + } + } + + public class ConditionAlwaysNot : ConditionBase + { + public override bool Evaluate(params object[] args) + { + return false; + } + } + + public class ConditionAnd : ConditionBase + { + private ConditionBase m_Left; + private ConditionBase m_Right; + public ConditionAnd(ConditionBase left, ConditionBase right) + { + m_Left = left; + m_Right = right; + } + public override bool Evaluate(params object[] args) + { + return m_Left.Evaluate(args) && m_Right.Evaluate(args); + } + } + + public class ConditionOr : ConditionBase + { + private ConditionBase m_Left; + private ConditionBase m_Right; + public ConditionOr(ConditionBase left, ConditionBase right) + { + m_Left = left; + m_Right = right; + } + public override bool Evaluate(params object[] args) + { + return m_Left.Evaluate(args) || m_Right.Evaluate(args); + } + } + + public delegate void Action(params object[] args); + + // 触发事件 + private string m_Event; + // 触发条件 + private ConditionBase m_Condition; + // 触发操作 + private List<Action> m_Actions = new List<Action>(); + + public void SetEvent(string eventType) + { + if (m_Event != string.Empty || m_Event != null) + { + GlobalEventManager.Instance.UnRegister(m_Event, OnEvent); + } + m_Event = eventType; + GlobalEventManager.Instance.Register(eventType, OnEvent); + } + + public void AddAction(Action action) + { + m_Actions.Add(action); + } + + public void SetCondition(ConditionBase condition) + { + m_Condition = condition; + } + + public void OnEvent(params object[] args) + { + if (m_Condition == null) + return; + if (m_Condition.Evaluate()) + { + foreach (Action action in m_Actions) + { + action.Invoke(args); + } + } + } + + } + +} diff --git a/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs.meta b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs.meta new file mode 100644 index 0000000..c5f1c19 --- /dev/null +++ b/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c3ee81c4dceea3346b4c5e7b1d371e92 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |