8 个版本 (5 个重大更新)
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.0 | 2023年2月11日 |
#1620 in 游戏开发
每月下载量 53 次
用于 pecs
64KB
1K SLoC
关于
pecs
是 Bevy 的一个插件,允许你在 Bevy 的 ecs
环境中通过链式多个 Promise 来异步执行代码。
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()
进行 Promise 链式 - 状态传递(对于 Promise 的
state
相当于对于项目中的self
)。 - 完整的类型推断(下一个 Promise 知道前一个结果的数据类型)。
- 内置计时器、UI 和 HTTP Promise,通过无状态的
asyn
模块和有状态的state.asyn()
方法。 - 自定义 Promise 注册(添加您想要的任何异步函数!)。
- 系统参数 获取(Promise
asyn!
函数接受与 Bevy 系统相同的参数)。 - 嵌套 Promise(显然是通过链式)。
- 通过无状态的
Promise::any()
/Promise::all()
方法或状态化的state.any()
/state.all()
方法,结合 promises 和any/all
用于 promises 的元组/vec。 - 通过
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
进行中
这个 crate 还很年轻。API 可能会变化。应用程序可能会崩溃。一些 promises 可能会静默失效。文档不完整。
但是。但是。示例工作得很好。这个事实给了我们很多希望。
许可证
pecs
可根据以下任一许可证双重授权:
- MIT 许可证 (LICENSE-MIT 或 http://opensource.org/licenses/MIT)
- Apache 许可证,版本 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
这意味着您可以选择您喜欢的许可证!这种双重许可方法在 Rust 生态系统中的事实标准,并且包含 非常好的理由 来包含两者。
lib.rs
:
通过 ehttp
异步执行 http
请求
依赖关系
~24–52MB
~857K SLoC