1- //! Shows how to render simple primitive shapes with a single color.
1+ //! Renders an animated sprite by loading all animation frames from a single image (a sprite sheet)
2+ //! into a texture atlas, and changing the displayed image periodically.
23
3- use bevy:: { prelude:: * , sprite :: MaterialMesh2dBundle } ;
4+ use bevy:: prelude:: * ;
45
56fn main ( ) {
67 App :: new ( )
7- . add_plugins ( DefaultPlugins )
8+ . add_plugins ( DefaultPlugins . set ( ImagePlugin :: default_nearest ( ) ) ) // prevents blurry sprites
89 . add_startup_system ( setup)
10+ . add_system ( animate_sprite)
911 . run ( ) ;
1012}
1113
14+ #[ derive( Component ) ]
15+ struct AnimationIndices {
16+ first : usize ,
17+ last : usize ,
18+ }
19+
20+ #[ derive( Component , Deref , DerefMut ) ]
21+ struct AnimationTimer ( Timer ) ;
22+
23+ fn animate_sprite (
24+ time : Res < Time > ,
25+ mut query : Query < (
26+ & AnimationIndices ,
27+ & mut AnimationTimer ,
28+ & mut TextureAtlasSprite ,
29+ ) > ,
30+ ) {
31+ for ( indices, mut timer, mut sprite) in & mut query {
32+ timer. tick ( time. delta ( ) ) ;
33+ if timer. just_finished ( ) {
34+ sprite. index = if sprite. index == indices. last {
35+ indices. first
36+ } else {
37+ sprite. index + 1
38+ } ;
39+ }
40+ }
41+ }
42+
1243fn setup (
1344 mut commands : Commands ,
14- mut meshes : ResMut < Assets < Mesh > > ,
15- mut materials : ResMut < Assets < ColorMaterial > > ,
45+ asset_server : Res < AssetServer > ,
46+ mut texture_atlases : ResMut < Assets < TextureAtlas > > ,
1647) {
48+ let texture_handle = asset_server. load ( "RPGMCharacter_v1.0/pngs/down_idle.png" ) ;
49+ let texture_atlas =
50+ TextureAtlas :: from_grid ( texture_handle, Vec2 :: new ( 64.0 , 64.0 ) , 2 , 2 , None , None ) ;
51+ let texture_atlas_handle = texture_atlases. add ( texture_atlas) ;
52+ // Use only the subset of sprites in the sheet that make up the run animation
53+ let animation_indices = AnimationIndices { first : 1 , last : 6 } ;
1754 commands. spawn ( Camera2dBundle :: default ( ) ) ;
18-
19- // Circle
20- commands. spawn ( MaterialMesh2dBundle {
21- mesh : meshes. add ( shape:: Circle :: new ( 50. ) . into ( ) ) . into ( ) ,
22- material : materials. add ( ColorMaterial :: from ( Color :: PURPLE ) ) ,
23- transform : Transform :: from_translation ( Vec3 :: new ( -150. , 0. , 0. ) ) ,
24- ..default ( )
25- } ) ;
26-
27- // Rectangle
28- commands. spawn ( SpriteBundle {
29- sprite : Sprite {
30- color : Color :: rgb ( 0.25 , 0.25 , 0.75 ) ,
31- custom_size : Some ( Vec2 :: new ( 50.0 , 100.0 ) ) ,
55+ commands. spawn ( (
56+ SpriteSheetBundle {
57+ texture_atlas : texture_atlas_handle,
58+ sprite : TextureAtlasSprite :: new ( animation_indices. first ) ,
59+ transform : Transform :: from_scale ( Vec3 :: splat ( 0.0 ) ) ,
3260 ..default ( )
3361 } ,
34- transform : Transform :: from_translation ( Vec3 :: new ( -50. , 0. , 0. ) ) ,
35- ..default ( )
36- } ) ;
37-
38- // Quad
39- commands. spawn ( MaterialMesh2dBundle {
40- mesh : meshes
41- . add ( shape:: Quad :: new ( Vec2 :: new ( 50. , 100. ) ) . into ( ) )
42- . into ( ) ,
43- material : materials. add ( ColorMaterial :: from ( Color :: LIME_GREEN ) ) ,
44- transform : Transform :: from_translation ( Vec3 :: new ( 50. , 0. , 0. ) ) ,
45- ..default ( )
46- } ) ;
47-
48- // Hexagon
49- commands. spawn ( MaterialMesh2dBundle {
50- mesh : meshes. add ( shape:: RegularPolygon :: new ( 50. , 6 ) . into ( ) ) . into ( ) ,
51- material : materials. add ( ColorMaterial :: from ( Color :: TURQUOISE ) ) ,
52- transform : Transform :: from_translation ( Vec3 :: new ( 150. , 0. , 0. ) ) ,
53- ..default ( )
54- } ) ;
62+ animation_indices,
63+ AnimationTimer ( Timer :: from_seconds ( 0.1 , TimerMode :: Repeating ) ) ,
64+ ) ) ;
5565}
0 commit comments