Skip to content

Commit

Permalink
Avoid using .config
Browse files Browse the repository at this point in the history
  • Loading branch information
DJMcNab committed Oct 16, 2021
1 parent 5d56aec commit 974409a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
14 changes: 9 additions & 5 deletions examples/2d/contributors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ fn main() {
.add_system(velocity_system)
.add_system(move_system)
.add_system(collision_system)
.add_system(select_system.config(|((), timer, (), (), (), ())| {
*timer = Some(Timer::from_seconds(SHOWCASE_TIMER_SECS, true))
}))
.insert_resource(ChangeSelectContributorTimer(Timer::from_seconds(
SHOWCASE_TIMER_SECS,
true,
)))
.add_system(select_system)
.run();
}

type Contributors = BTreeSet<String>;

struct ChangeSelectContributorTimer(Timer);

struct ContributorSelection {
order: Vec<(String, Entity)>,
idx: usize,
Expand Down Expand Up @@ -149,13 +153,13 @@ fn setup(
/// Finds the next contributor to display and selects the entity
fn select_system(
mut materials: ResMut<Assets<ColorMaterial>>,
mut timer: Local<Timer>,
mut timer: ResMut<ChangeSelectContributorTimer>,
mut sel: ResMut<ContributorSelection>,
mut dq: Query<&mut Text, With<ContributorDisplay>>,
mut q: Query<(&Contributor, &Handle<ColorMaterial>, &mut Transform)>,
time: Res<Time>,
) {
if !timer.tick(time.delta()).just_finished() {
if !timer.0.tick(time.delta()).just_finished() {
return;
}

Expand Down
19 changes: 11 additions & 8 deletions examples/2d/many_sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ fn main() {
})
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(
print_sprite_count
.config(|((), timer, ())| *timer = Some(Timer::from_seconds(1.0, true)))
.label("Tick"),
)
.insert_resource(PrintSpriteCountTimer(Timer::from_seconds(1.0, true)))
.add_system(print_sprite_count.label("Tick"))
.add_system(move_camera.after("Tick"))
.run()
}

struct PrintSpriteCountTimer(Timer);

fn setup(
mut commands: Commands,
assets: Res<AssetServer>,
Expand Down Expand Up @@ -85,10 +84,14 @@ fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Cam
}

// System for printing the number of sprites on every tick of the timer
fn print_sprite_count(time: Res<Time>, mut timer: Local<Timer>, sprites: Query<&Sprite>) {
timer.tick(time.delta());
fn print_sprite_count(
time: Res<Time>,
mut timer: ResMut<PrintSpriteCountTimer>,
sprites: Query<&Sprite>,
) {
timer.0.tick(time.delta());

if timer.just_finished() {
if timer.0.just_finished() {
info!("Sprites: {}", sprites.iter().count(),);
}
}

0 comments on commit 974409a

Please sign in to comment.