mirror of
https://github.com/storytold/storyteller-bevy.git
synced 2026-10-21 07:55:55 +00:00
Bevy Book: Plugins
This commit is contained in:
+4
-26
@@ -1,36 +1,14 @@
|
|||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
mod people;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
App::new()
|
App::new()
|
||||||
.add_systems(Startup, add_people)
|
.add_plugins((DefaultPlugins, people::Plugin))
|
||||||
.add_systems(Update, (hello_world, greet_people))
|
.add_systems(Startup, hello_world)
|
||||||
.run();
|
.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() {
|
fn hello_world() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
}
|
}
|
||||||
|
|
||||||
fn greet_people(q: Query<&Name, With<Person>>) {
|
|
||||||
for name in &q {
|
|
||||||
println!("Hello, {}!", name.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user