diff --git a/examples/games/contributors.rs b/examples/games/contributors.rs index 4fd8309a2f..226d09df49 100644 --- a/examples/games/contributors.rs +++ b/examples/games/contributors.rs @@ -1,12 +1,14 @@ //! This example displays each contributor to the bevy source code as a bouncing bevy-ball. use bevy::{ + math::bounding::Aabb2d, prelude::*, - utils::{thiserror, HashSet}, + utils::{thiserror, HashMap}, }; use rand::{prelude::SliceRandom, Rng}; use std::{ env::VarError, + hash::{DefaultHasher, Hash, Hasher}, io::{self, BufRead, BufReader}, process::Stdio, }; @@ -14,22 +16,14 @@ use std::{ fn main() { App::new() .add_plugins(DefaultPlugins) - .init_resource::() + .init_resource::() .add_systems(Startup, (setup_contributor_selection, setup)) - .add_systems( - Update, - ( - velocity_system, - move_system, - collision_system, - select_system, - ), - ) + .add_systems(Update, (gravity, movement, collisions, selection)) .run(); } -// Store contributors in a collection that preserves the uniqueness -type Contributors = HashSet; +// Store contributors with their commit count in a collection that preserves the uniqueness +type Contributors = HashMap; #[derive(Resource)] struct ContributorSelection { @@ -38,17 +32,14 @@ struct ContributorSelection { } #[derive(Resource)] -struct SelectionState { - timer: Timer, - has_triggered: bool, -} +struct SelectionTimer(Timer); -impl Default for SelectionState { +impl Default for SelectionTimer { fn default() -> Self { - Self { - timer: Timer::from_seconds(SHOWCASE_TIMER_SECS, TimerMode::Repeating), - has_triggered: false, - } + Self(Timer::from_seconds( + SHOWCASE_TIMER_SECS, + TimerMode::Repeating, + )) } } @@ -58,6 +49,7 @@ struct ContributorDisplay; #[derive(Component)] struct Contributor { name: String, + num_commits: usize, hue: f32, } @@ -70,11 +62,8 @@ struct Velocity { const GRAVITY: f32 = 9.821 * 100.0; const SPRITE_SIZE: f32 = 75.0; -const SATURATION_DESELECTED: f32 = 0.3; -const LIGHTNESS_DESELECTED: f32 = 0.2; -const SATURATION_SELECTED: f32 = 0.9; -const LIGHTNESS_SELECTED: f32 = 0.7; -const ALPHA: f32 = 0.92; +const SELECTED: Hsla = Hsla::hsl(0.0, 0.9, 0.7); +const DESELECTED: Hsla = Hsla::new(0.0, 0.3, 0.2, 0.92); const SHOWCASE_TIMER_SECS: f32 = 3.0; @@ -82,11 +71,12 @@ const CONTRIBUTORS_LIST: &[&str] = &["Carter Anderson", "And Many More"]; fn setup_contributor_selection(mut commands: Commands, asset_server: Res) { // 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 + // the constant array. Contributors are stored in a HashMap with their + // commit count. let contribs = contributors().unwrap_or_else(|_| { CONTRIBUTORS_LIST .iter() - .map(|name| name.to_string()) + .map(|name| (name.to_string(), 1)) .collect() }); @@ -99,28 +89,31 @@ fn setup_contributor_selection(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2dBundle::default()); + let text_style = TextStyle { + font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 60.0, + ..default() + }; + commands.spawn(( TextBundle::from_sections([ - TextSection::new( - "Contributor showcase", - TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 60.0, - ..default() - }, - ), + TextSection::new("Contributor showcase", text_style.clone()), TextSection::from_style(TextStyle { - font: asset_server.load("fonts/FiraSans-Bold.ttf"), - font_size: 60.0, - ..default() + font_size: 30., + ..text_style }), ]) .with_style(Style { - align_self: AlignSelf::FlexEnd, + position_type: PositionType::Absolute, + top: Val::Px(12.), + left: Val::Px(12.), ..default() }), ContributorDisplay, @@ -167,28 +160,26 @@ fn setup(mut commands: Commands, asset_server: Res) { } /// Finds the next contributor to display and selects the entity -fn select_system( - mut timer: ResMut, +fn selection( + mut timer: ResMut, mut contributor_selection: ResMut, mut text_query: Query<&mut Text, With>, mut query: Query<(&Contributor, &mut Sprite, &mut Transform)>, time: Res