Jeremy Smitherman 1523485fe2 feat/state (#2)
Reviewed-on: #2
Co-authored-by: Jeremy Smitherman <Jeremysmitherman@gmail.com>
Co-committed-by: Jeremy Smitherman <Jeremysmitherman@gmail.com>
2026-04-24 18:57:06 +00:00

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);
}
}
}