From e1c2ace1f27d2c11fec9036053ddfbcac38c060c Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Thu, 14 Apr 2022 19:30:36 +0000 Subject: [PATCH] More tidying of contributors example (#4443) # Objective Continue the effort to clean up this example ## Solution - Store contributor name as component to avoid awkward vec of tuples - Name the variable storing the Random Number Generator "rng" - Use init_resource for resource implementing default - Fix a few spots where an Entity was unnecessarily referenced and immediately dereferenced - Fix up an awkward comment --- examples/games/contributors.rs | 46 ++++++++++++++++------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index aba9d7970b..7cc038e06a 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -15,7 +15,7 @@ fn main() { .add_system(move_system) .add_system(collision_system) .add_system(select_system) - .insert_resource(SelectionState::default()) + .init_resource::() .run(); } @@ -23,7 +23,7 @@ fn main() { type Contributors = HashSet; struct ContributorSelection { - order: Vec<(String, Entity)>, + order: Vec, idx: usize, } @@ -46,6 +46,7 @@ struct ContributorDisplay; #[derive(Component)] struct Contributor { + name: String, hue: f32, } @@ -85,23 +86,23 @@ fn setup_contributor_selection(mut commands: Commands, asset_server: Res, mut query: Query<(&mut Velocity, &mut Transform), With>, ) { - let mut rnd = rand::thread_rng(); + let mut rng = rand::thread_rng(); let window = windows.primary(); @@ -277,7 +275,7 @@ fn collision_system( if bottom < ground { transform.translation.y = ground + SPRITE_SIZE / 2.0; // apply an impulse upwards - velocity.translation.y = rnd.gen_range(700.0..1000.0); + velocity.translation.y = rng.gen_range(700.0..1000.0); } if top > ceiling { transform.translation.y = ceiling - SPRITE_SIZE / 2.0;