Skip to content

Commit ab790db

Browse files
committed
v6.0.0
* Modified StateMachines (again), so that a state's "Enter" method can process a deltaTime argument * Updated README.md
1 parent 8006440 commit ab790db

File tree

8 files changed

+101
-152
lines changed

8 files changed

+101
-152
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This repository is a Unity package for C# code to reuse across projects. It cont
99
- A "Progress" class to help various tasks to generically track their progress.
1010
- A "SortKeyDictionary" class.
1111
- A "StateMachines" framework for setting up (finite) state machines.
12+
- A "VariableRanges" suite of structs to set and store paired range values.
1213
- A "TypeReferences" system to serialize Type references.
1314
- Custom attributes.
1415
- Extension methods for various standard C#/Unity classes, as well as some classes from this package.

Runtime/StateMachines/Interfaces/IState.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,28 @@ public interface IState
1212
/// </summary>
1313
string Name { get; }
1414

15+
/// <summary>
16+
/// A delegate that the <see cref="IState"/> can use to obtain a reference to other <see cref="IState"/>s from
17+
/// the same <see cref="IStateMachine"/>.
18+
/// </summary>
19+
System.Func<string, IState> StateRegister { get; set; }
20+
1521
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1622
#endregion
1723
#region Interface Methods
1824

1925
/// <summary>
2026
/// Perform set-up behaviour for the <see cref="IState"/> upon being entered.
2127
/// </summary>
22-
void EnterState();
23-
24-
/// <summary>
25-
/// Perform clean-up behaviour for the <see cref="IState"/> upon being exited.
26-
/// </summary>
27-
void ExitState();
28-
29-
/// <summary>
30-
/// Retrieve a <see cref="IState"/> reference from the <see cref="IState"/>'s set state register.
31-
/// </summary>
32-
/// <param name="stateName">
33-
/// The <see cref="string"/> name of the <see cref="IState"/> to retrieve.
28+
/// <param name="deltaTime">
29+
/// The amount of time (in seconds) that has passed since the prior update.
3430
/// </param>
35-
/// <returns>
36-
/// A <see cref="IState"/>.
37-
/// </returns>
38-
IState RetrieveStateFromRegister(string stateName);
31+
void Enter(float deltaTime);
3932

4033
/// <summary>
41-
/// Register a delegate that the <see cref="IState"/> can use to obtain a reference to other
42-
/// <see cref="IState"/>s from the same <see cref="IStateMachine"/>.
34+
/// Perform clean-up behaviour for the <see cref="IState"/> upon being exited.
4335
/// </summary>
44-
/// <param name="stateRegister">
45-
/// The delegate to set.
46-
/// </param>
47-
void SetStateRegister(System.Func<string, IState> stateRegister);
36+
void Exit();
4837

4938
/// <summary>
5039
/// Perform the update behaviour for the <see cref="IState"/>.
@@ -56,7 +45,7 @@ public interface IState
5645
/// A <see cref="IState"/> if a transition to said <see cref="IState"/> is required.
5746
/// <see cref="null"/> if no <see cref="IState"/> transition is required.
5847
/// </returns>
59-
IState UpdateState(float deltaTime);
48+
IState Update(float deltaTime);
6049

6150
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6251
#endregion

Runtime/StateMachines/Interfaces/IStateT.cs

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,28 @@ public interface IState<TEnum> where TEnum: System.Enum
1616
/// </summary>
1717
TEnum Key { get; }
1818

19+
/// <summary>
20+
/// A delegate that the <see cref="IState{TEnum}"/> can use to obtain a reference to other
21+
/// <see cref="IState{TEnum}"/>s from the same <see cref="IStateMachine{TEnum}"/>.
22+
/// </summary>
23+
System.Func<TEnum, IState<TEnum>> StateRegister { get; set; }
24+
1925
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2026
#endregion
2127
#region Interface Methods
2228

2329
/// <summary>
2430
/// Perform set-up behaviour for the <see cref="IState{TEnum}"/> upon being entered.
2531
/// </summary>
26-
void EnterState();
27-
28-
/// <summary>
29-
/// Perform clean-up behaviour for the <see cref="IState{IState}"/> upon being exited.
30-
/// </summary>
31-
void ExitState();
32-
33-
/// <summary>
34-
/// Retrieve a <see cref="IState{TEnum}"/> reference from the <see cref="IState{TEnum}"/>'s set state register.
35-
/// </summary>
36-
/// <param name="state">
37-
/// The <typeparamref name="TEnum"/> key of the <see cref="IState"/> to retrieve.
32+
/// <param name="deltaTime">
33+
/// The amount of time (in seconds) that has passed since the prior update.
3834
/// </param>
39-
/// <returns>
40-
/// A <see cref="IState{TEnum"/>.
41-
/// </returns>
42-
IState<TEnum> RetrieveStateFromRegister(TEnum state);
35+
void Enter(float deltaTime);
4336

4437
/// <summary>
45-
/// Register a delegate that the <see cref="IState{TEnum}"/> can use to obtain a reference to other
46-
/// <see cref="IState{TEnum}"/>s from the same <see cref="IStateMachine{TEnum}"/>.
38+
/// Perform clean-up behaviour for the <see cref="IState{IState}"/> upon being exited.
4739
/// </summary>
48-
/// <param name="stateRegister">
49-
/// The delegate to set.
50-
/// </param>
51-
void SetStateRegister(System.Func<TEnum, IState<TEnum>> stateRegister);
40+
void Exit();
5241

5342
/// <summary>
5443
/// Perform the update behaviour for the <see cref="IState{TEnum}"/>.
@@ -60,7 +49,7 @@ public interface IState<TEnum> where TEnum: System.Enum
6049
/// A <see cref="IState{TEnum}"/> if a transition to said <see cref="IState{TEnum}"/> is required.
6150
/// <see cref="null"/> if no <see cref="IState{TEnum}"/> transition is required.
6251
/// </returns>
63-
IState<TEnum> UpdateState(float deltaTime);
52+
IState<TEnum> Update(float deltaTime);
6453

6554
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
6655
#endregion

Runtime/StateMachines/State.cs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,24 @@ public abstract class State : IState
1414
/// </summary>
1515
public string Name => name;
1616

17+
/// <summary>
18+
/// A delegate that the <see cref="State"/> can use to obtain a reference to other
19+
/// <see cref="State}"/>s from the same <see cref="IStateMachine"/>.
20+
/// </summary>
21+
System.Func<string, IState> IState.StateRegister
22+
{
23+
get
24+
=> stateRegister ?? throw
25+
new System.InvalidOperationException(NoStateRegisterSet);
26+
set
27+
{
28+
if (stateRegister != null)
29+
throw
30+
new System.InvalidOperationException(StateRegisterAlreadySet);
31+
stateRegister = value;
32+
}
33+
}
34+
1735
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1836
#endregion
1937
#region Fields
@@ -26,10 +44,10 @@ public abstract class State : IState
2644
#region Constructor
2745

2846
/// <summary>
29-
///
47+
/// Creates a new <see cref="State"/>.
3048
/// </summary>
3149
/// <param name="name">
32-
///
50+
/// The <see cref="string"/> name to assign to the <see cref="State"/>.
3351
/// </param>
3452
public State(string name)
3553
{
@@ -42,48 +60,15 @@ public State(string name)
4260
/// <summary>
4361
/// Perform set-up behaviour for the <see cref="State"/> upon being entered.
4462
/// </summary>
45-
public abstract void EnterState();
46-
47-
/// <summary>
48-
/// Perform clean-up behaviour for the <see cref="State"/> upon being exited.
49-
/// </summary>
50-
public abstract void ExitState();
51-
52-
/// <summary>
53-
/// Retrieve a <see cref="IState"/> reference from the <see cref="State"/>'s set state register.
54-
/// </summary>
55-
/// <param name="stateName">
56-
/// The <see cref="string"/> name of the <see cref="IState"/> to retrieve.
63+
/// <param name="deltaTime">
64+
/// The amount of time (in seconds) that has passed since the prior update.
5765
/// </param>
58-
/// <returns>
59-
/// A <see cref="IState"/>.
60-
/// </returns>
61-
/// <exception cref="System.InvalidOperationException">
62-
/// If no state register delegate has been set to the <see cref="State"/>.
63-
/// </exception>
64-
IState IState.RetrieveStateFromRegister(string stateName)
65-
=> stateRegister != null ?
66-
stateRegister.Invoke(stateName)
67-
: throw
68-
new System.InvalidOperationException(NoStateRegisterSet);
66+
public abstract void Enter(float deltaTime);
6967

7068
/// <summary>
71-
/// Register a delegate that the <see cref="IState"/> can use to obtain a reference to other
72-
/// <see cref="IState"/>s from the same <see cref="IStateMachine"/>.
69+
/// Perform clean-up behaviour for the <see cref="State"/> upon being exited.
7370
/// </summary>
74-
/// <param name="stateRegister">
75-
/// The delegate to set.
76-
/// </param>
77-
/// <exception cref="System.InvalidOperationException">
78-
/// If a state register delegate has already been set to the <see cref="State"/>.
79-
/// </exception>
80-
void IState.SetStateRegister(System.Func<string, IState> stateRegister)
81-
{
82-
if (this.stateRegister != null)
83-
throw
84-
new System.InvalidOperationException(StateRegisterAlreadySet);
85-
this.stateRegister = stateRegister;
86-
}
71+
public abstract void Exit();
8772

8873
/// <summary>
8974
/// Perform the update behaviour for the <see cref="State"/>.
@@ -95,15 +80,14 @@ void IState.SetStateRegister(System.Func<string, IState> stateRegister)
9580
/// A <see cref="IState"/> if a transition to said <see cref="IState"/> is required.
9681
/// <see cref="null"/> if no <see cref="IState"/> transition is required.
9782
/// </returns>
98-
public abstract IState UpdateState(float deltaTime);
83+
public abstract IState Update(float deltaTime);
9984

10085
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
10186
#endregion
10287
#region Methods
10388

10489
/// <summary>
105-
/// A method that <see cref="State"/>-inheriting classes can use to obtain references to other
106-
/// <see cref="IState"/>s used by the same <see cref="IStateMachine"/>.
90+
/// Obtains a reference to another <see cref="IState"/>s used by the same <see cref="IStateMachine"/>.
10791
/// </summary>
10892
/// <param name="stateName">
10993
/// The <see cref="string"/> name of the <see cref="IState"/> to obtain.
@@ -112,7 +96,7 @@ void IState.SetStateRegister(System.Func<string, IState> stateRegister)
11296
/// A <see cref="IState"/>.
11397
/// </returns>
11498
protected IState GetState(string stateName)
115-
=> ((IState)this).RetrieveStateFromRegister(stateName);
99+
=> ((IState)this).StateRegister.Invoke(stateName);
116100

117101
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
118102
#endregion

Runtime/StateMachines/StateMachine.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public StateMachine(IEnumerable<IState> states)
100100
throw
101101
new System.ArgumentException(StateNameDuplicate(name, type));
102102
this.states.Add(state);
103-
state.SetStateRegister(NameToState);
103+
state.StateRegister = NameToState;
104104
types.Add(type);
105105
names.Add(name);
106106
}
@@ -153,9 +153,9 @@ public void UpdateCurrentState(float deltaTime)
153153
throw
154154
new System.InvalidOperationException(NoStateSet);
155155
timeCurrentStateActive += deltaTime;
156-
var nextState = currentState.UpdateState(deltaTime);
156+
var nextState = currentState.Update(deltaTime);
157157
if (nextState != null)
158-
SetState(nextState);
158+
SetState(nextState, deltaTime);
159159
}
160160
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
161161
#endregion
@@ -171,16 +171,16 @@ private IState NameToState(string name)
171171
foundState;
172172
}
173173

174-
private void SetState(IState targetState)
174+
private void SetState(IState targetState, float deltaTime = 0f)
175175
{
176176
if (targetState != currentState)
177177
{
178178
if (currentState != null)
179179
{
180-
currentState.ExitState();
180+
currentState.Exit();
181181
OnStateChange?.Invoke(this);
182182
}
183-
targetState.EnterState();
183+
targetState.Enter(deltaTime);
184184
currentState = targetState;
185185
timeCurrentStateActive = 0f;
186186
}

Runtime/StateMachines/StateMachineT.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public StateMachine(IEnumerable<IState<TEnum>> states)
100100
throw
101101
new Exceptions.StateDuplicationException(type);
102102
this.states.Add(state.Key, state);
103-
state.SetStateRegister(EnumToState);
103+
state.StateRegister = EnumToState;
104104
types.Add(type);
105105
}
106106
if (this.states.IsEmpty())
@@ -151,24 +151,24 @@ public void UpdateCurrentState(float deltaTime)
151151
throw
152152
new System.InvalidOperationException(NoStateSet);
153153
timeCurrentStateActive += deltaTime;
154-
var nextState = currentState.UpdateState(deltaTime);
154+
var nextState = currentState.Update(deltaTime);
155155
if (nextState != null)
156-
SetState(nextState);
156+
SetState(nextState, deltaTime);
157157
}
158158
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159159
#endregion
160160
#region Methods
161161

162-
private void SetState(IState<TEnum> targetState)
162+
private void SetState(IState<TEnum> targetState, float deltaTime = 0f)
163163
{
164164
if (targetState != currentState)
165165
{
166166
if (currentState != null)
167167
{
168-
currentState.ExitState();
168+
currentState.Exit();
169169
OnStateChange?.Invoke(this);
170170
}
171-
targetState.EnterState();
171+
targetState.Enter(deltaTime);
172172
currentState = targetState;
173173
timeCurrentStateActive = 0f;
174174
}

0 commit comments

Comments
 (0)