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(); .run();
} }
#[derive(Resource)]
struct GreetTimer(Timer);
#[derive(Component)] #[derive(Component)]
struct Name(String); struct Name(String);
@ -27,10 +30,12 @@ fn update_people(mut query: Query<&mut Name, With<Person>>) {
} }
} }
fn greet_people(query: Query<&Name, With<Person>>) { 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 { for name in &query {
println!("hello {}!", name.0); println!("hello {}!", name.0);
} }
}
} }
fn hello_world() { fn hello_world() {
@ -40,7 +45,8 @@ fn hello_world() {
pub struct HelloPlugin; pub struct HelloPlugin;
impl Plugin for HelloPlugin { impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) { 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())); .add_systems(Update, (hello_world, (update_people, greet_people).chain()));
} }
} }