HFSM (Hierarchical Finite State Machine) is a simple and flexible library for building state machines in C#. The library provides an easy-to-use API for defining states, transitions, and hierarchical relationships between states.
- Define states with custom behavior (OnEnter, OnUpdate, OnExit)
- Define transitions based on events or guard conditions
- Hierarchical state organization
- State machine builder for easy construction and configuration
- Event-driven state updates
To use the HFSM library, follow these steps:
- Define your state and event enums.
- Create a new
HFSMBuilder
instance, passing in the state and event enums as generic arguments. - Add states to the builder using the
AddState
method. - Define transitions between states using the
AddTransition
method. - Build the state machine using the
Build
method. - Start the state machine with the
Start
method. - Update the state machine using the
Update
method.
Here's an example:
// Define your state and event enums
public enum MyState { Root, Idle, Walk, Run }
public enum MyEvent { StartWalking, StartRunning, Stop }
// Create a new HFSMBuilder instance
var builder = new HFSMBuilder<MyState, MyEvent>();
// Add states to the builder
builder.AddState(MyState.Movement);
builder.AddState(MyState.Idle, MyState.Movement);
builder.AddState(MyState.Walk, MyState.Movement);
builder.AddState(MyState.Run, MyState.Movement);
// Define transitions between states
builder.AddTransition(MyState.Idle, MyState.Walk, MyEvent.StartWalking);
builder.AddTransition(MyState.Walk, MyState.Run, MyEvent.StartRunning);
builder.AddTransition(MyState.Run, MyState.Idle, MyEvent.Stop);
// Build the state machine
var hfsm = builder.Build();
// Start the state machine
hfsm.Start();
// Update the state machine
hfsm.Update();
Contributions to the HFSM library are welcome! To contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Make your changes and commit them to your branch.
- Create a pull request, providing a detailed description of your changes.
Before submitting your pull request, please make sure to test your changes and provide appropriate unit tests if applicable.