2021-03-25 01:46:22 +00:00
|
|
|
use bevy::{
|
|
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
|
|
math::Quat,
|
|
|
|
prelude::*,
|
2021-09-10 19:13:14 +00:00
|
|
|
render::camera::Camera,
|
2021-04-13 20:18:56 +00:00
|
|
|
sprite::SpriteSettings,
|
2021-03-25 01:46:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
use rand::Rng;
|
|
|
|
|
|
|
|
const CAMERA_SPEED: f32 = 1000.0;
|
|
|
|
|
2021-04-21 23:19:34 +00:00
|
|
|
/// This example is for performance testing purposes.
|
|
|
|
/// See https://github.com/bevyengine/bevy/pull/1492
|
2021-03-25 01:46:22 +00:00
|
|
|
fn main() {
|
2021-07-27 20:21:06 +00:00
|
|
|
App::new()
|
2021-03-25 01:46:22 +00:00
|
|
|
.add_plugin(LogDiagnosticsPlugin::default())
|
|
|
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
2021-04-13 20:18:56 +00:00
|
|
|
.insert_resource(SpriteSettings {
|
|
|
|
// NOTE: this is an experimental feature that doesn't work in all cases
|
|
|
|
frustum_culling_enabled: true,
|
2021-03-25 01:46:22 +00:00
|
|
|
})
|
|
|
|
.add_plugins(DefaultPlugins)
|
2021-06-27 00:40:09 +00:00
|
|
|
.add_startup_system(setup)
|
2021-09-10 19:13:14 +00:00
|
|
|
.add_system(tick_system.label("Tick"))
|
|
|
|
.add_system(move_camera_system.after("Tick"))
|
2021-03-25 01:46:22 +00:00
|
|
|
.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn setup(
|
|
|
|
mut commands: Commands,
|
|
|
|
assets: Res<AssetServer>,
|
|
|
|
mut materials: ResMut<Assets<ColorMaterial>>,
|
|
|
|
) {
|
|
|
|
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());
|
|
|
|
|
2021-09-10 19:13:14 +00:00
|
|
|
// Spawns the camera
|
2021-03-25 01:46:22 +00:00
|
|
|
commands
|
|
|
|
.spawn()
|
|
|
|
.insert_bundle(OrthographicCameraBundle::new_2d())
|
2021-09-10 19:13:14 +00:00
|
|
|
.insert(Timer::from_seconds(1.0, true))
|
|
|
|
.insert(Transform::from_xyz(0.0, 0.0, 1000.0));
|
2021-03-25 01:46:22 +00:00
|
|
|
|
2021-09-10 19:13:14 +00:00
|
|
|
// Builds and spawns the sprites
|
|
|
|
let mut sprites = vec![];
|
2021-03-25 01:46:22 +00:00
|
|
|
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::<f32>());
|
|
|
|
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
|
|
|
|
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);
|
|
|
|
|
2021-09-10 19:13:14 +00:00
|
|
|
sprites.push(SpriteBundle {
|
2021-03-25 01:46:22 +00:00
|
|
|
material: sprite_handle.clone(),
|
|
|
|
transform: Transform {
|
|
|
|
translation,
|
|
|
|
rotation,
|
|
|
|
scale,
|
|
|
|
},
|
|
|
|
sprite: Sprite::new(tile_size),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-09-10 19:13:14 +00:00
|
|
|
commands.spawn_batch(sprites);
|
2021-03-25 01:46:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:13:14 +00:00
|
|
|
// System for rotating and translating the camera
|
|
|
|
fn move_camera_system(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
|
2021-09-10 20:23:50 +00:00
|
|
|
let mut camera_transform = camera_query.single_mut();
|
2021-09-10 19:13:14 +00:00
|
|
|
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
|
|
|
|
*camera_transform = *camera_transform
|
|
|
|
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
|
2021-03-25 01:46:22 +00:00
|
|
|
}
|
|
|
|
|
2021-09-10 19:13:14 +00:00
|
|
|
// System for printing the number of sprites on every tick of the timer
|
|
|
|
fn tick_system(time: Res<Time>, sprites_query: Query<&Sprite>, mut timer_query: Query<&mut Timer>) {
|
2021-09-10 20:23:50 +00:00
|
|
|
let mut timer = timer_query.single_mut();
|
2021-09-10 19:13:14 +00:00
|
|
|
timer.tick(time.delta());
|
2021-03-25 01:46:22 +00:00
|
|
|
|
2021-09-10 19:13:14 +00:00
|
|
|
if timer.just_finished() {
|
|
|
|
info!("Sprites: {}", sprites_query.iter().count(),);
|
2021-03-25 01:46:22 +00:00
|
|
|
}
|
|
|
|
}
|