Improve many sprites example (#2785)
# Objective My attempt at fixing #2075 . This is my very first contribution to this repo. Also, I'm very new to both Rust and bevy, so any feedback is *deeply* appreciated. ## Solution - Changed `move_camera_system` so it only targets the camera entity. My approach here differs from the one used in the [cheatbook](https://bevy-cheatbook.github.io/cookbook/cursor2world.html?highlight=maincamera#2d-games) (in which a marker component is used to track the camera), so please, let me know which of them is more idiomatic. - `move_camera_system` does not require both `Position` and `Transform` anymore (I used `rotate` for rotating the `Transform` in place, but couldn't find an equivalent `translate` method). - Changed `tick_system` so it only targets the timer entity. - Sprites are now spawned via a single `spawn_batch` instead of multiple `spawn`s.
This commit is contained in:
parent
27bfbda7bc
commit
5ff96b8e7e
@ -2,6 +2,7 @@ use bevy::{
|
|||||||
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
||||||
math::Quat,
|
math::Quat,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
render::camera::Camera,
|
||||||
sprite::SpriteSettings,
|
sprite::SpriteSettings,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -9,9 +10,6 @@ use rand::Rng;
|
|||||||
|
|
||||||
const CAMERA_SPEED: f32 = 1000.0;
|
const CAMERA_SPEED: f32 = 1000.0;
|
||||||
|
|
||||||
pub struct PrintTimer(Timer);
|
|
||||||
pub struct Position(Transform);
|
|
||||||
|
|
||||||
/// This example is for performance testing purposes.
|
/// This example is for performance testing purposes.
|
||||||
/// See https://github.com/bevyengine/bevy/pull/1492
|
/// See https://github.com/bevyengine/bevy/pull/1492
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -24,8 +22,8 @@ fn main() {
|
|||||||
})
|
})
|
||||||
.add_plugins(DefaultPlugins)
|
.add_plugins(DefaultPlugins)
|
||||||
.add_startup_system(setup)
|
.add_startup_system(setup)
|
||||||
.add_system(tick.label("Tick"))
|
.add_system(tick_system.label("Tick"))
|
||||||
.add_system(move_camera.after("Tick"))
|
.add_system(move_camera_system.after("Tick"))
|
||||||
.run()
|
.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,14 +42,15 @@ fn setup(
|
|||||||
|
|
||||||
let sprite_handle = materials.add(assets.load("branding/icon.png").into());
|
let sprite_handle = materials.add(assets.load("branding/icon.png").into());
|
||||||
|
|
||||||
|
// Spawns the camera
|
||||||
commands
|
commands
|
||||||
.spawn()
|
.spawn()
|
||||||
.insert_bundle(OrthographicCameraBundle::new_2d())
|
.insert_bundle(OrthographicCameraBundle::new_2d())
|
||||||
.insert(PrintTimer(Timer::from_seconds(1.0, true)))
|
.insert(Timer::from_seconds(1.0, true))
|
||||||
.insert(Position(Transform::from_translation(Vec3::new(
|
.insert(Transform::from_xyz(0.0, 0.0, 1000.0));
|
||||||
0.0, 0.0, 1000.0,
|
|
||||||
))));
|
|
||||||
|
|
||||||
|
// Builds and spawns the sprites
|
||||||
|
let mut sprites = vec![];
|
||||||
for y in -half_y..half_y {
|
for y in -half_y..half_y {
|
||||||
for x in -half_x..half_x {
|
for x in -half_x..half_x {
|
||||||
let position = Vec2::new(x as f32, y as f32);
|
let position = Vec2::new(x as f32, y as f32);
|
||||||
@ -59,7 +58,7 @@ fn setup(
|
|||||||
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
|
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
|
||||||
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);
|
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);
|
||||||
|
|
||||||
commands.spawn().insert_bundle(SpriteBundle {
|
sprites.push(SpriteBundle {
|
||||||
material: sprite_handle.clone(),
|
material: sprite_handle.clone(),
|
||||||
transform: Transform {
|
transform: Transform {
|
||||||
translation,
|
translation,
|
||||||
@ -71,26 +70,23 @@ fn setup(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
commands.spawn_batch(sprites);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_camera(time: Res<Time>, mut query: Query<(&mut Transform, &mut Position)>) {
|
// System for rotating and translating the camera
|
||||||
for (mut transform, mut position) in query.iter_mut() {
|
fn move_camera_system(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
|
||||||
position
|
let mut camera_transform = camera_query.single_mut().unwrap();
|
||||||
.0
|
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
|
||||||
.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
|
*camera_transform = *camera_transform
|
||||||
position.0 =
|
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
|
||||||
position.0 * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
|
}
|
||||||
transform.translation = position.0.translation;
|
|
||||||
transform.rotation *= Quat::from_rotation_z(time.delta_seconds() / 2.0);
|
// 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>) {
|
||||||
}
|
let mut timer = timer_query.single_mut().unwrap();
|
||||||
|
timer.tick(time.delta());
|
||||||
fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTimer>) {
|
|
||||||
for mut timer in query.iter_mut() {
|
if timer.just_finished() {
|
||||||
timer.0.tick(time.delta());
|
info!("Sprites: {}", sprites_query.iter().count(),);
|
||||||
|
|
||||||
if timer.0.just_finished() {
|
|
||||||
info!("Sprites: {}", sprites.iter().count(),);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user