mirror of
https://github.com/bevyengine/bevy
synced 2024-11-26 14:40:19 +00:00
73605f43b6
As mentioned in #2926, it's better to have an explicit type that clearly communicates the intent of the timer mode rather than an opaque boolean, which can be only understood when knowing the signature or having to look up the documentation. This also opens up a way to merge different timers, such as `Stopwatch`, and possibly future ones, such as `DiscreteStopwatch` and `DiscreteTimer` from #2683, into one struct. Signed-off-by: Lena Milizé <me@lvmn.org> # Objective Fixes #2926. ## Solution Introduce `TimerMode` which replaces the `bool` argument of `Timer` constructors. A `Default` value for `TimerMode` is `Once`. --- ## Changelog ### Added - `TimerMode` enum, along with variants `TimerMode::Once` and `TimerMode::Repeating` ### Changed - Replace `bool` argument of `Timer::new` and `Timer::from_seconds` with `TimerMode` - Change `repeating: bool` field of `Timer` with `mode: TimerMode` ## Migration Guide - Replace `Timer::new(duration, false)` with `Timer::new(duration, TimerMode::Once)`. - Replace `Timer::new(duration, true)` with `Timer::new(duration, TimerMode::Repeating)`. - Replace `Timer::from_seconds(seconds, false)` with `Timer::from_seconds(seconds, TimerMode::Once)`. - Replace `Timer::from_seconds(seconds, true)` with `Timer::from_seconds(seconds, TimerMode::Repeating)`. - Change `timer.repeating()` to `timer.mode() == TimerMode::Repeating`.
136 lines
4.3 KiB
Rust
136 lines
4.3 KiB
Rust
//! Renders a lot of animated sprites to allow performance testing.
|
|
//!
|
|
//! It sets up many animated sprites in different sizes and rotations, and at different scales in the world,
|
|
//! and moves the camera over them to see how well frustum culling works.
|
|
//!
|
|
//! To measure performance realistically, be sure to run this in release mode.
|
|
//! `cargo run --example many_animated_sprites --release`
|
|
|
|
use std::time::Duration;
|
|
|
|
use bevy::{
|
|
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
|
|
math::Quat,
|
|
prelude::*,
|
|
render::camera::Camera,
|
|
};
|
|
|
|
use rand::Rng;
|
|
|
|
const CAMERA_SPEED: f32 = 1000.0;
|
|
|
|
fn main() {
|
|
App::new()
|
|
// Since this is also used as a benchmark, we want it to display performance data.
|
|
.add_plugin(LogDiagnosticsPlugin::default())
|
|
.add_plugin(FrameTimeDiagnosticsPlugin::default())
|
|
.add_plugins(DefaultPlugins)
|
|
.add_startup_system(setup)
|
|
.add_system(animate_sprite)
|
|
.add_system(print_sprite_count)
|
|
.add_system(move_camera.after(print_sprite_count))
|
|
.run();
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
assets: Res<AssetServer>,
|
|
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
|
) {
|
|
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 texture_handle = assets.load("textures/rpg/chars/gabe/gabe-idle-run.png");
|
|
let texture_atlas =
|
|
TextureAtlas::from_grid(texture_handle, Vec2::new(24.0, 24.0), 7, 1, None, None);
|
|
let texture_atlas_handle = texture_atlases.add(texture_atlas);
|
|
|
|
// Spawns the camera
|
|
|
|
commands.spawn(Camera2dBundle::default());
|
|
|
|
// Builds and spawns the sprites
|
|
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);
|
|
let mut timer = Timer::from_seconds(0.1, TimerMode::Repeating);
|
|
timer.set_elapsed(Duration::from_secs_f32(rng.gen::<f32>()));
|
|
|
|
commands.spawn((
|
|
SpriteSheetBundle {
|
|
texture_atlas: texture_atlas_handle.clone(),
|
|
transform: Transform {
|
|
translation,
|
|
rotation,
|
|
scale,
|
|
},
|
|
sprite: TextureAtlasSprite {
|
|
custom_size: Some(tile_size),
|
|
..default()
|
|
},
|
|
..default()
|
|
},
|
|
AnimationTimer(timer),
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
// System for rotating and translating the camera
|
|
fn move_camera(time: Res<Time>, mut camera_query: Query<&mut Transform, With<Camera>>) {
|
|
let mut camera_transform = camera_query.single_mut();
|
|
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());
|
|
}
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
struct AnimationTimer(Timer);
|
|
|
|
fn animate_sprite(
|
|
time: Res<Time>,
|
|
texture_atlases: Res<Assets<TextureAtlas>>,
|
|
mut query: Query<(
|
|
&mut AnimationTimer,
|
|
&mut TextureAtlasSprite,
|
|
&Handle<TextureAtlas>,
|
|
)>,
|
|
) {
|
|
for (mut timer, mut sprite, texture_atlas_handle) in query.iter_mut() {
|
|
timer.tick(time.delta());
|
|
if timer.just_finished() {
|
|
let texture_atlas = texture_atlases.get(texture_atlas_handle).unwrap();
|
|
sprite.index = (sprite.index + 1) % texture_atlas.textures.len();
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Deref, DerefMut)]
|
|
struct PrintingTimer(Timer);
|
|
|
|
impl Default for PrintingTimer {
|
|
fn default() -> Self {
|
|
Self(Timer::from_seconds(1.0, TimerMode::Repeating))
|
|
}
|
|
}
|
|
|
|
// System for printing the number of sprites on every tick of the timer
|
|
fn print_sprite_count(
|
|
time: Res<Time>,
|
|
mut timer: Local<PrintingTimer>,
|
|
sprites: Query<&TextureAtlasSprite>,
|
|
) {
|
|
timer.tick(time.delta());
|
|
|
|
if timer.just_finished() {
|
|
info!("Sprites: {}", sprites.iter().count(),);
|
|
}
|
|
}
|