use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, math::Quat, prelude::*, sprite::SpriteSettings, }; use rand::Rng; const CAMERA_SPEED: f32 = 1000.0; pub struct PrintTimer(Timer); pub struct Position(Transform); /// This example is for performance testing purposes. /// See https://github.com/bevyengine/bevy/pull/1492 fn main() { App::build() .add_plugin(LogDiagnosticsPlugin::default()) .add_plugin(FrameTimeDiagnosticsPlugin::default()) .insert_resource(SpriteSettings { // NOTE: this is an experimental feature that doesn't work in all cases frustum_culling_enabled: true, }) .add_plugins(DefaultPlugins) .add_startup_system(setup.system()) .add_system(tick.system().label("Tick")) .add_system(move_camera.system().after("Tick")) .run() } fn setup( mut commands: Commands, assets: Res, mut materials: ResMut>, ) { let mut rng = rand::thread_rng(); let tile_size = Vec2::splat(64.0); let map_size = Vec2::splat(320.0); let half_x = (map_size.x / 2.0) as i32; let half_y = (map_size.y / 2.0) as i32; let sprite_handle = materials.add(assets.load("branding/icon.png").into()); commands .spawn() .insert_bundle(OrthographicCameraBundle::new_2d()) .insert(PrintTimer(Timer::from_seconds(1.0, true))) .insert(Position(Transform::from_translation(Vec3::new( 0.0, 0.0, 1000.0, )))); for y in -half_y..half_y { for x in -half_x..half_x { let position = Vec2::new(x as f32, y as f32); let translation = (position * tile_size).extend(rng.gen::()); let rotation = Quat::from_rotation_z(rng.gen::()); let scale = Vec3::splat(rng.gen::() * 2.0); commands.spawn().insert_bundle(SpriteBundle { material: sprite_handle.clone(), transform: Transform { translation, rotation, scale, }, sprite: Sprite::new(tile_size), ..Default::default() }); } } } fn move_camera(time: Res