mirror of
https://github.com/bevyengine/bevy
synced 2024-11-22 20:53:53 +00:00
4847f7e3ad
Remove unnecessary calls to `iter()`/`iter_mut()`. Mainly updates the use of queries in our code, docs, and examples. ```rust // From for _ in list.iter() { for _ in list.iter_mut() { // To for _ in &list { for _ in &mut list { ``` We already enable the pedantic lint [clippy::explicit_iter_loop](https://rust-lang.github.io/rust-clippy/stable/) inside of Bevy. However, this only warns for a few known types from the standard library. ## Note for reviewers As you can see the additions and deletions are exactly equal. Maybe give it a quick skim to check I didn't sneak in a crypto miner, but you don't have to torture yourself by reading every line. I already experienced enough pain making this PR :) Co-authored-by: devil-ira <justthecooldude@gmail.com>
52 lines
1.7 KiB
Rust
52 lines
1.7 KiB
Rust
//! Renders an animated sprite by loading all animation frames from a single image (a sprite sheet)
|
|
//! into a texture atlas, and changing the displayed image periodically.
|
|
|
|
use bevy::{prelude::*, render::texture::ImageSettings};
|
|
|
|
fn main() {
|
|
App::new()
|
|
.insert_resource(ImageSettings::default_nearest()) // prevents blurry sprites
|
|
.add_plugins(DefaultPlugins)
|
|
.add_startup_system(setup)
|
|
.add_system(animate_sprite)
|
|
.run();
|
|
}
|
|
|
|
#[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 &mut query {
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
asset_server: Res<AssetServer>,
|
|
mut texture_atlases: ResMut<Assets<TextureAtlas>>,
|
|
) {
|
|
let texture_handle = asset_server.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);
|
|
let texture_atlas_handle = texture_atlases.add(texture_atlas);
|
|
commands.spawn_bundle(Camera2dBundle::default());
|
|
commands
|
|
.spawn_bundle(SpriteSheetBundle {
|
|
texture_atlas: texture_atlas_handle,
|
|
transform: Transform::from_scale(Vec3::splat(6.0)),
|
|
..default()
|
|
})
|
|
.insert(AnimationTimer(Timer::from_seconds(0.1, true)));
|
|
}
|