tutorial: plugins

This commit is contained in:
yuni 2024-03-16 15:13:00 +01:00
parent 36724b9602
commit 6ed2169efe

View file

@ -2,8 +2,7 @@ use bevy::prelude::*;
fn main() { fn main() {
App::new() App::new()
.add_systems(Startup, add_people) .add_plugins((DefaultPlugins, HelloPlugin))
.add_systems(Update, (hello_world, greet_people))
.run(); .run();
} }
@ -19,6 +18,15 @@ fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("Zayna Nieves".to_string()))); commands.spawn((Person, Name("Zayna Nieves".to_string())));
} }
fn update_people(mut query: Query<&mut Name, With<Person>>) {
for mut name in &mut query {
if name.0 == "Elaina Proctor" {
name.0 = "Elaina Hume".to_string();
break;
}
}
}
fn greet_people(query: Query<&Name, With<Person>>) { fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query { for name in &query {
println!("hello {}!", name.0); println!("hello {}!", name.0);
@ -28,3 +36,11 @@ fn greet_people(query: Query<&Name, With<Person>>) {
fn hello_world() { fn hello_world() {
println!("hello world!"); println!("hello world!");
} }
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, (update_people, greet_people).chain()));
}
}