completed first steps

This commit is contained in:
nek0 2024-04-21 19:16:03 +02:00
parent 90ea7c8b92
commit 4d48236931

View file

@ -2,16 +2,10 @@ use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, (update_people, greet_people).chain()))
.add_plugins((DefaultPlugins, HelloPlugin))
.run();
}
fn hello_world() {
println!("hi!");
}
#[derive(Component)]
struct Person;
@ -24,9 +18,18 @@ fn add_people(mut commands: Commands) {
commands.spawn((Person, Name("Marie Skłodowska".to_string())));
}
fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
#[derive(Resource)]
struct GreetTimer(Timer);
fn greet_people(
time: Res<Time>,
mut timer: ResMut<GreetTimer>,
query: Query<&Name, With<Person>>
) {
if timer.0.tick(time.delta()).just_finished() {
for name in &query {
println!("hello {}!", name.0);
}
}
}
@ -38,3 +41,13 @@ fn update_people(mut query: Query<&mut Name, With<Person>>) {
}
}
}
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
.add_systems(Startup,add_people)
.add_systems(Update, (update_people, greet_people).chain());
}
}