资源
Entity
和 Component
数据类型非常适合表示复杂的可查询数据组。但大多数应用程序还需要某种“全局唯一”数据。在 Bevy ECS 中,我们使用 Resource
特性来表示全局唯一数据。
以下是一些可以作为 Resource
编码的数据示例
- 经过时间
- 资产集合(声音、纹理、网格)
- 渲染器
使用资源跟踪时间 #
让我们通过每两秒钟只打印一次“hello”来解决应用程序的“hello spam”问题。我们将使用 Time
资源,该资源通过 add_plugins(DefaultPlugins)
自动添加到我们的应用程序中。
为了简单起见,从您的应用程序中删除 hello_world
系统。这样我们只需要适应 greet_people
系统。
访问资源的方式与访问组件的方式非常相似。您可以在系统中像这样访问 Time
资源
fn greet_people(time: Res<Time>, query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
}
}
delta
字段在 Time
上为我们提供了自上次更新以来经过的时间。但是为了每两秒钟运行一次我们的系统,我们必须跟踪在一系列更新中经过的时间。为了简化操作,Bevy 提供了 Timer
类型。让我们创建一个新的资源来使用 Timer
跟踪经过的时间
#[derive(Resource)]
struct GreetTimer(Timer);
并在我们的系统中使用它
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);
}
}
}
现在剩下要做的就是将 GreetTimer
资源添加到我们的 HelloPlugin
中。使用 TimerMode::Repeating
使计时器重复。
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, (update_people, greet_people).chain());
}
}
现在 cargo run
该应用程序。它现在应该以合理的速率问候人们。