feat: print sprite names every 2 seconds

This commit is contained in:
Christopher Willis-Ford 2022-09-30 15:30:45 -07:00
parent 69b0de5d84
commit 1d03842f33

View file

@ -2,16 +2,11 @@ use bevy::prelude::*;
fn main() {
App::new()
.add_startup_system(add_default_sprites)
.add_system(hello_world)
.add_system(print_sprite_names)
.add_plugins(DefaultPlugins)
.add_plugin(HelloPlugin)
.run();
}
fn hello_world() {
println!("hello, world!");
}
#[derive(Component)]
struct Sprite;
@ -23,8 +18,23 @@ fn add_default_sprites(mut commands: Commands) {
commands.spawn().insert(Sprite).insert(Name("Sprite 2".to_string()));
}
fn print_sprite_names(query: Query<&Name, With<Sprite>>) {
for name in query.iter() {
println!("found a sprite named {}", name.0);
fn print_sprite_names(time: Res<Time>, mut timer: ResMut<SpriteNameTimer>, query: Query<&Name, With<Sprite>>) {
if timer.0.tick(time.delta()).just_finished() {
for name in query.iter() {
println!("found a sprite named {}", name.0);
}
}
}
pub struct HelloPlugin;
struct SpriteNameTimer(Timer);
impl Plugin for HelloPlugin {
fn build(&self, app: &mut App) {
app
.insert_resource(SpriteNameTimer(Timer::from_seconds(2.0, true)))
.add_startup_system(add_default_sprites)
.add_system(print_sprite_names);
}
}