@@ -10,7 +10,7 @@ fn main() {
1010 App :: new ( )
1111 . add_plugins ( DefaultPlugins )
1212 // Add the plugin to enable animations.
13- // This makes the AnimationLibrary resource available to your systems.
13+ // This makes the Assets<Animation> resource available to your systems.
1414 . add_plugins ( SpritesheetAnimationPlugin )
1515 . add_systems (
1616 Startup ,
@@ -19,7 +19,12 @@ fn main() {
1919 . run ( ) ;
2020}
2121
22- fn create_animation ( mut commands : Commands , mut library : ResMut < AnimationLibrary > ) {
22+ #[ derive( Resource ) ]
23+ struct MyAnimation {
24+ handle : Handle < Animation > ,
25+ }
26+
27+ fn create_animation ( mut commands : Commands , mut animations : ResMut < Assets < Animation > > ) {
2328 commands. spawn ( Camera2d ) ;
2429
2530 // Create a clip that references some frames from a spritesheet
@@ -28,49 +33,52 @@ fn create_animation(mut commands: Commands, mut library: ResMut<AnimationLibrary
2833
2934 let clip = Clip :: from_frames ( spritesheet. row ( 3 ) ) ;
3035
31- let clip_id = library. register_clip ( clip) ;
32-
3336 // Create an animation that uses the clip
37+ //
38+ // This is a simple animation with a single clip but we can create more sophisticated
39+ // animations with multiple clips, each one having different parameters.
40+ //
41+ // See the `composition` example for more details.
3442
35- let animation = Animation :: from_clip ( clip_id) ;
36-
37- let animation_id = library. register_animation ( animation) ;
43+ let animation = Animation :: from_clip ( clip) ;
3844
3945 // Name the animation to retrieve it from other systems
4046
41- library . name_animation ( animation_id , "walk" ) . unwrap ( ) ;
47+ let animation_handle = animations . add ( animation ) ;
4248
43- // This is a simple animation with a single clip but we can create more sophisticated
44- // animations with multiple clips, each one having different parameters.
45- //
46- // See the `composition` example for more details.
49+ commands. insert_resource ( MyAnimation {
50+ handle : animation_handle,
51+ } ) ;
4752}
4853
4954// We split the setup in two separate systems to show how to retrieve animations from their name
5055
5156fn spawn_sprite (
5257 mut commands : Commands ,
53- library : Res < AnimationLibrary > ,
54- mut atlas_layouts : ResMut < Assets < TextureAtlasLayout > > ,
5558 assets : Res < AssetServer > ,
59+ mut atlas_layouts : ResMut < Assets < TextureAtlasLayout > > ,
60+ my_animation : Res < MyAnimation > ,
5661) {
5762 // Retrieve our animation from the library
5863
59- if let Some ( animation_id) = library. animation_with_name ( "walk" ) {
60- // Create an image and a texture atlas like you would for any Bevy sprite
64+ // Create an image and a texture atlas like you would for any Bevy sprite
65+ //
66+ // However, here we use the Spritesheet helper to easily generate the atlas.
67+ // This is optional and you may prefer to build the atlas manually.
68+
69+ let image = assets. load ( "character.png" ) ;
6170
62- let image = assets . load ( "character.png" ) ;
71+ let spritesheet = Spritesheet :: new ( 8 , 8 ) ;
6372
64- let atlas = TextureAtlas {
65- layout : atlas_layouts. add ( Spritesheet :: new ( 8 , 8 ) . atlas_layout ( 96 , 96 ) ) ,
66- ..default ( )
67- } ;
73+ let atlas = TextureAtlas {
74+ layout : atlas_layouts. add ( spritesheet . atlas_layout ( 96 , 96 ) ) ,
75+ ..default ( )
76+ } ;
6877
69- // Spawn a sprite with a SpritesheetAnimation component that references our animation
78+ // Spawn a sprite with a SpritesheetAnimation component that references our animation
7079
71- commands. spawn ( (
72- Sprite :: from_atlas_image ( image, atlas) ,
73- SpritesheetAnimation :: from_id ( animation_id) ,
74- ) ) ;
75- }
80+ commands. spawn ( (
81+ Sprite :: from_atlas_image ( image, atlas) ,
82+ SpritesheetAnimation :: new ( my_animation. handle . clone ( ) ) ,
83+ ) ) ;
7684}
0 commit comments