//! This example provides a 2D benchmark. //! //! Usage: spawn more entities by clicking on the screen. use bevy::{ diagnostic::{DiagnosticsStore, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, window::{PresentMode, WindowResolution}, }; use rand::{rngs::StdRng, thread_rng, Rng, SeedableRng}; const BIRDS_PER_SECOND: u32 = 10000; const GRAVITY: f32 = -9.8 * 100.0; const MAX_VELOCITY: f32 = 750.; const BIRD_SCALE: f32 = 0.15; const HALF_BIRD_SIZE: f32 = 256. * BIRD_SCALE * 0.5; #[derive(Resource)] struct BevyCounter { pub count: usize, pub color: Color, } #[derive(Component)] struct Bird { velocity: Vec3, } fn main() { App::new() .add_plugins(( DefaultPlugins.set(WindowPlugin { primary_window: Some(Window { title: "BevyMark".into(), resolution: (800., 600.).into(), present_mode: PresentMode::AutoNoVsync, ..default() }), ..default() }), FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin::default(), )) .insert_resource(BevyCounter { count: 0, color: Color::WHITE, }) .add_systems(Startup, setup) .add_systems(FixedUpdate, scheduled_spawner) .add_systems( Update, ( mouse_handler, movement_system, collision_system, counter_system, ), ) .insert_resource(FixedTime::new_from_secs(0.2)) .run(); } #[derive(Resource)] struct BirdScheduled { wave: usize, per_wave: usize, } fn scheduled_spawner( mut commands: Commands, windows: Query<&Window>, mut scheduled: ResMut, mut counter: ResMut, bird_texture: Res, ) { let window = windows.single(); if scheduled.wave > 0 { spawn_birds( &mut commands, &window.resolution, &mut counter, scheduled.per_wave, bird_texture.clone_weak(), ); let mut rng = thread_rng(); counter.color = Color::rgb_linear(rng.gen(), rng.gen(), rng.gen()); scheduled.wave -= 1; } } #[derive(Resource, Deref)] struct BirdTexture(Handle); #[derive(Component)] struct StatsText; fn setup(mut commands: Commands, asset_server: Res) { warn!(include_str!("warning_string.txt")); let texture = asset_server.load("branding/icon.png"); let text_section = move |color, value: &str| { TextSection::new( value, TextStyle { font_size: 40.0, color, ..default() }, ) }; commands.spawn(Camera2dBundle::default()); commands.spawn(( TextBundle::from_sections([ text_section(Color::GREEN, "Bird Count"), text_section(Color::CYAN, ""), text_section(Color::GREEN, "\nFPS (raw): "), text_section(Color::CYAN, ""), text_section(Color::GREEN, "\nFPS (SMA): "), text_section(Color::CYAN, ""), text_section(Color::GREEN, "\nFPS (EMA): "), text_section(Color::CYAN, ""), ]) .with_style(Style { position_type: PositionType::Absolute, top: Val::Px(5.0), left: Val::Px(5.0), ..default() }), StatsText, )); 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( mut commands: Commands, time: Res