|
1 | 1 | // This example shows how to create a simple animated sprite. |
2 | 2 |
|
3 | | -#[path = "./common/mod.rs"] |
4 | | -pub mod common; |
5 | | - |
6 | 3 | use bevy::prelude::*; |
7 | 4 | use bevy_spritesheet_animation::prelude::*; |
8 | 5 |
|
9 | 6 | fn main() { |
10 | 7 | App::new() |
11 | | - .add_plugins(DefaultPlugins) |
12 | | - // Add the plugin to enable animations. |
13 | | - // This makes the AnimationLibrary resource available to your systems. |
| 8 | + .add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest())) |
| 9 | + // Add the plugin to enable animations |
| 10 | + // |
| 11 | + // This configures the app to play animations for entities with a SpritesheetAnimation component. |
| 12 | + // This also makes the Assets<Animation> resource available to your systems. |
14 | 13 | .add_plugins(SpritesheetAnimationPlugin) |
15 | | - .add_systems( |
16 | | - Startup, |
17 | | - (create_animation, spawn_sprite.after(create_animation)), |
18 | | - ) |
| 14 | + .add_systems(Startup, create_animated_sprite) |
19 | 15 | .run(); |
20 | 16 | } |
21 | 17 |
|
22 | | -fn create_animation(mut commands: Commands, mut library: ResMut<AnimationLibrary>) { |
| 18 | +fn create_animated_sprite( |
| 19 | + mut commands: Commands, |
| 20 | + assets: Res<AssetServer>, |
| 21 | + mut animations: ResMut<Assets<Animation>>, |
| 22 | + mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>, |
| 23 | +) { |
23 | 24 | commands.spawn(Camera2d); |
24 | 25 |
|
25 | | - // Create a clip that references some frames from a spritesheet |
26 | | - |
27 | | - let spritesheet = Spritesheet::new(8, 8); |
28 | | - |
29 | | - let clip = Clip::from_frames(spritesheet.row(3)); |
30 | | - |
31 | | - let clip_id = library.register_clip(clip); |
| 26 | + // Create an animation from a row of an 8x8 spritesheet |
| 27 | + // |
| 28 | + // This is a simple animation made of a single clip but we can create more sophisticated animations with multiple clips, each one having different parameters. |
| 29 | + // |
| 30 | + // See the `composition` example for more details. |
32 | 31 |
|
33 | | - // Create an animation that uses the clip |
| 32 | + let image = assets.load("character.png"); |
34 | 33 |
|
35 | | - let animation = Animation::from_clip(clip_id); |
| 34 | + let spritesheet = Spritesheet::new(&image, 8, 8); |
36 | 35 |
|
37 | | - let animation_id = library.register_animation(animation); |
| 36 | + let animation = spritesheet |
| 37 | + .create_animation() |
| 38 | + .add_row(3) |
| 39 | + .set_duration(AnimationDuration::PerFrame(100)) |
| 40 | + .build(); |
38 | 41 |
|
39 | | - // Name the animation to retrieve it from other systems |
| 42 | + // Register the animation as an asset |
40 | 43 |
|
41 | | - library.name_animation(animation_id, "walk").unwrap(); |
| 44 | + let animation_handle = animations.add(animation); |
42 | 45 |
|
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. |
| 46 | + // Create a regular Bevy sprite |
45 | 47 | // |
46 | | - // See the `composition` example for more details. |
47 | | -} |
48 | | - |
49 | | -// We split the setup in two separate systems to show how to retrieve animations from their name |
50 | | - |
51 | | -fn spawn_sprite( |
52 | | - mut commands: Commands, |
53 | | - library: Res<AnimationLibrary>, |
54 | | - mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>, |
55 | | - assets: Res<AssetServer>, |
56 | | -) { |
57 | | - // Retrieve our animation from the library |
58 | | - |
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 |
61 | | - |
62 | | - let image = assets.load("character.png"); |
| 48 | + // Here we use the spritesheet to automatically generate the animation-ready Bevy sprite. |
| 49 | + // This is optional and you may prefer to build the sprite manually. |
63 | 50 |
|
64 | | - let atlas = TextureAtlas { |
65 | | - layout: atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96)), |
66 | | - ..default() |
67 | | - }; |
| 51 | + let sprite = spritesheet |
| 52 | + .with_size_hint(768, 768) |
| 53 | + .sprite(&mut atlas_layouts); |
68 | 54 |
|
69 | | - // Spawn a sprite with a SpritesheetAnimation component that references our animation |
| 55 | + // Spawn the sprite with a SpritesheetAnimation component that references our animation |
70 | 56 |
|
71 | | - commands.spawn(( |
72 | | - Sprite::from_atlas_image(image, atlas), |
73 | | - SpritesheetAnimation::from_id(animation_id), |
74 | | - )); |
75 | | - } |
| 57 | + commands.spawn(( |
| 58 | + // This is a regular Bevy sprite |
| 59 | + sprite, |
| 60 | + // This is the component that animates the sprite |
| 61 | + SpritesheetAnimation::new(animation_handle), |
| 62 | + )); |
76 | 63 | } |
0 commit comments