Bevy Book: Plugins

This commit is contained in:
Danny McGee
2023-09-27 11:33:55 -04:00
parent dbf00340ac
commit 4585fdec24
2 changed files with 42 additions and 26 deletions
+4 -26
View File
@@ -1,36 +1,14 @@
use bevy::prelude::*;
mod people;
fn main() {
App::new()
.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, greet_people))
.add_plugins((DefaultPlugins, people::Plugin))
.add_systems(Startup, hello_world)
.run();
}
#[derive(Component)]
struct Person;
#[derive(Component)]
struct Name(String);
impl Name {
fn new<S: Into<String>>(name: S) -> Self {
Self(name.into())
}
}
fn add_people(mut cmds: Commands) {
cmds.spawn((Person, Name::new("Elaina Proctor")));
cmds.spawn((Person, Name::new("Renzo Hume")));
cmds.spawn((Person, Name::new("Zayna Nieves")));
}
fn hello_world() {
println!("Hello, world!");
}
fn greet_people(q: Query<&Name, With<Person>>) {
for name in &q {
println!("Hello, {}!", name.0);
}
}
+38
View File
@@ -0,0 +1,38 @@
use bevy::{
app::{Startup, Update},
prelude::{App, Commands, Component, Query, With},
};
pub struct Plugin;
impl bevy::app::Plugin for Plugin {
fn build(&self, app: &mut App) {
#[rustfmt::skip]
app.add_systems(Startup, startup)
.add_systems(Update, greet);
}
}
#[derive(Component)]
struct Person;
#[derive(Component)]
struct Name(String);
impl Name {
fn new<S: Into<String>>(name: S) -> Self {
Self(name.into())
}
}
fn startup(mut cmds: Commands) {
cmds.spawn((Person, Name::new("Elaina Proctor")));
cmds.spawn((Person, Name::new("Renzo Hume")));
cmds.spawn((Person, Name::new("Zayna Nieves")));
}
fn greet(q: Query<&Name, With<Person>>) {
for name in &q {
println!("Hello, {}!", name.0);
}
}