|
| 1 | +use std::f32::consts::TAU; |
| 2 | + |
| 3 | +use bevy::{ |
| 4 | + diagnostic::{Diagnostics, FrameTimeDiagnosticsPlugin}, |
| 5 | + prelude::*, |
| 6 | +}; |
| 7 | + |
| 8 | +fn main() { |
| 9 | + App::new() |
| 10 | + .add_plugins(DefaultPlugins) |
| 11 | + .add_plugin(FrameTimeDiagnosticsPlugin::default()) |
| 12 | + .insert_resource(Config { |
| 13 | + line_count: 50_000, |
| 14 | + simple: false, |
| 15 | + }) |
| 16 | + .insert_resource(DebugDrawConfig { |
| 17 | + always_on_top: false, |
| 18 | + ..default() |
| 19 | + }) |
| 20 | + .add_startup_system(setup) |
| 21 | + .add_system(system) |
| 22 | + .add_system(ui_system) |
| 23 | + .run(); |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Resource, Debug)] |
| 27 | +struct Config { |
| 28 | + line_count: u32, |
| 29 | + simple: bool, |
| 30 | +} |
| 31 | + |
| 32 | +fn system( |
| 33 | + mut draw: ResMut<DebugDraw>, |
| 34 | + mut config: ResMut<Config>, |
| 35 | + input: Res<Input<KeyCode>>, |
| 36 | + time: Res<Time>, |
| 37 | +) { |
| 38 | + if input.just_pressed(KeyCode::Up) { |
| 39 | + config.line_count += 10_000; |
| 40 | + } |
| 41 | + if input.just_pressed(KeyCode::Down) { |
| 42 | + config.line_count = config.line_count.saturating_sub(10_000); |
| 43 | + } |
| 44 | + if input.just_pressed(KeyCode::Space) { |
| 45 | + config.simple = !config.simple; |
| 46 | + } |
| 47 | + |
| 48 | + if config.simple { |
| 49 | + for _ in 0..config.line_count { |
| 50 | + draw.line(Vec3::NEG_Y, Vec3::Y, Color::BLACK); |
| 51 | + } |
| 52 | + } else { |
| 53 | + for i in 0..config.line_count { |
| 54 | + let angle = i as f32 / config.line_count as f32 * TAU; |
| 55 | + |
| 56 | + let vector = (Vec2::from(angle.sin_cos())).extend(time.elapsed_seconds().sin()); |
| 57 | + let start_color = Color::rgb(vector.x, vector.z, 0.5); |
| 58 | + let end_color = Color::rgb(-vector.z, -vector.y, 0.5); |
| 59 | + |
| 60 | + draw.line_gradient(vector, -vector, start_color, end_color); |
| 61 | + } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +fn setup(mut commands: Commands, asset_server: Res<AssetServer>) { |
| 66 | + commands.spawn(Camera3dBundle { |
| 67 | + transform: Transform::from_xyz(1., 3., 5.).looking_at(Vec3::ZERO, Vec3::Y), |
| 68 | + ..default() |
| 69 | + }); |
| 70 | + |
| 71 | + commands.spawn(TextBundle::from_section( |
| 72 | + "", |
| 73 | + TextStyle { |
| 74 | + font: asset_server.load("fonts/FiraSans-Bold.ttf"), |
| 75 | + font_size: 30., |
| 76 | + ..default() |
| 77 | + }, |
| 78 | + )); |
| 79 | +} |
| 80 | + |
| 81 | +fn ui_system(mut query: Query<&mut Text>, config: Res<Config>, diag: Res<Diagnostics>) { |
| 82 | + let mut text = query.single_mut(); |
| 83 | + |
| 84 | + let Some(fps) = diag.get(FrameTimeDiagnosticsPlugin::FPS).and_then(|fps| fps.smoothed()) else { |
| 85 | + return; |
| 86 | + }; |
| 87 | + |
| 88 | + text.sections[0].value = format!( |
| 89 | + "Line count: {}\n\ |
| 90 | + FPS: {:.0}\n\n\ |
| 91 | + Controls:\n\ |
| 92 | + Up/Down: Raise or lower the line count.\n\ |
| 93 | + Spacebar: Toggle simple mode.", |
| 94 | + config.line_count, fps, |
| 95 | + ); |
| 96 | +} |
0 commit comments