use bevy::{ core::FixedTimestep, diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, prelude::*, window::PresentMode, }; use rand::{thread_rng, Rng}; 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; struct BevyCounter { pub count: usize, pub color: Color, } #[derive(Component)] struct Bird { velocity: Vec3, } /// This example provides a 2D benchmark. /// /// Usage: spawn more entities by clicking on the screen. fn main() { App::new() .insert_resource(WindowDescriptor { title: "BevyMark".to_string(), width: 800., height: 600., present_mode: PresentMode::Immediate, resizable: true, ..default() }) .add_plugins(DefaultPlugins) .add_plugin(FrameTimeDiagnosticsPlugin::default()) .add_plugin(LogDiagnosticsPlugin::default()) .insert_resource(BevyCounter { count: 0, color: Color::WHITE, }) .add_startup_system(setup) .add_system(mouse_handler) .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 BirdScheduled { wave: usize, per_wave: usize, } fn scheduled_spawner( mut commands: Commands, windows: Res, mut scheduled: ResMut, mut counter: ResMut, bird_texture: Res, ) { if scheduled.wave > 0 { spawn_birds( &mut commands, &windows, &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(Deref)] struct BirdTexture(Handle); #[derive(Component)] struct StatsText; 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 { text: Text { sections: vec![ TextSection { value: "Bird Count: ".to_string(), style: TextStyle { font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 0.0), }, }, TextSection { value: "".to_string(), style: TextStyle { font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 1.0), }, }, TextSection { value: "\nAverage FPS: ".to_string(), style: TextStyle { font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 0.0), }, }, TextSection { value: "".to_string(), style: TextStyle { font: asset_server.load("fonts/FiraSans-Bold.ttf"), font_size: 40.0, color: Color::rgb(0.0, 1.0, 1.0), }, }, ], ..default() }, style: Style { position_type: PositionType::Absolute, position: UiRect { top: Val::Px(5.0), left: Val::Px(5.0), ..default() }, ..default() }, ..default() }) .insert(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