Den999 / GameStateMachine

Flexible and configurable game state machine for unity (it s not a real FSM but more like the game events aggregator)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

GameStateMachine

  • Lightweight and have NO dependecies.
  • Supports custom game states. You can create them!
  • Easy to use and safe (no scene setup, all you need will be created automatically at runtime)
  • NO singletons and static classes.

Basic usage:

public class Sample : MonoBehaviour
{
    private void Start()
    {
        // Just like a FindObjectOfType, but if it is not exists => create it
        // (better singleton version)
        var stateMachine = this.FindLazy<GameStateMachine>();

        // Subscribing
        stateMachine.On<RunningState>(() => Debug.Log("Running"));
        stateMachine.On<LoseState, WinState>(() => Debug.Log("The game was finished (lose or win)"));

        // Pushing states
        stateMachine.Push(new RunningState());
        stateMachine.Push(new WinState());
    }
}

Common use cases:

// On player trigger the finish => push WinState
// GameStateMachineUser hashes StateMachine for us :)
public class Finish : GameStateMachineUser
{
    private void OnTriggerEnter(Collider other)
    {
        if (other.TryGetComponent(out Player p))
            StateMachine.Push(new WinState());
    }
}

// OnGameFinish will be called on WinState or LoseState (from GameStateMachineUser) 
public class PlayerMovement : GameStateMachineUser
{
    private float _speedFactor = 1;

    protected override void OnGameFinish()
    {
        _speedFactor = 0;
    }
}

Custom states be like:

// Create your own states!
public class MyCustomState : GameState
{
    // Which states can be after our custom state?
    public override GameState[] PossibleNextStates => GameState[]
    {
        new RunningState(),
        new WinState(),
        new LoseState(),
    };

    // The state is not active (like pause, etc.)
    public override bool IsGameActiveDuringState => false;
}

About

Flexible and configurable game state machine for unity (it s not a real FSM but more like the game events aggregator)


Languages

Language:C# 100.0%