介绍
bevy是一个用Rust编写的开源游戏引擎,旨在提供简单易用且高性能的游戏开发体验。它采用了数据驱动的设计理念,利用Rust的安全性和并发性,为开发者提供了强大的工具和功能。
环境前提
- Rust开发环境(包括cargo包管理器)
- 支持的操作系统(如Windows、macOS或Linux)
- 基本的Rust编程知识
创建新项目
使用cargo命令创建一个新的Rust项目:
cargo new bevy_learning-game1
cd bevy_learning-game1
添加bevy依赖
cargo add bevy
调试速度优化
默认配置调试速度较慢,加载资源时会有明显的卡顿。可以通过修改Cargo.toml文件来提升调试速度:
# Enable a small amount of optimization in the dev profile.
[profile.dev]
opt-level = 1
# Enable a large amount of optimization in the dev profile for dependencies.
[profile.dev.package."*"]
opt-level = 3
首次构建
首次构建bevy项目可能需要较长时间,因为需要下载和编译大量依赖包,后续构建速度会显著提升。
cargo build
修改main.rs
打开src/main.rs文件,替换为以下代码:
use bevy::prelude::*;
fn main() {
App::new().run();
}
运行项目
cargo run
运行后发现,无事发生,因为还没有添加任何实体或系统。
App结构
bevy应用程序的核心是App结构体,它管理游戏的生命周期、资源和系统(System)。通过向App添加系统(System)和资源,可以定义游戏的行为和状态。
- world:存储所有实体和组件的数据结构。
- schedule:管理系统(System)的执行顺序和调度。
- runner:控制广泛的执行流程,如主循环。
ECS简介
bevy采用ECS(Entity-Component-System)架构,这是一种数据驱动的设计模式,适用于游戏开发。ECS将游戏对象分解为实体(Entity)、组件(Component)和系统(System),使得游戏逻辑更加模块化和高效。
- 实体(Entity):游戏中的对象,可以是玩家、敌人、道具等,但本身不包含数据。具体到bevy中,实体是一个包含唯一数字的类型。
struct Entity(u64);
- 组件(Component):附加到实体上的数据,如位置、速度、外观。具体到bevy中,组件是结构体,定义了实体的属性。
#[derive(Component)]
struct Position {
x: f32,
y: f32,
}
- 系统(System):处理实体和组件的逻辑,如移动、渲染、碰撞检测。具体到bevy中,是函数,定义了如何处理特定类型的组件。
fn print_position_system(query: Query<&Position>) {
for position in &query {
println!("position: {} {}", position.x, position.y);
}
}
添加系统,实体和组件
修改main.rs,添加简单的系统,实体和组件:
新建一个组件
#[derive(Component)]
struct Person;
Person组件表示一个人。人员理论上可以有名字等属性,但是考虑到不止人员有名字属性,我们单独创建一个Name组件:
#[derive(Component)]
struct Name(String);
这里可以类比于rust的trait,Name组件表示一个名字特征,可以附加到任何实体上。
添加实体
创建一个函数,在应用启动时添加一些人员实体进world:
fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("熊大".to_string())));
commands.spawn((Person, Name("熊二".to_string())));
commands.spawn((Person, Name("光头强".to_string())));
}
添加系统
创建一个简单的系统,打印"hello world!":
fn hello_world() {
println!("hello world!");
}
创建一个系统,向所有人员打招呼:
fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
}
}
将系统加入App:
fn main() {
App::new()
.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, greet_people).chain())
.run();
}
- Startup System:在应用启动时运行一次的系统。
- Update System:在每一帧更新时运行的系统。
- chain():确保系统按顺序执行,前一个系统完成后再执行下一个系统。
完整代码
use bevy::prelude::*;
fn main() {
App::new()
.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, greet_people).chain())
.run();
}
fn hello_world() {
println!("hello world!");
}
#[derive(Component)]
struct Person;
#[derive(Component)]
struct Name(String);
fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("熊大".to_string())));
commands.spawn((Person, Name("熊二".to_string())));
commands.spawn((Person, Name("光头强".to_string())));
}
fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
}
}
运行项目:
cargo run
控制台会输出
hello world!
hello 熊大!
hello 熊二!
hello 光头强!
系统成功运行。
使用Plugins
bevy提供了丰富的插件(Plugins),用于扩展引擎功能,如渲染、物理、音频等。可以通过添加预定义的插件或自定义插件来增强应用程序,例如UIPlugin用于创建用户界面。 还可以通过添加插件组来启用一组相关功能,例如DefaultPlugins包含了bevy的核心功能和常用插件。
更改main.rs,添加DefaultPlugins:
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, greet_people).chain())
.run();
}
这将启用bevy的核心功能,如窗口管理、渲染和输入处理。 运行项目,会弹出一个窗口,同时在控制台不停的输出之前的问候信息。
构建Plugin
通过实现Plugin trait,可以创建自定义插件:
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, add_people);
app.add_systems(Update, (hello_world, greet_people).chain());
}
}
将插件添加到App:
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(HelloPlugin)
.run();
}
运行项目,功能与之前相同,但代码结构更清晰,便于维护和扩展。
使用资源(Resources)
资源是全局单例数据,可以在系统之间共享。通过定义结构体并将其注册为资源,可以轻松管理应用程序的状态和配置。 资源的访问方式与组件类似,但资源是全局的,而组件是附加到实体上的。例如访问一个存在于DefaultPlugins中的Time资源:
fn greet_people(time: Res<Time>, query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
}
}
实现2秒钟打招呼一次
Time上的delta字段为我们提供了自上次更新以来经过的时间。但是,为了每两秒钟运行一次系统greet_people,必须跟踪一系列更新所经过的时间。为了使这更容易,Bevy提供了Timer类型。依次创建一个新的资源来使用计时器跟踪经过的时间:
#[derive(Resource)]
struct GreetTimer(Timer);
在应用启动时初始化资源:
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)));
app.add_systems(Startup, add_people);
app.add_systems(Update ,greet_people);
}
}
修改greet_people系统以使用计时器:
fn greet_people(time: Res<Time>, mut timer: ResMut<GreetTimer>, query: Query<&Name, With<Person>>) {
// update our timer with the time elapsed since the last update
// if that caused the timer to finish, we say hello to everyone
if timer.0.tick(time.delta()).just_finished() {
for name in &query {
println!("hello {}!", name.0);
}
}
}
运行项目,控制台每两秒钟输出一次问候信息。
总结
bevy的功能初次体验到此结束。学习了一下bevy的基本概念和使用方法,包括创建项目、添加依赖、理解App结构、ECS架构、系统和组件的使用、插件机制以及资源管理。通过这些基础知识,可以开始构建简单的游戏应用程序,并逐步探索bevy提供的更多高级功能和特性。