-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateApp.cs
52 lines (41 loc) · 1.41 KB
/
StateApp.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
using System;
using VContainer.Unity;
namespace App.States
{
public class StateApp : IStartable
{
public enum States
{
First,
Second,
Third
}
// Scripts that act on the state can use the static properties. Scripts that change the state need to inject StateApp.
public static States CurrentState { get; private set; }
public static Action<States> OnStateChanged;
public void Start()
{
//AppState.OnStateChanged += (CurrentState) =>
//{
// if (CurrentState == CurrentState.States.First)
// // Do action.
//};
}
public void SetState(States currentState)
{
UnityEngine.Debug.Log($"AppState: {CurrentState}");
if (CurrentState == currentState)
return;
CurrentState = currentState;
OnStateChanged?.Invoke(CurrentState);
}
public void NextState()
{
// Get the number of states in the States enum.
int stateCount = Enum.GetNames(typeof(States)).Length;
// Convert the current networkedState to an integer, add 1, and apply modulo operation to cycle back to the start if at the end.
int nextState = ((int)CurrentState + 1) % stateCount;
SetState((States)nextState);
}
}
}