Skip to content

Commit 5ff96b8

Browse files
committed
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.
1 parent 27bfbda commit 5ff96b8

File tree

1 file changed

+22
-26
lines changed

1 file changed

+22
-26
lines changed

examples/2d/many_sprites.rs

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ use bevy::{
22
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
33
math::Quat,
44
prelude::*,
5+
render::camera::Camera,
56
sprite::SpriteSettings,
67
};
78

89
use rand::Rng;
910

1011
const CAMERA_SPEED: f32 = 1000.0;
1112

12-
pub struct PrintTimer(Timer);
13-
pub struct Position(Transform);
14-
1513
/// This example is for performance testing purposes.
1614
/// See https://github.com/bevyengine/bevy/pull/1492
1715
fn main() {
@@ -24,8 +22,8 @@ fn main() {
2422
})
2523
.add_plugins(DefaultPlugins)
2624
.add_startup_system(setup)
27-
.add_system(tick.label("Tick"))
28-
.add_system(move_camera.after("Tick"))
25+
.add_system(tick_system.label("Tick"))
26+
.add_system(move_camera_system.after("Tick"))
2927
.run()
3028
}
3129

@@ -44,22 +42,23 @@ fn setup(
4442

4543
let sprite_handle = materials.add(assets.load("branding/icon.png").into());
4644

45+
// Spawns the camera
4746
commands
4847
.spawn()
4948
.insert_bundle(OrthographicCameraBundle::new_2d())
50-
.insert(PrintTimer(Timer::from_seconds(1.0, true)))
51-
.insert(Position(Transform::from_translation(Vec3::new(
52-
0.0, 0.0, 1000.0,
53-
))));
49+
.insert(Timer::from_seconds(1.0, true))
50+
.insert(Transform::from_xyz(0.0, 0.0, 1000.0));
5451

52+
// Builds and spawns the sprites
53+
let mut sprites = vec![];
5554
for y in -half_y..half_y {
5655
for x in -half_x..half_x {
5756
let position = Vec2::new(x as f32, y as f32);
5857
let translation = (position * tile_size).extend(rng.gen::<f32>());
5958
let rotation = Quat::from_rotation_z(rng.gen::<f32>());
6059
let scale = Vec3::splat(rng.gen::<f32>() * 2.0);
6160

62-
commands.spawn().insert_bundle(SpriteBundle {
61+
sprites.push(SpriteBundle {
6362
material: sprite_handle.clone(),
6463
transform: Transform {
6564
translation,
@@ -71,26 +70,23 @@ fn setup(
7170
});
7271
}
7372
}
73+
commands.spawn_batch(sprites);
7474
}
7575

76-
fn move_camera(time: Res<Time>, mut query: Query<(&mut Transform, &mut Position)>) {
77-
for (mut transform, mut position) in query.iter_mut() {
78-
position
79-
.0
80-
.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
81-
position.0 =
82-
position.0 * Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
83-
transform.translation = position.0.translation;
84-
transform.rotation *= Quat::from_rotation_z(time.delta_seconds() / 2.0);
85-
}
76+
// System for rotating and translating the camera
77+
fn move_camera_system(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
78+
let mut camera_transform = camera_query.single_mut().unwrap();
79+
camera_transform.rotate(Quat::from_rotation_z(time.delta_seconds() * 0.5));
80+
*camera_transform = *camera_transform
81+
* Transform::from_translation(Vec3::X * CAMERA_SPEED * time.delta_seconds());
8682
}
8783

88-
fn tick(time: Res<Time>, sprites: Query<&Sprite>, mut query: Query<&mut PrintTimer>) {
89-
for mut timer in query.iter_mut() {
90-
timer.0.tick(time.delta());
84+
// System for printing the number of sprites on every tick of the timer
85+
fn tick_system(time: Res<Time>, sprites_query: Query<&Sprite>, mut timer_query: Query<&mut Timer>) {
86+
let mut timer = timer_query.single_mut().unwrap();
87+
timer.tick(time.delta());
9188

92-
if timer.0.just_finished() {
93-
info!("Sprites: {}", sprites.iter().count(),);
94-
}
89+
if timer.just_finished() {
90+
info!("Sprites: {}", sprites_query.iter().count(),);
9591
}
9692
}

0 commit comments

Comments
 (0)