From ca80fe65eddffd554bd297581c31c7e936a87b6e Mon Sep 17 00:00:00 2001 From: Rostyslav Toch Date: Wed, 8 Dec 2021 20:09:53 +0000 Subject: [PATCH] Improve contributors example quality (#3258) # Objective Fixes #3181 ## Solution Refactored `contributors.rs` example: - Renamed unclear variables - Split setup system into two separate systems Co-authored-by: CrazyRoka --- examples/2d/contributors.rs | 206 ++++++++++++++++++++++-------------- 1 file changed, 124 insertions(+), 82 deletions(-) diff --git a/examples/2d/contributors.rs b/examples/2d/contributors.rs index 74040446aadbe..1cd9a99eaeb7f 100644 --- a/examples/2d/contributors.rs +++ b/examples/2d/contributors.rs @@ -1,14 +1,15 @@ -use bevy::prelude::*; +use bevy::{prelude::*, utils::HashSet}; use rand::{prelude::SliceRandom, Rng}; use std::{ - collections::BTreeSet, - io::{BufRead, BufReader}, + env::VarError, + io::{self, BufRead, BufReader}, process::Stdio, }; fn main() { App::new() .add_plugins(DefaultPlugins) + .add_startup_system(setup_contributor_selection) .add_startup_system(setup) .add_system(velocity_system) .add_system(move_system) @@ -17,7 +18,8 @@ fn main() { .run(); } -type Contributors = BTreeSet; +// Store contributors in a collection that preserves the uniqueness +type Contributors = HashSet; struct ContributorSelection { order: Vec<(String, Entity)>, @@ -52,19 +54,25 @@ const ALPHA: f32 = 0.92; const SHOWCASE_TIMER_SECS: f32 = 3.0; -fn setup( +const CONTRIBUTORS_LIST: &[&str] = &["Carter Anderson", "And Many More"]; + +fn setup_contributor_selection( mut commands: Commands, asset_server: Res, mut materials: ResMut>, ) { - let contribs = contributors(); + // Load contributors from the git history log or use default values from + // the constant array. Contributors must be unique, so they are stored in a HashSet + let contribs = contributors().unwrap_or_else(|_| { + CONTRIBUTORS_LIST + .iter() + .map(|name| name.to_string()) + .collect() + }); let texture_handle = asset_server.load("branding/icon.png"); - commands.spawn_bundle(OrthographicCameraBundle::new_2d()); - commands.spawn_bundle(UiCameraBundle::default()); - - let mut sel = ContributorSelection { + let mut contributor_selection = ContributorSelection { order: vec![], idx: 0, }; @@ -82,7 +90,7 @@ fn setup( let transform = Transform::from_xyz(pos.0, pos.1, 0.0); - let e = commands + let entity = commands .spawn() .insert_bundle(( Contributor { hue }, @@ -107,10 +115,17 @@ fn setup( }) .id(); - sel.order.push((name, e)); + contributor_selection.order.push((name, entity)); } - sel.order.shuffle(&mut rnd); + contributor_selection.order.shuffle(&mut rnd); + + commands.insert_resource(contributor_selection); +} + +fn setup(mut commands: Commands, asset_server: Res) { + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); + commands.spawn_bundle(UiCameraBundle::default()); commands.spawn_bundle((SelectTimer, Timer::from_seconds(SHOWCASE_TIMER_SECS, true))); @@ -145,25 +160,23 @@ fn setup( }, ..Default::default() }); - - commands.insert_resource(sel); } /// Finds the next contributor to display and selects the entity fn select_system( mut materials: ResMut>, - mut sel: ResMut, - mut dq: Query<&mut Text, With>, - mut tq: Query<&mut Timer, With>, - mut q: Query<(&Contributor, &Handle, &mut Transform)>, + mut contributor_selection: ResMut, + mut text_query: Query<&mut Text, With>, + mut timer_query: Query<&mut Timer, With>, + mut query: Query<(&Contributor, &Handle, &mut Transform)>, time: Res