/shardy-fsm

Finite state machine for Shardy and more

Primary LanguageC#MIT LicenseMIT

Static Badge Static Badge

FSM for Shardy

FSM for Shardy

Framework for online games and apps

Quick start · Docs · Unity client · Shardy · Report Bug

💬 Overview

This package is an implementation of a finite state machine (FSM) for Shardy, and more. It provides a flexible API for building FSMs using a chain of methods and conditions.

Note

Shardy is a framework for online games and applications for Node.js. It provides the basic functionality for building microservices solutions: mobile, social, web, multiplayer games, realtime applications, chats, middleware services, etc.

Read about Shardy 💪

✨ Features

  • Flexible API: states, triggers, conditions
  • Custom state and trigger types
  • UML diagram generation (notes support)
  • Good reference materials: docs, snippets, samples
  • No 3rd party libs

🚀 Usage

Installation

Get it from releases page or add the line to Packages/manifest.json and module will be installed directly from Git url:

"com.mopsicus.shardy.fsm": "https://github.com/mopsicus/shardy-fsm.git",

Environment setup

For a better experience, you can set up an environment for local development. Since Shardy and all modules (like this) are developed with VS Code, all settings are provided for it.

  1. Use Monokai Pro or eppz! themes
  2. Use FiraCode font
  3. Install extensions:
    • C#
    • C# Dev Kit
    • Unity
  4. Enable Inlay Hints in C# extension
  5. Install Visual Studio Editor package in Unity
  6. Put .editorconfig in root project directory
  7. Be cool

Quick start

  1. Install package
  2. Add Shardy.FSM to uses section
  3. Define your states and triggers
  4. Call Builder method
  5. Add states, transtions, conditions
  6. Call Build method to finish initialization
  7. Call trigger(s)
  8. Profit
using Shardy.FSM;

/// <summary>
/// Test FSM
/// </summary>
void Test() {
    _fsm = FSM<State, Action>.Builder(State.Standing)
    .State(State.Standing)
    .OnEnter((data) => {
        Debug.Log("on enter standing");
    })
    .To(State.Sitting).On(Action.Down)
    .To(State.Jumping).On(Action.Space).If(s => _isConditionActive)
    .State(State.Sitting)
    .To(State.Lying).On(Action.Down)
    .To(State.Standing).On(Action.Up)
    .State(State.Lying)
    .To(State.Sitting).On(Action.Up)
    .State(State.Jumping)
    .Note("some help message here")
    .OnExit((data) => {
        Debug.Log("on exit jumping");
    })
    .To(State.Standing).On(Action.Down)
    .Build();

    _fsm.Trigger(Action.Down);
}

Conditions

You can also add condition(s) to transitions. When the trigger is called, it checks if all conditions are met, and if so, the transition is allowed.

State(State.Standing).To(State.Jumping).On(Action.Space).If(YOUR_CONDITION)

UML

FSM allows you to generate code with UML structure:

@startuml
skin rose
title TestFSM
left to right direction
agent Standing
agent Sitting
agent Lying
agent Jumping
note left of Jumping
some help message here
end note
Start --> Standing
Standing --> Sitting : Down
Standing ~~> Jumping : Space
Sitting --> Lying : Down
Sitting --> Standing : Up
Lying --> Sitting : Up
Jumping --> Standing : Down
@enduml

And you can render it on https://www.planttext.com or http://www.plantuml.com/plantuml/uml/ and check your FSM for correct transitions and triggers:

UML diagram

Important

Dotted lines are transitions configured with conditions.

Note

You can also pass a file with UML content and render the diagram at url: http://www.plantuml.com/plantuml/proxy?src=XXX, where XXX is the url to your *.puml file.

Demo

See the sample section to get a demo app. This demo contains a simple FSM example with a small number of states and triggers.

Tested in Unity 2022.3.x.

🏗️ Contributing

We invite you to contribute and help improve FSM for Shardy. Please see contributing document. 🤗

You also can contribute to the Shardy project by:

  • Helping other users
  • Monitoring the issue queue
  • Sharing it to your socials
  • Referring it in your projects

🤝 Support

You can support Shardy by using any of the ways below:

  • Bitcoin (BTC): 1VccPXdHeiUofzEj4hPfvVbdnzoKkX8TJ
  • USDT (TRC20): TMHacMp461jHH2SHJQn8VkzCPNEMrFno7m
  • TON: UQDVp346KxR6XxFeYc3ksZ_jOuYjztg7b4lEs6ulEWYmJb0f
  • Visa, Mastercard via Boosty
  • MIR via CloudTips

✉️ Contact

Before you ask a question, it is best to search for existing issues that might help you. Anyway, you can ask any questions and send suggestions by email or Telegram.

🔑 License

FSM for Shardy is licensed under the MIT License. Use it for free and be happy. 🎉