Skip to content

Commit 6eb8e15

Browse files
committed
Improved bevymark: no bouncing offscreen and spawn waves from CLI (#3364)
# 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>
1 parent e48f9d8 commit 6eb8e15

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

examples/tools/bevymark.rs

+41-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
use bevy::{
2+
core::FixedTimestep,
23
diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
34
prelude::*,
45
};
5-
use rand::{random, Rng};
6+
use rand::random;
67

78
const BIRDS_PER_SECOND: u32 = 10000;
89
const _BASE_COLOR: Color = Color::rgb(5.0, 5.0, 5.0);
@@ -43,30 +44,44 @@ fn main() {
4344
.add_system(movement_system)
4445
.add_system(collision_system)
4546
.add_system(counter_system)
47+
.add_system_set(
48+
SystemSet::new()
49+
.with_run_criteria(FixedTimestep::step(0.2))
50+
.with_system(scheduled_spawner),
51+
)
4652
.run();
4753
}
4854

49-
struct BirdTexture(Handle<Image>);
55+
struct BirdScheduled {
56+
wave: u128,
57+
per_wave: u128,
58+
}
5059

51-
fn setup(
60+
fn scheduled_spawner(
5261
mut commands: Commands,
5362
windows: Res<Windows>,
63+
mut scheduled: ResMut<BirdScheduled>,
5464
mut counter: ResMut<BevyCounter>,
55-
asset_server: Res<AssetServer>,
65+
bird_texture: Res<BirdTexture>,
5666
) {
57-
let texture = asset_server.load("branding/icon.png");
58-
if let Some(initial_count) = std::env::args()
59-
.nth(1)
60-
.and_then(|arg| arg.parse::<u128>().ok())
61-
{
67+
if scheduled.wave > 0 {
6268
spawn_birds(
6369
&mut commands,
6470
&windows,
6571
&mut counter,
66-
initial_count,
67-
texture.clone_weak(),
72+
scheduled.per_wave,
73+
bird_texture.0.clone_weak(),
6874
);
75+
counter.color = Color::rgb_linear(random(), random(), random());
76+
scheduled.wave -= 1;
6977
}
78+
}
79+
80+
struct BirdTexture(Handle<Image>);
81+
82+
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
83+
let texture = asset_server.load("branding/icon.png");
84+
7085
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
7186
commands.spawn_bundle(UiCameraBundle::default());
7287
commands.spawn_bundle(TextBundle {
@@ -120,6 +135,16 @@ fn setup(
120135
});
121136

122137
commands.insert_resource(BirdTexture(texture));
138+
commands.insert_resource(BirdScheduled {
139+
per_wave: std::env::args()
140+
.nth(1)
141+
.and_then(|arg| arg.parse::<u128>().ok())
142+
.unwrap_or_default(),
143+
wave: std::env::args()
144+
.nth(2)
145+
.and_then(|arg| arg.parse::<u128>().ok())
146+
.unwrap_or(1),
147+
});
123148
}
124149

125150
fn mouse_handler(
@@ -131,7 +156,7 @@ fn mouse_handler(
131156
mut counter: ResMut<BevyCounter>,
132157
) {
133158
if mouse_button_input.just_released(MouseButton::Left) {
134-
counter.color = Color::rgb(random(), random(), random());
159+
counter.color = Color::rgb_linear(random(), random(), random());
135160
}
136161

137162
if mouse_button_input.pressed(MouseButton::Left) {
@@ -141,7 +166,7 @@ fn mouse_handler(
141166
&windows,
142167
&mut counter,
143168
spawn_count,
144-
bird_texture.0.clone(),
169+
bird_texture.0.clone_weak(),
145170
);
146171
}
147172
}
@@ -210,6 +235,9 @@ fn collision_system(windows: Res<Windows>, mut bird_query: Query<(&mut Bird, &Tr
210235
if y_vel < 0. && y_pos - HALF_BIRD_SIZE < -half_height {
211236
bird.velocity.y = -y_vel;
212237
}
238+
if y_pos + HALF_BIRD_SIZE > half_height && y_vel > 0.0 {
239+
bird.velocity.y = 0.0;
240+
}
213241
}
214242
}
215243

@@ -227,15 +255,3 @@ fn counter_system(
227255
}
228256
};
229257
}
230-
231-
/// Generate a color modulation
232-
///
233-
/// Because there is no `Mul<Color> for Color` instead `[f32; 3]` is
234-
/// used.
235-
fn _gen_color(rng: &mut impl Rng) -> [f32; 3] {
236-
let r = rng.gen_range(0.2..1.0);
237-
let g = rng.gen_range(0.2..1.0);
238-
let b = rng.gen_range(0.2..1.0);
239-
let v = Vec3::new(r, g, b);
240-
v.normalize().into()
241-
}

0 commit comments

Comments
 (0)