From 3b036c6de871aa519a1f7fbfb52e09618945041f Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Mon, 15 May 2023 09:28:11 +0800 Subject: *misc --- .../Assets/Scripts/Tools/TriggerSystem/Trigger.cs | 110 +++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs (limited to 'WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs') 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 +{ + + /// + /// 事件发生后检测条件的被动触发器 + /// + 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 m_Actions = new List(); + + 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); + } + } + } + + } + +} -- cgit v1.1-26-g67d0