#bevy #gamedev #game

bevy_wasm_sys

将其导入您的 wasm 脚本。与 bevy_wasm crate 一起使用。

12 个版本

0.10.1 2023 年 3 月 21 日
0.10.0 2023 年 3 月 6 日
0.9.9 2023 年 2 月 13 日
0.9.7 2022 年 12 月 30 日
0.1.4 2022 年 12 月 10 日

905WebAssembly 中排名

Download history 1/week @ 2024-03-09 65/week @ 2024-03-30 14/week @ 2024-04-06

每月 61 次下载

MIT/Apache

52KB
665

Bevy WASM

使用 WebAssembly 修改您的 Bevy 游戏!

CI MIT/Apache 2.0 Crates.io
Bevy

bevy_wasm 用于游戏
bevy_wasm_sys 用于插件
bevy_wasm_shared 用于协议

请参阅 examples/cubes 以了解如何使用此功能的全面示例。

更新日志

协议

我们的协议 crate 定义了游戏和插件之间通信的两个消息类型。

[dependencies]
bevy_wasm_shared = "0.10"
serde = { version = "1.0", features = ["derive"] }
use bevy_wasm_shared::prelude::*;
use serde::{Deserialize, Serialize};

/// The version of the protocol. Automatically set from the `CARGO_PKG_XXX` environment variables.
pub const PROTOCOL_VERSION: Version = version!();

/// A message to be sent Mod -> Game.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ModMessage {
    Hello,
}

/// A message to be sent Game -> Mod.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum GameMessage {
    HiThere,
}

游戏

我们的游戏将从 bevy_wasm 中导入 WasmPlugin,并使用它自动发送和接收与插件的消息。

[dependencies]
bevy = "0.10"
bevy_wasm = "0.10"
my_game_protocol = { git = "https://github.com/username/my_game_protocol" }
use bevy::prelude::*;
use bevy_wasm::prelude::*;
use my_game_protocol::{GameMessage, ModMessage, PROTOCOL_VERSION};

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(WasmPlugin::<GameMessage, ModMessage>::new(PROTOCOL_VERSION))
        .add_startup_system(add_mods)
        .add_system(listen_for_mod_messages)
        .add_system(send_messages_to_mods)
        .run();
}

fn add_mods(mut commands: Commands, asset_server: Res<AssetServer>) {
    commands.spawn(WasmMod {
        wasm: asset_server.load("some_mod.wasm"),
    });
    commands.spawn(WasmMod {
        wasm: asset_server.load("some_other_mod.wasm"),
    })
}

fn listen_for_mod_messages(mut events: EventReader<ModMessage>) {
    for event in events.iter() {
        match event {
            ModMessage::Hello => {
                println!("The mod said hello!");
            }
        }
    }
}

fn send_messages_to_mods(mut events: EventWriter<GameMessage>) {
    events.send(GameMessage::HiThere);
}

插件

我们的插件将从 bevy_wasm_sys 中导入 FFIPlugin,并使用它自动发送和接收与游戏的消息。

[dependencies]
bevy_wasm_sys = "0.10"
my_game_protocol = { git = "https://github.com/username/my_game_protocol" }
use bevy_wasm_sys::prelude::*;
use my_game_protocol::{GameMessage, ModMessage, PROTOCOL_VERSION};

#[no_mangle]
pub unsafe extern "C" fn build_app() {
    App::new()
        .add_plugin(FFIPlugin::<GameMessage, ModMessage>::new(PROTOCOL_VERSION))
        .add_system(listen_for_game_messages)
        .add_system(send_messages_to_game)
        .run();
}

fn listen_for_game_messages(mut events: EventReader<GameMessage>) {
    for event in events.iter() {
        match event {
            GameMessage::HiThere => {
                println!("The game said hi there!");
            }
        }
    }
}

fn send_messages_to_game(mut events: EventWriter<ModMessage>) {
    events.send(ModMessage::Hello);
}

共享资源

协议

#[derive(Resource, Default, Serialize, Deserialize)]
pub struct MyResource {
    pub value: i32,
}

游戏

App::new()
    ...
    .add_resource(MyResource { value: 0 })
    .add_plugin(
        WasmPlugin::<GameMessage, ModMessage>::new(PROTOCOL_VERSION)
            .share_resource::<MyResource>()
    )
    .add_system(change_resource_value)
    ...

fn change_resource_value(mut resource: ResMut<MyResource>) {
    resource.value += 1;
}

插件

App::new()
    ...
    .add_plugin(FFIPlugin::<GameMessage, ModMessage>::new(PROTOCOL_VERSION))
    .add_startup_system(setup)
    .add_system(print_resource_value)
    ...

fn setup(mut extern_resource: ResMut<ExternResources>) {
    extern_resources.insert::<MyResource>();
}

fn print_resource_value(resource: ExternRes<MyResource>) {
    println!("MyResource value: {}", resource.value);
}

请参阅 examples/shared_resources 以了解完整示例。

路线图

游戏中的 wasmtime 运行时
从插件发送消息到游戏
从游戏发送消息到插件
多插件支持
时间管理
协议版本检查
外部资源
启动系统插件加载
直接更新控制
插件卸载
插件识别(事件不是广播到所有插件)
浏览器支持
外部查询
同步时间
插件热加载
自动组件同步

许可

Bevy WASM 是免费、开源且许可宽松的!除非另有说明(以下或单个文件中),此存储库中的所有代码均受以下协议的双重许可:

由您选择。这意味着您可以选择您偏好的许可协议!这种双重许可方法在Rust生态系统中是事实上的标准,并且存在很好的理由来包含两者。

您的贡献

除非您明确声明,否则您根据Apache-2.0许可证定义的任何有意提交以包含在作品中的贡献,将按照上述方式双重许可,没有任何额外的条款或条件。

依赖关系

~0.3–10MB
~99K SLoC