【游戏引擎】Bevy HelloWorld

414 阅读1分钟

环境配置

先参照文档做好初步配置 Bevy - Setup (bevyengine.org)

接下来就是精通 hello world 的时候了:

use bevy::prelude::*;

fn main() {
    App::new().run();
}

问题 - IDE 断点配置失败

使用 VSCode 的 launch.json 配置断点出错, 错误信息:

dyld[10150]: Library not loaded: '@rpath/libstd-2b42fd6bb1400760.dylib'

🎉 解决方案

在 VSCode 的 launch.json 中引入环境变量:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Cargo launch",
            "type": "lldb",
            "request": "launch",
            "cargo": {
                "args": [
                    "build",
                    "--features=bevy/dynamic"
                ],
            },
            "env": {
                "DYLD_LIBRARY_PATH": "target/debug/deps:/Users/zekaiwang/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib:$DYLD_LIBRARY_PATH"
            },
            "cwd": "${workspaceFolder}",
            "args": []
        },
    ]
}

Hello world

我精通十几门语言和无数工具的 helloworld .jpg

use bevy::prelude::*;

#[derive(Component)]
struct Person;

#[derive(Component)]
struct Name(String);

fn add_people(mut commands: Commands) {
    commands.spawn()
        .insert(Person)
        .insert(Name("David".to_string()));
    commands.spawn()
        .insert(Person)
        .insert(Name("Alice".to_string()));
}

fn greet_people(query: Query<&Name, With<Person>>) {
    for name in query.iter() {
        println!("Hello {}!", name.0);
    }
}

pub struct HelloPlugin;

impl Plugin for HelloPlugin {
    fn build(&self, app: &mut App) {
        app.add_startup_system(add_people)
            .add_system(greet_people);
    }
}

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_plugin(HelloPlugin)
        .run();
}

这会启动一个灰色的窗口, 并不停地在命令行打印 Hello 信息. 操作过程的讲解可以在官方文档的对应章节找到, 这里将所有的实现全部贴了出来.

资料夹 📁