#animation #bevy #gamedev

bevy-simple-state-machine

为Bevy提供的简单动画状态机系统

6个版本 (破坏性)

0.6.0 2024年2月17日
0.5.0 2023年11月4日
0.4.0 2023年7月27日
0.3.0 2023年3月10日
0.1.0 2022年8月7日

#656 in 游戏开发

每月 35次下载

MIT/Apache

23KB
280

Bevy Simple State Machine

Crates.io docs MIT/Apache 2.0

为Bevy引擎提供的插件,实现了简单的动画状态机系统。

要使用此插件,您需要在您的应用中添加SimpleStateMachinePlugin

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

然后在您的实体上插入一个AnimationStateMachine组件

fn setup(mut commands: Commands) {
    let starting_state = "idle";
    let my_states_map = HashMap::from([
        ("idle".to_string(), AnimationState{
            name: "idle".to_string(),
            clip: idle_clip_handle,
            interruptible: true,
        }),
        ("run".to_string(), 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".to_string(), StateMachineVariableType::Bool(false)),    
    ]);
     
    commands.spawn_bundle(SpatialBundle::default())
        .insert(AnimationPlayer::default())
        .insert(AnimationStateMachine::new(
            starting_state,
            my_states_map,
            my_states_transitions_vec,
            state_machine_vars,
        ));
}

然后您可以通过更改状态机变量的值来控制它

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

当前支持的功能

  • 自定义转换条件
  • 从通配符状态AnyState转换
  • 在转换结束时发出事件
  • 内部状态机变量

目前,转换在触发时同一帧结束。


Bevy兼容性

Bevy版本 插件版本
0.13 main
0.13 0.6.0
0.12 0.5.0
0.11 0.4.0
0.10 0.3.0
0.9 0.2.0
0.8 0.1.0

依赖项

~18–45MB
~720K SLoC