From 6eb8e15e3d34f1c417103b6869ed47f81c919706 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Sat, 18 Dec 2021 20:13:59 +0000 Subject: [PATCH] Improved bevymark: no bouncing offscreen and spawn waves from CLI (#3364) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - In bevymark, bevies were very bouncy and could go off screen after a while, skewing the FPS https://user-images.githubusercontent.com/8672791/146540848-282fa11b-d058-4da9-8e95-688ae67d9406.mp4 ## Solution - Make bevies bounce also on top of the screen - I also changed how the examples read args from CLI to be able to spawn waves: `cargo run --example bevymark --release -- 5 100` (first is number of bevy per wave, second is number of wave, with one wave every 0.2 seconds). This makes it easier to reproduce some exact situations https://user-images.githubusercontent.com/8672791/146542857-2f953fa0-7b8d-4772-93f8-b8d7a31259dc.mp4 Co-authored-by: François <8672791+mockersf@users.noreply.github.com> --- examples/tools/bevymark.rs | 66 +++++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 25 deletions(-) diff --git a/examples/tools/bevymark.rs b/examples/tools/bevymark.rs index ac3c975438..290fdf4259 100644 --- a/examples/tools/bevymark.rs +++ b/examples/tools/bevymark.rs @@ -1,8 +1,9 @@ use bevy::{ + core::FixedTimestep, diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, }; -use rand::{random, Rng}; +use rand::random; const BIRDS_PER_SECOND: u32 = 10000; const _BASE_COLOR: Color = Color::rgb(5.0, 5.0, 5.0); @@ -43,30 +44,44 @@ fn main() { .add_system(movement_system) .add_system(collision_system) .add_system(counter_system) + .add_system_set( + SystemSet::new() + .with_run_criteria(FixedTimestep::step(0.2)) + .with_system(scheduled_spawner), + ) .run(); } -struct BirdTexture(Handle); +struct BirdScheduled { + wave: u128, + per_wave: u128, +} -fn setup( +fn scheduled_spawner( mut commands: Commands, windows: Res, + mut scheduled: ResMut, mut counter: ResMut, - asset_server: Res, + bird_texture: Res, ) { - let texture = asset_server.load("branding/icon.png"); - if let Some(initial_count) = std::env::args() - .nth(1) - .and_then(|arg| arg.parse::().ok()) - { + if scheduled.wave > 0 { spawn_birds( &mut commands, &windows, &mut counter, - initial_count, - texture.clone_weak(), + scheduled.per_wave, + bird_texture.0.clone_weak(), ); + counter.color = Color::rgb_linear(random(), random(), random()); + scheduled.wave -= 1; } +} + +struct BirdTexture(Handle); + +fn setup(mut commands: Commands, asset_server: Res) { + let texture = asset_server.load("branding/icon.png"); + commands.spawn_bundle(OrthographicCameraBundle::new_2d()); commands.spawn_bundle(UiCameraBundle::default()); commands.spawn_bundle(TextBundle { @@ -120,6 +135,16 @@ fn setup( }); commands.insert_resource(BirdTexture(texture)); + commands.insert_resource(BirdScheduled { + per_wave: std::env::args() + .nth(1) + .and_then(|arg| arg.parse::().ok()) + .unwrap_or_default(), + wave: std::env::args() + .nth(2) + .and_then(|arg| arg.parse::().ok()) + .unwrap_or(1), + }); } fn mouse_handler( @@ -131,7 +156,7 @@ fn mouse_handler( mut counter: ResMut, ) { if mouse_button_input.just_released(MouseButton::Left) { - counter.color = Color::rgb(random(), random(), random()); + counter.color = Color::rgb_linear(random(), random(), random()); } if mouse_button_input.pressed(MouseButton::Left) { @@ -141,7 +166,7 @@ fn mouse_handler( &windows, &mut counter, spawn_count, - bird_texture.0.clone(), + bird_texture.0.clone_weak(), ); } } @@ -210,6 +235,9 @@ fn collision_system(windows: Res, mut bird_query: Query<(&mut Bird, &Tr if y_vel < 0. && y_pos - HALF_BIRD_SIZE < -half_height { bird.velocity.y = -y_vel; } + if y_pos + HALF_BIRD_SIZE > half_height && y_vel > 0.0 { + bird.velocity.y = 0.0; + } } } @@ -227,15 +255,3 @@ fn counter_system( } }; } - -/// Generate a color modulation -/// -/// Because there is no `Mul for Color` instead `[f32; 3]` is -/// used. -fn _gen_color(rng: &mut impl Rng) -> [f32; 3] { - let r = rng.gen_range(0.2..1.0); - let g = rng.gen_range(0.2..1.0); - let b = rng.gen_range(0.2..1.0); - let v = Vec3::new(r, g, b); - v.normalize().into() -}