All Projects → Real-Serious-Games → Fluent State Machine

Real-Serious-Games / Fluent State Machine

Licence: mit
Fluent API for creating state machines in C#

Projects that are alternatives of or similar to Fluent State Machine

Swissarmylib
Collection of helpful utilities we use in our Unity projects.
Stars: ✭ 154 (-21.03%)
Mutual labels:  state-machine, unity, unity3d, game-development
Gdk For Unity Fps Starter Project
SpatialOS GDK for Unity FPS Starter Project
Stars: ✭ 119 (-38.97%)
Mutual labels:  unity, unity3d, game-development
Afsm
C++14 Finite State Machine library
Stars: ✭ 113 (-42.05%)
Mutual labels:  state-machine, finite-state-machine, fsm
Django Fsm
Django friendly finite state machine support
Stars: ✭ 1,898 (+873.33%)
Mutual labels:  state-machine, finite-state-machine, fsm
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (-56.92%)
Mutual labels:  state-machine, finite-state-machine, fsm
Finity
A finite state machine library for Node.js and the browser with a friendly configuration DSL.
Stars: ✭ 88 (-54.87%)
Mutual labels:  state-machine, finite-state-machine, fsm
Unityfx.async
Asynchronous operations (promises) for Unity3d.
Stars: ✭ 143 (-26.67%)
Mutual labels:  unity, unity3d, game-development
09 Zombierunner Original
First person shooter with Unity terrain and AI pathfinding (http://gdev.tv/cudgithub)
Stars: ✭ 64 (-67.18%)
Mutual labels:  unity, unity3d, game-development
Unitypausemenu
This is an open source Unity pause menu created for the game New Horizons, and it's completely free because of how a pause menu is a core component of a game, while the unity asset store was lacking in such an asset (until this was released on the asset store).
Stars: ✭ 160 (-17.95%)
Mutual labels:  unity, unity3d, game-development
Nanostate
🚦- Small Finite State Machines
Stars: ✭ 151 (-22.56%)
Mutual labels:  state-machine, finite-state-machine, fsm
Htframework
Unity HTFramework, a rapid development framework of client to the unity.
Stars: ✭ 179 (-8.21%)
Mutual labels:  fsm, unity, unity3d
Unity Design Pattern
🍵 All Gang of Four Design Patterns written in Unity C# with many examples. And some Game Programming Patterns written in Unity C#. | 各种设计模式的Unity3D C#版本实现
Stars: ✭ 2,600 (+1233.33%)
Mutual labels:  unity, unity3d, game-development
1 Character Movement
The first section of the course. You will learn everything required to build a simple movement system in your RPG, creating the core experience. http://gdev.tv/rpggithub
Stars: ✭ 81 (-58.46%)
Mutual labels:  unity, unity3d, game-development
Reflexityai
Provide a basic framework to build an Utility IA in Unity using the xNode editor of Siccity
Stars: ✭ 109 (-44.1%)
Mutual labels:  unity, unity3d, game-development
3d Game Shaders For Beginners
🎮 A step-by-step guide to implementing SSAO, depth of field, lighting, normal mapping, and more for your 3D game.
Stars: ✭ 11,698 (+5898.97%)
Mutual labels:  unity, unity3d, game-development
Hfsm2
High-Performance Hierarchical Finite State Machine Framework
Stars: ✭ 134 (-31.28%)
Mutual labels:  state-machine, fsm, game-development
Unity Scene Query
A library to traverse and query the Unity scene to find particular objects, uses something similar to CSS selectors to identify game objects.
Stars: ✭ 63 (-67.69%)
Mutual labels:  unity, unity3d, game-development
3 Modifiers And Abilities
Customise character abilities, weapons, characters and enemies. This includes multiple damage types, modifiers, sounds, animations. By the end you can create your core combat experience. (REF MA_RPG) http://gdev.tv/rpggithub
Stars: ✭ 64 (-67.18%)
Mutual labels:  unity, unity3d, game-development
Sm
🚀 SM – a static State Machine library
Stars: ✭ 149 (-23.59%)
Mutual labels:  state-machine, finite-state-machine, game-development
Rimlight
Customizable rimlight shader for Unity that includes pulsation and noise scrolling. Give your scenes that extra oomph!
Stars: ✭ 170 (-12.82%)
Mutual labels:  unity, unity3d, game-development

Fluent-State-Machine Build Status NuGet

Fluent API for creating hierarchical finite state machines in C#.

A basic example - creating a state machine with a single state

Reference the dll and include the namespace:

using RSG;

Create a state via the builder class:

var rootState = new StateMachineBuilder()
    .State("main")
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine("Updating main state");
        })
    .End()
    .Build();

Calling Build() on our StateMachineBuilder sets up all the states we've specified and returns a root state which is automatically created to be the parent of any top-level states we create.

The function in Enter will be called once when we first enter the state, and the function in Update will be called every time we update the state.

Once our state machine is set up, we then need to set the initial state:

rootState.ChangeState("main");

Finally, we can update our currently active state with a specified time since the last update as follows:

rootState.Update(timeSinceLastFrame); 

Conditions

As well as Enter and Update, conditions can be set up, which are functions called when the state is updated only when a specified predicate is satisfied.

var rootState = new StateMachineBuilder()
    .State("main")
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine("Updating main state");
        })
        .Condition(() => SomeBoolean, state => {
            Console.WriteLine("Condition satisfied");
        })
    .End()
    .Build();

The predicate to the condition is also a function, and this function will be invoked every time the state is updated.

Using multiple states

Switch between states by using IState.ChangeState(stateName)

var rootState = new StateMachineBuilder()
    .State("main")
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine("Updating main state");
        })
        .Condition(() => SomeBoolean, state => {
            state.Parent.ChangeState("secondary");
        })
        .Exit(state => {
            Console.WriteLine("Exited main state");
        })
    .End()
    .State("secondary")
        .Enter(state => {
            Console.WriteLine("Entered secondary state")
        })
    .End()
    .Build();

ChangeState will attempt to exit the current state and change to the specified one. Note that since both our "main" state and "secondary" states are children of the root state, we actually need to call ChangeState on the main state's parent (which in this case is the root state).

The function specified in Exit will be called when we exit the main state.

Nesting states

Nested states, sometimes referred to as super-states are useful for encapsulating different parts of logic and simplifying transitions between states.

var rootState = new StateMachineBuilder()
    .State("main")
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine("Updating main state");
        })
        .Condition(() => SomeBoolean, state => {
            state.ChangeState("child");
        })
        .Exit(state => {
            Console.WriteLine("Exited main state");
        })
        .State("child")
            .Enter(state => {
                Console.WriteLine("Entered secondary state")
            })
        .End()
    .End()
    .Build();

Our second state is now a child of the main state. This example doesn't really add any extra functionality but serves to show how a nesting can work in its most basic form. Note that Exit is not called on the main state when we change to it even though it is no longer being updated, since it is still technically active but only the end of the tree is updated.

Pushing and popping nested states

In addition to a list of children, each state contains a stack of active children

var rootState = new StateMachineBuilder()
    .State("main")
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine("Updating main state");
        })
        .Condition(() => SomeBoolean, state => {
            state.PushState("firstChild");
        })
        .Exit(state => {
            Console.WriteLine("Exited main state");
        })
        .State("firstChild")
            .Enter(state => {
                Console.WriteLine("Entered secondary state")
            })
            .Condition(() => SomeBoolean, state => {
                state.Parent.PushState("secondChild");
            })
        .End()
        .State("secondChild")
            .Enter(state => {
                Console.WriteLine("Entered third child state")
            })
            .Condition(() => ShouldPopState, state => {
                state.Parent.PopState();
            })
        .End()
    .End()
    .Build();

In this example, we will start out in main, then when SomeBoolean is true we'll go to firstChild. If SomeBoolean is still true we will transition to secondChild, and then when ShouldPopState is true we will go back the previous state on the stack (in this case firstChild).

Events

The state builder doesn't give us a reference to the new states it creates, but if we want to trigger an action on the currently active state that we don't want to run every frame (like a condition) we can use an event.

var rootState = new StateMachineBuilder()
    .State("main")
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine("Updating main state");
        })
        .Condition(() => SomeBoolean, state => {
            state.ChangeState("child");
        })
        .State("child")
            .Enter(state => {
                Console.WriteLine("Entered secondary state")
            })
            .Event("MyEvent", state => {
                Console.WriteLine("MyEvent triggered");
            });
        .End()
    .End()
    .Build();

Invoking the event on the root state will trigger it on whatever the currently active state is:

rootState.TriggerEvent("MyEvent"); 

Custom states

Sometimes we will want to have data shared by the Enter, Update, Condition, Event and Exit functions of a state, specific to that state, or we might want to have some methods that are internal to just that state. This an be achieved by creating a sub-class of AbstractState that has our extra functionality in it, and specifying this when we create the state.

class MainState : AbstractState
{
    public string updateMessage = "Updating main state";
    
    public void PushChildState()
    {
        PushState("child");
    }
}

Note that since this class inherits from AbstractState, it has full access to methods for pushing, popping and changing states. Since this will be newed-up by the state builder it must have either no constructor or a parameterless constructor.

Setting up the state is basically the same as it would otherwise be except that we now also specify the type of the state. The name field is now optional since the builder can just automatically take the name from the name of the class it uses, although since no two states that are children of the same state can have the same name, this can only be done once per type of state for each group.

var rootState = new StateMachineBuilder()
    .State<MainState>()
        .Enter(state => {
            Console.WriteLine("Entered main state");
        })
        .Update((state, deltaTime) => {
            Console.WriteLine(state.updateMessage);
        })
        .Condition(() => SomeBoolean, state => {
            state.PushChildState();
        })
        .State("child")
            .Enter(state => {
                Console.WriteLine("Entered secondary state")
            })
            .Event("MyEvent", state => {
                Console.WriteLine("MyEvent triggered");
            });
        .End()
    .End()
    .Build();

Examples

In the Examples directory there are a couple of example projects to demonstrate usage of the library.

Example 1

This sample demonstrates a fairly simple state machine with custom nested states, from a console app.

Unity Example

This sample comes as a Unity project and shows how one could set up the library for use in a Unity game, as well as using events to trigger actions in response to Unity physics collision events.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].