系统

可参考的官方例子: ecs_guide, startup_system, system_param.


系统是你编写的函数,由 Bevy 运行。

这是你实现所有游戏逻辑的地方。

这些函数只能接受特殊的参数类型,以指定你想要访问的内容。如果你在你的函数中使用不支持的参数类型,你会得到令人疑惑的编译器错误

以下是一些可选项:

fn debug_start(
    // access resource
    start: Res<StartingLevel>
) {
    eprintln!("Starting on level {:?}", *start);
}

System parameters can be grouped into tuples (which can be nested). This is useful for organization.

fn complex_system(
    (a, mut b): (Res<ResourceA>, ResMut<ResourceB>),
    // this resource might not exist, so wrap it in an Option
    mut c: Option<ResMut<ResourceC>>,
) {
    if let Some(mut c) = c {
        // do something
    }
}

你的 system 函数总共最多可以有 16 个参数,如果你需要更多参数,用元组把它们包起来解决这个限制。单个元组最多可以包含 16 个成员,但可以无限地嵌套。

Runtime

To run your systems, you need to add them to Bevy via the app builder:

    fn main() {
        App::new()
            // ...

            // run it only once at launch
            .add_startup_system(init_menu)
            .add_startup_system(debug_start)

            // run it every frame update
            .add_system(move_player)
            .add_system(enemies_ai)

            // ...
            .run();
    }

The above is enough for simple projects.

As your project grows more complex, you might want to enhance your app builder with some of the powerful tools that Bevy offers for managing when/how your systems run, such as: explicit ordering with labels, system sets, states, run criteria, and stages.