mirror of
https://github.com/bevyengine/bevy.git
synced 2026-06-30 15:55:32 -04:00
8e52194d70
Adopted from #20456 Notes: * The origin of the `dead_code` lints were coming from the `ShaderType ` derive macro. This has been reported as https://github.com/teoxoy/encase/issues/102, and a temporary workspace-wide `allow` added to the top level Cargo.toml. * One of the lints pointed out that `PartialEq` and `Eq` may not work as expected for function pointers, so `CloneBehavior` no longer implements either trait, and pattern matching is used in instead. Original PR Description: ># Objective > >Unbreak CI. > > ## Solution > > Fix the lints. > >I've opted for anonymous lifetimes in every revealed case so far; please let me know if you think I should used named lifetimes in specific cases and why. > >## Testing > >Is CI green? > >## Context > > This lint originally had a much larger splash damage, with fairly negative effects on Bevy. See https://github.com/rust-lang/rust/issues/131725. > > The more restricted former is much more helpful, despite the large diff in this PR. Bevy is a large code base! > >## TODO > >- [x] discuss proposed lifetime lint fixes >,- [x] use cargo clippy --fix to fix newly uncovered docs misformatting >- [x] fix newly revealed dead code issues >- [x] ensure CI is green --------- Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com> Co-authored-by: Mike <mike.hsu@gmail.com>
68 lines
2.0 KiB
Rust
68 lines
2.0 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::*;
|
|
|
|
fn main() {
|
|
App::new()
|
|
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) // prevents blurry sprites
|
|
.add_systems(Startup, setup)
|
|
.add_systems(Update, animate_sprite)
|
|
.run();
|
|
}
|
|
|
|
#[derive(Component)]
|
|
struct AnimationIndices {
|
|
first: usize,
|
|
last: usize,
|
|
}
|
|
|
|
#[derive(Component, Deref, DerefMut)]
|
|
struct AnimationTimer(Timer);
|
|
|
|
fn animate_sprite(
|
|
time: Res<Time>,
|
|
mut query: Query<(&AnimationIndices, &mut AnimationTimer, &mut Sprite)>,
|
|
) {
|
|
for (indices, mut timer, mut sprite) in &mut query {
|
|
timer.tick(time.delta());
|
|
|
|
if timer.just_finished()
|
|
&& let Some(atlas) = &mut sprite.texture_atlas
|
|
{
|
|
atlas.index = if atlas.index == indices.last {
|
|
indices.first
|
|
} else {
|
|
atlas.index + 1
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
fn setup(
|
|
mut commands: Commands,
|
|
asset_server: Res<AssetServer>,
|
|
mut texture_atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
|
|
) {
|
|
let texture = asset_server.load("textures/rpg/chars/gabe/gabe-idle-run.png");
|
|
let layout = TextureAtlasLayout::from_grid(UVec2::splat(24), 7, 1, None, None);
|
|
let texture_atlas_layout = texture_atlas_layouts.add(layout);
|
|
// Use only the subset of sprites in the sheet that make up the run animation
|
|
let animation_indices = AnimationIndices { first: 1, last: 6 };
|
|
|
|
commands.spawn(Camera2d);
|
|
|
|
commands.spawn((
|
|
Sprite::from_atlas_image(
|
|
texture,
|
|
TextureAtlas {
|
|
layout: texture_atlas_layout,
|
|
index: animation_indices.first,
|
|
},
|
|
),
|
|
Transform::from_scale(Vec3::splat(6.0)),
|
|
animation_indices,
|
|
AnimationTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
|
|
));
|
|
}
|