mirror of
https://github.com/scratchfoundation/scratch-tech-explorations.git
synced 2025-08-12 22:08:44 -04:00
feat: print sprite names every 2 seconds
This commit is contained in:
parent
69b0de5d84
commit
1d03842f33
1 changed files with 20 additions and 10 deletions
30
src/main.rs
30
src/main.rs
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue