blob: f21cee663d67bbf9f40b5a4d2d9c8dd984ee6a4b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using System.Collections.Generic;
using System.Diagnostics.Tracing;
namespace WK
{
/// <summary>
/// 事件发生后检测条件的被动触发器
/// </summary>
public class GlobalPassiveTrigger
{
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);
}
}
}
}
}
|