Crate bevy_simple_state_machine

Source
Expand description

§Bevy Simple State Machine

Plugin for the Bevy Engine which implements a rudimentary animation state machine system.

To use this, you have to add the SimpleStateMachinePlugin to you app

App::new()
    .add_plugins(DefaultPlugins)
    .add_plugins(SimpleStateMachinePlugin::new());

And then insert an AnimationStateMachine component on your entities

fn setup(mut commands: Commands) {
    let starting_state = "idle";
    let my_states_map = HashMap::from([
        ("idle", AnimationState{
            name: "idle".to_string(),
            clip: idle_clip_handle,
            interruptible: true,
        }),
        ("run", AnimationState{
            name: "run".to_string(),
            clip: run_clip_handle,
            interruptible: true,
        }),
    ]);
    let my_states_transitions_vec = vec![
        StateMachineTransition::immediate(
            AnimationStateRef::from_string("idle"),
            AnimationStateRef::from_string("run"),
            StateMachineTrigger::from(|vars| vars["run"].is_bool(true)),
        ),
   ];
    let state_machine_vars = HashMap::from([
        ("run", StateMachineVariableType::Bool(false)),    
    ]);
      
    commands.spawn(SpatialBundle::default())
        .insert(AnimationPlayer::default())
        .insert(AnimationStateMachine::new(
            starting_state,
            my_states_map,
            my_states_transitions_vec,
            state_machine_vars,
        ));
}

And then you can control it changing the values of the state machine variables

state_machine.update_variable("run", StateMachineVariableType::Bool(true));

§Currently supported features:

  • Custom transition conditions
  • Transitions from wildcard state AnyState
  • Events emitted on transition end
  • Internal state machine variables

Currently, transitions end on the same frame they are triggered.

Structs§

AnimationState
AnimationStateMachine state structure
AnimationStateMachine
Main state machine component
SimpleStateMachinePlugin
Plugin that handles all state machine executions
StateMachineTransition
Transition from AnimationState A to AnimationState B
TransitionEndedEvent
Event emitted once a StateMachineTransition has been executed

Enums§

AnimationStateRef
Reference to an AnimationState name
StateMachineSet
State machine system label
StateMachineTrigger
Trigger condition for a StateMachineTransition
StateMachineVariableType
State machine variable type

Type Aliases§

StateMachineVariables
Internal state machine variables map type