1- // This example shows how to create controllable character with multiple animations.
1+ // This example shows how to create a controllable character with multiple animations.
22//
3- // - We'll create a few animations for our character (idle, run, shoot) in a setup system
4- // - We'll move the character with the keyboard and switch between animations in another system
3+ // - We create a few animations for our character (idle, run, shoot) in a Startup system
4+ // - We move the character with the keyboard and switch between animations in an Update system
55
66#[ path = "./common/mod.rs" ]
77pub mod common;
@@ -20,34 +20,11 @@ fn main() {
2020 . run ( ) ;
2121}
2222
23- #[ derive( Resource ) ]
24- struct MyAnimations {
25- idle : Handle < Animation > ,
26- run : Handle < Animation > ,
27- shoot : Handle < Animation > ,
28- }
29-
30- // macro_rules! declare_animation_set_field_type {
31- // ($x:ident) => {
32- // Handle<Animation>
33- // };
34-
35- // ($x:ident ?) => {
36- // Option<Handle<u8>>
37- // };
38- // }
39-
40- // macro_rules! declare_animation_set {
41- // ($name:ident [ $($field:ident $(?)?),* ]) => {
42- // #[derive(Resource, Default)]
43- // pub struct $name {
44- // $(
45- // pub $field: declare_animation_set_field_type!($field),
46- // )*
47- // }
48- // };
49- // }
50- // declare_animation_set!(MyAnimations [ idle, run, shoot? ]);
23+ animation_set ! ( MyAnimations [
24+ anim idle,
25+ anim run,
26+ anim shoot
27+ ] ) ;
5128
5229fn spawn_character (
5330 mut commands : Commands ,
@@ -85,6 +62,8 @@ fn spawn_character(
8562
8663 let shoot_animation_handle = animations. add ( shoot_animation) ;
8764
65+ // Store the animation set as a resource
66+
8867 commands. insert_resource ( MyAnimations {
8968 idle : idle_animation_handle. clone ( ) ,
9069 run : run_animation_handle. clone ( ) ,
@@ -96,7 +75,7 @@ fn spawn_character(
9675 let image = assets. load ( "character.png" ) ;
9776
9877 let atlas = TextureAtlas {
99- layout : atlas_layouts. add ( Spritesheet :: new ( 8 , 8 ) . atlas_layout ( 96 , 96 ) ) ,
78+ layout : atlas_layouts. add ( spritesheet . atlas_layout ( 96 , 96 ) ) ,
10079 ..default ( )
10180 } ;
10281
@@ -129,13 +108,13 @@ fn control_character(
129108 const CHARACTER_SPEED : f32 = 150.0 ;
130109
131110 for ( entity, mut sprite, mut animation, mut transform, shooting) in & mut characters {
132- // Except if they're shooting, in which case we wait for the animation to end
111+ // If they're shooting, do nothing and wait for the animation to end
133112
134113 if shooting. is_some ( ) {
135114 continue ;
136115 }
137116
138- // Shoot
117+ // Shoot with the spacebar
139118 if keyboard. pressed ( KeyCode :: Space ) {
140119 // Set the animation
141120
@@ -145,15 +124,17 @@ fn control_character(
145124
146125 commands. entity ( entity) . insert ( Shooting ) ;
147126 }
148- // Move left or right
127+ // Run with the arrows
149128 else if keyboard. pressed ( KeyCode :: ArrowLeft ) || keyboard. pressed ( KeyCode :: ArrowRight ) {
150129 // Set the animation
130+ //
131+ // Only if not already running as we don't want to reset the animation in that case
151132
152133 if animation. animation != my_animations. run {
153134 animation. switch ( my_animations. run . clone ( ) ) ;
154135 }
155136
156- // Move
137+ // Move the entity and flip it horizontally depending on the direction
157138
158139 let translation = Vec3 :: X * time. delta_secs ( ) * CHARACTER_SPEED ;
159140
@@ -168,6 +149,8 @@ fn control_character(
168149 // Idle
169150 else {
170151 // Set the animation
152+ //
153+ // Only if not already idle as we don't want to reset the animation in that case
171154
172155 if animation. animation != my_animations. idle {
173156 animation. switch ( my_animations. idle . clone ( ) ) ;
0 commit comments