-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStateMachine.AsState.cs
More file actions
81 lines (67 loc) · 2.41 KB
/
StateMachine.AsState.cs
File metadata and controls
81 lines (67 loc) · 2.41 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
namespace BAStudio.StatePattern
{
public partial class StateMachine<T> : IState<T>
{
// --- IState<T> explicit implementation ---
void IState<T>.OnEntered(IStateMachine<T> machine, IState<T> previous, object parameter)
{
Subject = machine.Subject;
OnNestedEntry(previous, parameter);
}
void IState<T>.Update(IStateMachine<T> machine)
{
Update();
}
void IState<T>.OnLeaving(IStateMachine<T> machine, IState<T> next, object parameter)
{
if (CurrentState is not NoOpState && CurrentState != null)
CurrentState.OnLeaving(this, null, parameter);
CurrentState = new NoOpState();
}
void IState<T>.Reset()
{
ResetInternalState();
}
#if UNITY_2017_1_OR_NEWER
void IState<T>.FixedUpdate(IStateMachine<T> machine)
{
FixedUpdate();
}
void IState<T>.LateUpdate(IStateMachine<T> machine)
{
LateUpdate();
}
#endif
// --- Virtual hooks ---
/// <summary>
/// Called when this machine is entered as a state inside a parent machine.
/// Override to set the initial state (e.g., <c>ChangeState<MyInitialState>()</c>).
/// Subject is already set when this is called.
/// </summary>
protected virtual void OnNestedEntry(IState<T> previous, object parameter) { }
/// <summary>
/// Called when IState<T>.Reset() is invoked (after parent transitions away).
/// Default: no-op (AutoStateCache preserved for re-entry efficiency).
/// Override to clear: <c>AutoStateCache?.Clear();</c>
/// </summary>
protected virtual void ResetInternalState()
{
peerEventForwardingDepth = 0;
peerDispatchingDepth = 0;
}
// --- Debug path ---
/// <summary>
/// Full nested state path from this machine downward.
/// Example: "Alive > Exploration > Walking"
/// </summary>
public string GetStatePath()
{
if (CurrentState == null || CurrentState is NoOpState)
return "(none)";
string name = CurrentState.GetType().Name;
if (CurrentState is StateMachine<T> sub)
return name + " > " + sub.GetStatePath();
return name;
}
}
}