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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MovementEffects;
namespace WK
{
/// <summary>
/// 公共状态机
/// </summary>
public class AsyncStatemachine
{
public delegate void LoadStateComplete();
public abstract class State
{
public AsyncStatemachine owner;
public int stateID; // stateID可以是string.GetHashCode() 以提供扩展性
public abstract IEnumerator<float> OnStart();
public abstract IEnumerator<float> OnEnd();
public abstract void OnUpdate(float deltaTime);
}
public const int NULL_STATE_ID = -1;
public const float COROUINT_DELTIME = 0.01f;
private Dictionary<int/*stateID*/, State> allState = new Dictionary<int, State>();
private int curStateID = NULL_STATE_ID;
private int stateIDDistributor = 0;
// 在状态切换完成前为false,不能进行update
private bool isUpdateActive = false;
/// <summary>
/// 添加状态,并返回在状态机中的ID
/// </summary>
/// <param name="newState"></param>
/// <returns></returns>
public int RegisterState(State newState)
{
if (newState == null)
return NULL_STATE_ID;
if (stateIDDistributor + 1 >= int.MaxValue)
{
LogHelper.LogError("状态机添加状态失败,此状态机中添加的状态过多");
return NULL_STATE_ID;
}
++stateIDDistributor;
if (!allState.ContainsKey(stateIDDistributor))
{
newState.owner = this;
newState.stateID = stateIDDistributor;
allState.Add(stateIDDistributor, newState);
return stateIDDistributor;
}
LogHelper.LogError("状态机添加状态失败,状态已经存在");
return NULL_STATE_ID;
}
public bool RemoveState(State state)
{
if (state != null)
{
return RemoveState(state.stateID);
}
LogHelper.LogError("状态机删除状态失败,状态为空");
return false;
}
public bool RemoveState(int stateID)
{
if (allState.ContainsKey(stateID))
{
allState.Remove(stateID);
return true;
}
LogHelper.LogError("状态机删除状态失败,该状态不存在,stateID = " + stateID);
return false;
}
/// <summary>
/// 开始运行状态机
/// </summary>
/// <param name="stateID"></param>
/// <returns></returns>
public bool Start(int stateID)
{
if (!HasBegin())
{
ForceGotoState(stateID);
return true;
}
return false;
}
/// <summary>
/// 结束状态机
/// </summary>
/// <returns></returns>
public bool Stop()
{
if (HasBegin())
{
ForceGotoState(NULL_STATE_ID);
return true;
}
return false;
}
/// <summary>
/// 更新状态机
/// </summary>
/// <param name="deltaTime"></param>
public void Update(float deltaTime)
{
if (HasBegin())
{
UpdateState(curStateID, deltaTime);
}
}
public bool GotoState(int stateID, bool skipStartFunc = false, bool bForce = false, LoadStateComplete callback = null)
{
if (HasBegin())
return ForceGotoState(stateID, skipStartFunc, bForce, callback);
return false;
}
public bool ForceGotoState(int nextStateID, bool skipStartFunc = false, bool bForce = false, LoadStateComplete callback = null)
{
if (curStateID != nextStateID || bForce)
{
Timing.Instance.RunCoroutineOnInstance(AyncForceGotoState(nextStateID, skipStartFunc, callback));
return true;
}
if (callback != null)
callback();
return false;
}
/// <summary>
/// 异步的切换到某个状态
/// </summary>
/// <returns></returns>
private IEnumerator<float> AyncForceGotoState(int nextStateID, bool skipStartFunc = false, LoadStateComplete callback = null)
{
isUpdateActive = false;
CoroutineHandle handle = Timing.RunCoroutine(EndState(curStateID));
while (handle.IsRunning)
{
yield return Timing.WaitForSeconds(COROUINT_DELTIME);
}
curStateID = nextStateID;
if (!skipStartFunc)
{
CoroutineHandle handle2 = Timing.RunCoroutine(StartState(curStateID));
while (handle2.IsRunning)
{
yield return Timing.WaitForSeconds(COROUINT_DELTIME);
}
}
if (callback != null)
callback();
isUpdateActive = true;
}
public int GetCurStateID()
{
return curStateID;
}
public bool HasBegin()
{
if (curStateID != NULL_STATE_ID && allState.ContainsKey(curStateID))
return true;
return false;
}
public bool IsInState(int stateID)
{
if (HasBegin())
{
return curStateID == stateID;
}
return false;
}
public State GetState(int stateID)
{
if (allState.ContainsKey(stateID))
{
return allState[stateID];
}
return null;
}
public void Clean()
{
allState.Clear();
curStateID = NULL_STATE_ID;
stateIDDistributor = 0;
}
public void ResetCurState()
{
curStateID = NULL_STATE_ID;
}
private IEnumerator<float> StartState(int stateID)
{
if (HasBegin())
{
State state = GetState(stateID);
if (state != null)
{
CoroutineHandle handle = Timing.RunCoroutine(state.OnStart());
while (handle.IsRunning)
{
yield return Timing.WaitForSeconds(COROUINT_DELTIME);
}
}
}
}
private IEnumerator<float> EndState(int stateID)
{
if (HasBegin())
{
State state = GetState(stateID);
if (state != null)
{
CoroutineHandle handle = Timing.RunCoroutine(state.OnEnd());
while (handle.IsRunning)
{
yield return Timing.WaitForSeconds(COROUINT_DELTIME);
}
}
}
}
private void UpdateState(int stateID, float deltaTime)
{
if (HasBegin())
{
State state = GetState(stateID);
if (state != null)
state.OnUpdate(deltaTime);
}
}
}
}
|