7 个版本 (4 个破坏性更改)

0.6.0 2024年3月18日
0.5.0 2023年11月21日
0.4.0 2023年7月25日
0.3.0 2023年3月21日
0.1.2 2023年2月11日

#494游戏开发

MIT/Apache

4MB
1.5K SLoC

crates.io MIT/Apache 2.0 Bevy tracking docs.rs

关于

pecs 是一个 Bevy 插件,允许您通过链式多个承诺作为 Bevy 的 ecs 环境的一部分来异步执行代码。

pecs 代表 Promise Entity Component System

资源

兼容性

bevy pecs
0.13 0.6
0.12 0.5
0.11 0.4
0.10 0.3
0.9 0.2

功能

  • 使用 then()/then_repeat() 进行承诺链式操作
  • 状态传递(承诺的状态类似于项目的 self)。
  • 完整的类型推断(下一个承诺知道前一个结果的数据类型)。
  • 开箱即用的计时器、UI 和 HTTP 承诺通过无状态的 asyn 模块和有状态的 state.asyn() 方法。
  • 自定义承诺注册(添加任何您想要的异步函数!)。
  • 系统参数 获取(承诺 asyn! 函数接受与 Bevy 系统相同的参数)。
  • 嵌套承诺(显然是通过链式操作)。
  • 使用无状态的 Promise::any() /Promise::all() 方法或有状态的 state.any()/state.all() 方法将承诺与 any/all 结合。
  • 通过 with(value)/map(func)(在链式调用中改变状态类型/值)进行状态映射。
  • 通过 with_result(value)/map_result(func)(在链式调用中改变结果类型/值)进行结果映射。

示例

use bevy::prelude::*;
use pecs::prelude::*;
fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugins(PecsPlugin)
        .add_systems(Startup, setup)
        .run();
}

fn setup(mut commands: Commands, time: Res<Time>) {
    let start = time.elapsed_seconds();
    commands
        // create PromiseLike chainable commands
        // with the current time as state
        .promise(|| start)
        // will be executed right after current stage
        .then(asyn!(state => {
            info!("Wait a second..");
            state.asyn().timeout(1.0)
        }))
        // will be executed after in a second after previous call
        .then(asyn!(state => {
            info!("How large is is the Bevy main web page?");
            state.asyn().http().get("https://bevy.rust-lang.net.cn")
        }))
        // will be executed after request completes
        .then(asyn!(state, result => {
            match result {
                Ok(response) => info!("It is {} bytes!", response.bytes.len()),
                Err(err) => info!("Ahhh... something goes wrong: {err}")
            }
            state.pass()
        }))
        // will be executed right after the previous one
        .then(asyn!(state, time: Res<Time> => {
            let duration = time.elapsed_seconds() - state.value;
            info!("It took {duration:0.2}s to do this job.");
            info!("Exiting now");
            asyn::app::exit()
        }));
}

以下是上述示例的输出,请注意时间戳

18.667 INFO bevy_render::renderer: AdapterInfo { ... }
18.835 INFO simple: Wait a second..
19.842 INFO simple: How large is is the Bevy main web page?
19.924 INFO simple: It is 17759 bytes!
19.924 INFO simple: It tooks 1.09s to do this job.
19.924 INFO simple: Exiting now

正在进行中

这个包还很年轻。API 可能会更改。应用可能会崩溃。一些承诺可能会默默取消。文档不完整。

但是。但是。示例工作得很好。这个事实给了我们很多希望。

许可

pecs 可以根据以下任一许可进行双许可

这意味着您可以选择您喜欢的许可证!这种双许可方法在 Rust 生态系统中是事实上的标准,并且有 很好的理由 包括两者。

依赖项

~24–52MB
~859K SLoC