-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStateControllerBase.cs
More file actions
54 lines (45 loc) · 1.73 KB
/
StateControllerBase.cs
File metadata and controls
54 lines (45 loc) · 1.73 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
using System;
using System.Collections;
using UnityEngine;
namespace SFI.GameStates
{
/// <summary>
/// This state machine is about the simplest state transition / requirement that I could imagine.
/// If you want to make more / different state machines,
/// just make this a base class and override the Awake() method
/// with a new set of state definitions.
/// </summary>
public class StateControllerBase : MonoBehaviour
{
public static event Action<IState> OnGameStateChanged;
protected StateMachine _stateMachine;
//Yes I am using a Singleton. They are still quite useful!
public static StateControllerBase _instance;
public IState returnToState;
public string currentStateName;
public Type CurrentStateType => _stateMachine.CurrentState.GetType();
public IState LastStateType => _stateMachine.PreviousState;
protected void Init()
{
if (_instance != null)
{
Destroy(gameObject);
return;
}
_instance = this;
DontDestroyOnLoad(gameObject);
_stateMachine = new StateMachine();
//Subscribe to OnGameStateChanged elsewhere if you want to see when a state changes as well as get access to information in the state
_stateMachine.OnStateChanged += state => OnGameStateChanged?.Invoke(state);
_stateMachine.OnStateChanged += state => StateChanged(state);
}
private void StateChanged(IState state)
{
currentStateName = state.ToString();
}
public void Update()
{
_stateMachine.Tick();
}
}
}