Giter Site home page Giter Site logo

gamestatemachine's Introduction

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

gamestatemachine's People

Contributors

den999 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.