-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworkedStateMachine.cs
39 lines (33 loc) · 1.02 KB
/
NetworkedStateMachine.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
using System;
using System.Collections.Generic;
using Unity.Netcode;
public class NetworkedStateMachine<EState> : NetworkBehaviour where EState : Enum
{
protected Dictionary<EState, NetworkedBaseState<EState>> States = new Dictionary<EState, NetworkedBaseState<EState>>();
protected NetworkedBaseState<EState> CurrentState;
private bool isTransitioning;
protected virtual void Start()
{
CurrentState.EnterState();
}
protected virtual void Update()
{
EState nextStateKey = CurrentState.GetNextState();
if (!isTransitioning && nextStateKey.Equals(CurrentState.StateKey))
{
CurrentState.UpdateState();
}
else if (!isTransitioning)
{
TransitionToState(nextStateKey);
}
}
private void TransitionToState(EState nextState)
{
isTransitioning = true;
CurrentState.ExitState();
CurrentState = States[nextState];
CurrentState.EnterState();
isTransitioning = false;
}
}