From 9e66728597a7554d37dd30b4e7a99b08a2d9f02e Mon Sep 17 00:00:00 2001 From: Danny McGee Date: Wed, 27 Sep 2023 11:46:02 -0400 Subject: [PATCH] Bevy Book: Resources --- hello-world/src/people.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/hello-world/src/people.rs b/hello-world/src/people.rs index dd54fd9..e2c066e 100644 --- a/hello-world/src/people.rs +++ b/hello-world/src/people.rs @@ -1,15 +1,20 @@ +use std::time::Duration; + use bevy::{ - app::{Startup, Update}, - prelude::{App, Commands, Component, Query, With}, + prelude::{App, Commands, Component, Query, Res, ResMut, Resource, Startup, Update, With}, + time::{Time, TimerMode}, }; 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); + app.insert_resource(Timer(bevy::time::Timer::from_seconds( + 2.0, + TimerMode::Repeating, + ))) + .add_systems(Startup, startup) + .add_systems(Update, greet); } } @@ -25,14 +30,25 @@ impl Name { } } +#[derive(Resource)] +struct Timer(bevy::time::Timer); + +impl Timer { + fn tick(&mut self, delta: Duration) -> &bevy::time::Timer { + self.0.tick(delta) + } +} + 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>) { - for name in &q { - println!("Hello, {}!", name.0); +fn greet(time: Res