Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Improved bevymark: no bouncing offscreen and spawn waves from CLI #3364

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 41 additions & 25 deletions examples/tools/bevymark.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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<Image>);
struct BirdScheduled {
wave: u128,
per_wave: u128,
}

fn setup(
fn scheduled_spawner(
mut commands: Commands,
windows: Res<Windows>,
mut scheduled: ResMut<BirdScheduled>,
mut counter: ResMut<BevyCounter>,
asset_server: Res<AssetServer>,
bird_texture: Res<BirdTexture>,
) {
let texture = asset_server.load("branding/icon.png");
if let Some(initial_count) = std::env::args()
.nth(1)
.and_then(|arg| arg.parse::<u128>().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<Image>);

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let texture = asset_server.load("branding/icon.png");

commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands.spawn_bundle(UiCameraBundle::default());
commands.spawn_bundle(TextBundle {
Expand Down Expand Up @@ -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::<u128>().ok())
.unwrap_or_default(),
wave: std::env::args()
.nth(2)
.and_then(|arg| arg.parse::<u128>().ok())
.unwrap_or(1),
});
}

fn mouse_handler(
Expand All @@ -131,7 +156,7 @@ fn mouse_handler(
mut counter: ResMut<BevyCounter>,
) {
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) {
Expand All @@ -141,7 +166,7 @@ fn mouse_handler(
&windows,
&mut counter,
spawn_count,
bird_texture.0.clone(),
bird_texture.0.clone_weak(),
);
}
}
Expand Down Expand Up @@ -210,6 +235,9 @@ fn collision_system(windows: Res<Windows>, 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;
}
}
}

Expand All @@ -227,15 +255,3 @@ fn counter_system(
}
};
}

/// Generate a color modulation
///
/// Because there is no `Mul<Color> 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()
}