-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathCoState.cs
More file actions
255 lines (231 loc) · 6.99 KB
/
CoState.cs
File metadata and controls
255 lines (231 loc) · 6.99 KB
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
255
using System.Collections;
using UnityEngine;
using System;
namespace UnityHFSM
{
/// <summary>
/// A state that can run a Unity coroutine as its <c>OnLogic</c> method.
/// </summary>
/// <inheritdoc />
public class CoState<TStateId, TEvent> : ActionState<TStateId, TEvent>
{
private readonly MonoBehaviour mono;
private readonly Func<IEnumerator> coroutineCreator;
private readonly Action<CoState<TStateId, TEvent>> onEnter;
private readonly Action<CoState<TStateId, TEvent>> onExit;
private readonly Func<CoState<TStateId, TEvent>, bool> canExit;
private readonly bool shouldLoopCoroutine;
public ITimer timer;
private Coroutine activeCoroutine;
// The CoState class allows you to use either a function without any parameters or a
// function that takes the state as a parameter to create the coroutine.
// To allow for this and ease of use, the class has two nearly identical constructors.
/// <summary>
/// Initializes a new instance of the CoState class.
/// </summary>
/// <param name="mono">The MonoBehaviour of the script that should run the coroutine.</param>
/// <param name="onEnter">A function that is called when the state machine enters this state.</param>
/// <param name="coroutine">A coroutine that is run while this state is active.
/// It runs independently of the parent state machine's <c>OnLogic()</c>, because it is handled by Unity.
/// It is started once the state enters and is terminated when the state exits.</param>
/// <param name="onExit">A function that is called when the state machine exits this state.</param>
/// <param name="canExit">(Only if <c>needsExitTime</c> is true):
/// Function that determines if the state is ready to exit (true) or not (false).
/// It is called OnExitRequest and on each logic step when a transition is pending.</param>
/// <param name="loop">If true, it will loop the coroutine, running it again once it has completed.</param>
/// <inheritdoc cref="StateBase{T}(bool, bool)"/>
public CoState(
MonoBehaviour mono,
Func<CoState<TStateId, TEvent>, IEnumerator> coroutine,
Action<CoState<TStateId, TEvent>> onEnter = null,
Action<CoState<TStateId, TEvent>> onExit = null,
Func<CoState<TStateId, TEvent>, bool> canExit = null,
bool loop = true,
bool needsExitTime = false,
bool isGhostState = false) : base(needsExitTime, isGhostState)
{
this.mono = mono;
this.coroutineCreator = () => coroutine(this);
this.onEnter = onEnter;
this.onExit = onExit;
this.canExit = canExit;
this.shouldLoopCoroutine = loop;
timer = new Timer();
}
/// <inheritdoc cref="CoState{TStateId, TEvent}(
/// MonoBehaviour,
/// Func{CoState{TStateId, TEvent}, IEnumerator},
/// Action{CoState{TStateId, TEvent}},
/// Action{CoState{TStateId, TEvent}},
/// Func{CoState{TStateId, TEvent}, bool},
/// bool,
/// bool,
/// bool
/// )"/>
public CoState(
MonoBehaviour mono,
Func<IEnumerator> coroutine,
Action<CoState<TStateId, TEvent>> onEnter = null,
Action<CoState<TStateId, TEvent>> onExit = null,
Func<CoState<TStateId, TEvent>, bool> canExit = null,
bool loop = true,
bool needsExitTime = false,
bool isGhostState = false) : base(needsExitTime, isGhostState)
{
this.mono = mono;
this.coroutineCreator = coroutine;
this.onEnter = onEnter;
this.onExit = onExit;
this.canExit = canExit;
this.shouldLoopCoroutine = loop;
timer = new Timer();
}
public override void OnEnter()
{
timer.Reset();
onEnter?.Invoke(this);
if (coroutineCreator != null)
{
activeCoroutine = mono.StartCoroutine(
shouldLoopCoroutine
? LoopCoroutine()
: coroutineCreator()
);
}
}
private IEnumerator LoopCoroutine()
{
IEnumerator routine = coroutineCreator();
while (true)
{
// This checks if the routine needs at least one frame to execute.
// If not, LoopCoroutine will wait 1 frame to avoid an infinite
// loop which will crash Unity.
if (routine.MoveNext())
yield return routine.Current;
else
yield return null;
// Iterate from the onLogic coroutine until it is depleted.
while (routine.MoveNext())
yield return routine.Current;
// Restart the coroutine.
routine = coroutineCreator();
}
}
public override void OnLogic()
{
if (needsExitTime && canExit != null && fsm.HasPendingTransition && canExit(this))
{
fsm.StateCanExit();
}
}
public override void OnExit()
{
if (activeCoroutine != null)
{
mono.StopCoroutine(activeCoroutine);
activeCoroutine = null;
}
onExit?.Invoke(this);
}
public override void OnExitRequest()
{
if (canExit != null && canExit(this))
{
fsm.StateCanExit();
}
}
}
/// <inheritdoc />
public class CoState<TStateId> : CoState<TStateId, string>
{
/// <inheritdoc />
public CoState(
MonoBehaviour mono,
Func<CoState<TStateId, string>, IEnumerator> coroutine,
Action<CoState<TStateId, string>> onEnter = null,
Action<CoState<TStateId, string>> onExit = null,
Func<CoState<TStateId, string>, bool> canExit = null,
bool loop = true,
bool needsExitTime = false,
bool isGhostState = false)
: base(
mono,
coroutine: coroutine,
onEnter: onEnter,
onExit: onExit,
canExit: canExit,
loop: loop,
needsExitTime: needsExitTime,
isGhostState: isGhostState)
{
}
/// <inheritdoc />
public CoState(
MonoBehaviour mono,
Func<IEnumerator> coroutine,
Action<CoState<TStateId, string>> onEnter = null,
Action<CoState<TStateId, string>> onExit = null,
Func<CoState<TStateId, string>, bool> canExit = null,
bool loop = true,
bool needsExitTime = false,
bool isGhostState = false)
: base(
mono,
coroutine: coroutine,
onEnter: onEnter,
onExit: onExit,
canExit: canExit,
loop: loop,
needsExitTime: needsExitTime,
isGhostState: isGhostState)
{
}
}
/// <inheritdoc />
public class CoState : CoState<string, string>
{
/// <inheritdoc />
public CoState(
MonoBehaviour mono,
Func<CoState<string, string>, IEnumerator> coroutine,
Action<CoState<string, string>> onEnter = null,
Action<CoState<string, string>> onExit = null,
Func<CoState<string, string>, bool> canExit = null,
bool loop = true,
bool needsExitTime = false,
bool isGhostState = false)
: base(
mono,
coroutine: coroutine,
onEnter: onEnter,
onExit: onExit,
canExit: canExit,
loop: loop,
needsExitTime: needsExitTime,
isGhostState: isGhostState)
{
}
/// <inheritdoc />
public CoState(
MonoBehaviour mono,
Func<IEnumerator> coroutine,
Action<CoState<string, string>> onEnter = null,
Action<CoState<string, string>> onExit = null,
Func<CoState<string, string>, bool> canExit = null,
bool loop = true,
bool needsExitTime = false,
bool isGhostState = false)
: base(
mono,
coroutine: coroutine,
onEnter: onEnter,
onExit: onExit,
canExit: canExit,
loop: loop,
needsExitTime: needsExitTime,
isGhostState: isGhostState)
{
}
}
}