summaryrefslogtreecommitdiff
path: root/WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs
diff options
context:
space:
mode:
Diffstat (limited to 'WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs')
-rw-r--r--WorldlineKeepers/Assets/Scripts/Tools/TriggerSystem/Trigger.cs110
1 files changed, 110 insertions, 0 deletions
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);
+ }
+ }
+ }
+
+ }
+
+}