tutorial: timer

This commit is contained in:
yuni 2024-03-16 15:21:51 +01:00
parent 6ed2169efe
commit 0b6b03a9c1

View file

@ -6,6 +6,9 @@ fn main() {
.run();
}
#[derive(Resource)]
struct GreetTimer(Timer);
#[derive(Component)]
struct Name(String);
@ -27,9 +30,11 @@ fn update_people(mut query: Query<&mut Name, With<Person>>) {
}
}
fn greet_people(query: Query<&Name, With<Person>>) {
for name in &query {
println!("hello {}!", name.0);
fn greet_people(query: Query<&Name, With<Person>>, time:Res<Time>, mut timer: ResMut<GreetTimer>) {
if timer.0.tick(time.delta()).just_finished() {
for name in &query {
println!("hello {}!", name.0);
}
}
}
@ -40,7 +45,8 @@ fn hello_world() {
pub struct HelloPlugin;
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, add_people)
app.insert_resource(GreetTimer(Timer::from_seconds(2.0, TimerMode::Repeating)))
.add_systems(Startup, add_people)
.add_systems(Update, (hello_world, (update_people, greet_people).chain()));
}
}