tutorial: timer
This commit is contained in:
parent
6ed2169efe
commit
0b6b03a9c1
10
src/main.rs
10
src/main.rs
|
@ -6,6 +6,9 @@ fn main() {
|
|||
.run();
|
||||
}
|
||||
|
||||
#[derive(Resource)]
|
||||
struct GreetTimer(Timer);
|
||||
|
||||
#[derive(Component)]
|
||||
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 {
|
||||
println!("hello {}!", name.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn hello_world() {
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue