Reviewed-on: #2 Co-authored-by: Jeremy Smitherman <Jeremysmitherman@gmail.com> Co-committed-by: Jeremy Smitherman <Jeremysmitherman@gmail.com>
33 lines
879 B
C#
33 lines
879 B
C#
using UnityEngine;
|
|
|
|
namespace State
|
|
{
|
|
/// <summary>
|
|
/// Simple state machine that provides OnEnter, OnExit, Update and FixedUpdate callbacks
|
|
/// to the current state.
|
|
/// </summary>
|
|
public class Machine : MonoBehaviour
|
|
{
|
|
private GameState _currentState;
|
|
|
|
public void Update()
|
|
{
|
|
_currentState?.OnUpdate(Time.deltaTime);
|
|
}
|
|
|
|
public void FixedUpdate()
|
|
{
|
|
_currentState?.OnFixedUpdate(Time.fixedDeltaTime);
|
|
}
|
|
|
|
// ChangeState calls the Exit callback on the currently loaded state if one is loaded,
|
|
// then loads up the new given state, and calls OnEnter on it.
|
|
public void ChangeState(GameState newState)
|
|
{
|
|
_currentState?.OnExit();
|
|
_currentState = newState;
|
|
_currentState.OnEnter(this);
|
|
}
|
|
}
|
|
}
|