|
| 1 | +use bevy::{ |
| 2 | + diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, |
| 3 | + math::Quat, |
| 4 | + prelude::*, |
| 5 | +}; |
| 6 | + |
| 7 | +use rand::Rng; |
| 8 | + |
| 9 | +const CAMERA_SPEED: f32 = 1000.0; |
| 10 | + |
| 11 | +pub struct PrintTimer(Timer); |
| 12 | +pub struct Position(Transform); |
| 13 | + |
| 14 | +///This example is for performance testing purposes. |
| 15 | +///See https://github.com/bevyengine/bevy/pull/1492 |
| 16 | +fn main() { |
| 17 | + App::build() |
| 18 | + .add_plugin(LogDiagnosticsPlugin::default()) |
| 19 | + .add_plugin(FrameTimeDiagnosticsPlugin::default()) |
| 20 | + .insert_resource(bevy::log::LogSettings { |
| 21 | + level: bevy::log::Level::DEBUG, |
| 22 | + ..Default::default() |
| 23 | + }) |
| 24 | + .add_plugins(DefaultPlugins) |
| 25 | + .add_startup_system(setup.system()) |
| 26 | + .add_system(tick.system().label("Tick")) |
| 27 | + .add_system(move_camera.system().after("Tick")) |
| 28 | + .run() |
| 29 | +} |
| 30 | + |
| 31 | +fn setup( |
| 32 | + mut commands: Commands, |
| 33 | + assets: Res<AssetServer>, |
| 34 | + mut materials: ResMut<Assets<ColorMaterial>>, |
| 35 | +) { |
| 36 | + let mut rng = rand::thread_rng(); |
| 37 | + |
| 38 | + let tile_size = Vec2::splat(64.0); |
| 39 | + let map_size = Vec2::splat(320.0); |
| 40 | + |
| 41 | + let half_x = (map_size.x / 2.0) as i32; |
| 42 | + let half_y = (map_size.y / 2.0) as i32; |
| 43 | + |
| 44 | + let sprite_handle = materials.add(assets.load("branding/icon.png").into()); |
| 45 | + |
| 46 | + commands |
| 47 | + .spawn() |
| 48 | + .insert_bundle(OrthographicCameraBundle::new_2d()) |
| 49 | + .insert(PrintTimer(Timer::from_seconds(1.0, true))) |
| 50 | + .insert(Position(Transform::from_translation(Vec3::new( |
| 51 | + 0.0, 0.0, 1000.0, |
| 52 | + )))); |
| 53 | + |
| 54 | + for y in -half_y..half_y { |
| 55 | + for x in -half_x..half_x { |
| 56 | + let position = Vec2::new(x as f32, y as f32); |
| 57 | + let translation = (position * tile_size).extend(rng.gen::<f32>()); |
| 58 | + let rotation = Quat::from_rotation_z(rng.gen::<f32>()); |
| 59 | + let scale = Vec3::splat(rng.gen::<f32>() * 2.0); |
| 60 | + |
| 61 | + commands.spawn().insert_bundle(SpriteBundle { |
| 62 | + material: sprite_handle.clone(), |
| 63 | + transform: Transform { |
| 64 | + translation, |
| 65 | + rotation, |
| 66 | + scale, |
| 67 | + }, |
| 68 | + sprite: Sprite::new(tile_size), |
| 69 | + ..Default::default() |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +fn move_camera(time: Res<Time>, mut query: Query<(&mut Transform, &mut Position)>) { |
| 76 | + for (mut transform, mut position) in query.iter_mut() { |
| 77 | + position |
| 78 | + .0 |
| 79 | + .rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5)); |
| 80 | + position.0 = |
| 81 | + position.0 * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds()); |
| 82 | + transform.translation = position.0.translation; |
| 83 | + transform.rotation *= Quat::from_rotation_z(time.delta_seconds() / 2.0); |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTimer>) { |
| 88 | + for mut timer in query.iter_mut() { |
| 89 | + timer.0.tick(time.delta()); |
| 90 | + |
| 91 | + if timer.0.just_finished() { |
| 92 | + println!("Sprites: {}", sprites.iter().count(),); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments