Skip to content

Instantly share code, notes, and snippets.

@mminer
Last active February 3, 2023 04:57
Show Gist options
  • Save mminer/688c829e20bc07db6fabfd599ca20fa6 to your computer and use it in GitHub Desktop.
Save mminer/688c829e20bc07db6fabfd599ca20fa6 to your computer and use it in GitHub Desktop.
Bare-bones C# state machine.
using System;
using System.Collections.Generic;
public class StateMachine
{
public interface IState
{
public void OnEnter();
public void OnExit();
}
public event Action<StateMachine> OnChanged;
public IState CurrentState { get; private set; }
public IState PreviousState { get; private set; }
readonly Dictionary<Type, IState> states = new Dictionary<Type, IState>();
public StateMachine(IState defaultState)
{
CurrentState = defaultState;
}
public void Change<T>() where T : IState
{
if (!TryGet<T>(out var state))
{
throw new KeyNotFoundException();
}
PreviousState = CurrentState;
CurrentState = state;
PreviousState.OnExit();
CurrentState.OnEnter();
OnChanged?.Invoke(this);
}
public bool IsIn<T>() where T : IState
{
return CurrentState is T;
}
public void Register<T>() where T : IState, new()
{
var key = typeof(T);
var state = new T();
states.Add(key, state);
}
public bool TryGet<T>(out IState state) where T : IState
{
var key = typeof(T);
return states.TryGetValue(key, out state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment