-
Notifications
You must be signed in to change notification settings - Fork 42
/
StateBuilder.cs
224 lines (192 loc) · 8.61 KB
/
StateBuilder.cs
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
using System;
namespace RSG
{
/// <summary>
/// Builder providing a fluent API for constructing states.
/// </summary>
public interface IStateBuilder<T, TParent> where T : AbstractState, new()
{
/// <summary>
/// Create a child state with a specified handler type. The state will take the
/// name of the handler type.
/// </summary>
/// <typeparam name="NewStateT">Handler type for the new state</typeparam>
/// <returns>A new state builder object for the new child state</returns>
IStateBuilder<NewStateT, IStateBuilder<T, TParent>> State<NewStateT>() where NewStateT : AbstractState, new();
/// <summary>
/// Create a named child state with a specified handler type.
/// </summary>
/// <typeparam name="NewStateT">Handler type for the new state</typeparam>
/// <param name="name">String for identifying state in parent</param>
/// <returns>A new state builder object for the new child state</returns>
IStateBuilder<NewStateT, IStateBuilder<T, TParent>> State<NewStateT>(string name) where NewStateT : AbstractState, new();
/// <summary>
/// Create a child state with the default handler type.
/// </summary>
/// <param name="name"></param>
/// <returns>A state builder object for the new child state</returns>
IStateBuilder<State, IStateBuilder<T, TParent>> State(string name);
/// <summary>
/// Set an action to be called when we enter the state.
/// </summary>
IStateBuilder<T, TParent> Enter(Action<T> onEnter);
/// <summary>
/// Set an action to be called when we exit the state.
/// </summary>
IStateBuilder<T, TParent> Exit(Action<T> onExit);
/// <summary>
/// Set an action to be called when we update the state.
/// </summary>
IStateBuilder<T, TParent> Update(Action<T, float> onUpdate);
/// <summary>
/// Set an action to be called on update when a condition is true.
/// </summary>
IStateBuilder<T, TParent> Condition(Func<bool> predicate, Action<T> action);
/// <summary>
/// Set an action to be triggerable when an event with the specified name is raised.
/// </summary>
IStateBuilder<T, TParent> Event(string identifier, Action<T> action);
/// <summary>
/// Set an action with arguments to be triggerable when an event with the specified name is raised.
/// </summary>
/// <typeparam name="TEvent"></typeparam>
/// <param name="identifier"></param>
/// <param name="action"></param>
/// <returns></returns>
IStateBuilder<T, TParent> Event<TEvent>(string identifier, Action<T, TEvent> action) where TEvent : EventArgs;
/// <summary>
/// Finalise the current state and return the builder for its parent.
/// </summary>
TParent End();
}
/// <summary>
/// Builder providing a fluent API for constructing states.
/// </summary>
public class StateBuilder<T, TParent> : IStateBuilder<T, TParent>
where T : AbstractState, new()
{
/// <summary>
/// Class to return when we call .End()
/// </summary>
private readonly TParent parentBuilder;
/// <summary>
/// The current state we're building.
/// </summary>
private T state;
/// <summary>
/// Create a new state builder with a specified parent state and parent builder.
/// </summary>
/// <param name="parentBuilder">The parent builder, or what we will return
/// when .End is called.</param>
/// <param name="parentState">The parent of the new state to create.</param>
public StateBuilder(TParent parentBuilder, AbstractState parentState)
{
this.parentBuilder = parentBuilder;
// New-up state of the prescrbed type.
state = new T();
parentState.AddChild(state);
}
/// <summary>
/// Create a new state builder with a specified parent state, parent builder,
/// and name for the new state.
/// </summary>
/// <param name="parentBuilder">The parent builder, or what we will return
/// when .End is called.</param>
/// <param name="parentState">The parent of the new state to create.</param>
/// <param name="name">Name of the state to add.</param>
public StateBuilder(TParent parentBuilder, AbstractState parentState, string name)
{
this.parentBuilder = parentBuilder;
// New-up state of the prescrbed type.
state = new T();
parentState.AddChild(state, name);
}
/// <summary>
/// Create a child state with a specified handler type. The state will take the
/// name of the handler type.
/// </summary>
/// <typeparam name="NewStateT">Handler type for the new state</typeparam>
/// <returns>A new state builder object for the new child state</returns>
public IStateBuilder<NewStateT, IStateBuilder<T, TParent>> State<NewStateT>()
where NewStateT : AbstractState, new()
{
return new StateBuilder<NewStateT, IStateBuilder<T, TParent>>(this, state);
}
/// <summary>
/// Create a named child state with a specified handler type.
/// </summary>
/// <typeparam name="NewStateT">Handler type for the new state</typeparam>
/// <param name="name">String for identifying state in parent</param>
/// <returns>A new state builder object for the new child state</returns>
public IStateBuilder<NewStateT, IStateBuilder<T, TParent>> State<NewStateT>(string name)
where NewStateT : AbstractState, new()
{
return new StateBuilder<NewStateT, IStateBuilder<T, TParent>>(this, state, name);
}
/// <summary>
/// Create a child state with the default handler type.
/// </summary>
/// <param name="name"></param>
/// <returns>A state builder object for the new child state</returns>
public IStateBuilder<State, IStateBuilder<T, TParent>> State(string name)
{
return new StateBuilder<State, IStateBuilder<T, TParent>>(this, state, name);
}
/// <summary>
/// Set an action to be called when we enter the state.
/// </summary>
public IStateBuilder<T, TParent> Enter(Action<T> onEnter)
{
state.SetEnterAction(() => onEnter(state));
return this;
}
/// <summary>
/// Set an action to be called when we exit the state.
/// </summary>
public IStateBuilder<T, TParent> Exit(Action<T> onExit)
{
state.SetExitAction(() => onExit(state));
return this;
}
/// <summary>
/// Set an action to be called when we update the state.
/// </summary>
public IStateBuilder<T, TParent> Update(Action<T, float> onUpdate)
{
state.SetUpdateAction(dt => onUpdate(state, dt));
return this;
}
/// <summary>
/// Set an action to be called on update when a condition is true.
/// </summary>
public IStateBuilder<T, TParent> Condition(Func<bool> predicate, Action<T> action)
{
state.SetCondition(predicate, () => action(state));
return this;
}
/// <summary>
/// Set an action to be triggerable when an event with the specified name is raised.
/// </summary>
public IStateBuilder<T, TParent> Event(string identifier, Action<T> action)
{
state.SetEvent<EventArgs>(identifier, _ => action(state));
return this;
}
/// <summary>
/// Set an action with arguments to be triggerable when an event with the specified name is raised.
/// </summary>
public IStateBuilder<T, TParent> Event<TEvent>(string identifier, Action<T, TEvent> action)
where TEvent : EventArgs
{
state.SetEvent<TEvent>(identifier, args => action(state, args));
return this;
}
/// <summary>
/// Finalise the current state and return the builder for its parent.
/// </summary>
public TParent End()
{
return parentBuilder;
}
}
}