Skip to content

Commit cbc1e14

Browse files
committed
WIP
1 parent 2d05df0 commit cbc1e14

13 files changed

Lines changed: 115 additions & 80 deletions

File tree

examples/3d.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn spawn_sprites(
4141

4242
let animation_handle = animations.add(animation);
4343

44-
// Create an image and a texture atlas like you would for any Bevy sprite
44+
// Create an image and a texture atlas like you would for 2D sprites
4545

4646
let image = assets.load("character.png");
4747

@@ -164,9 +164,11 @@ struct Orbit {
164164
}
165165

166166
fn orbit(time: Res<Time>, mut query: Query<(&Orbit, &mut Transform)>) {
167+
let secs_elapsed = time.elapsed_secs();
168+
167169
for (orbit, mut transform) in &mut query {
168-
transform.translation.x = (orbit.start_angle + time.elapsed_secs()).cos() * 1500.0;
169-
transform.translation.z = (orbit.start_angle + time.elapsed_secs()).sin() * 1500.0;
170+
transform.translation.x = (orbit.start_angle + secs_elapsed).cos() * 1500.0;
171+
transform.translation.z = (orbit.start_angle + secs_elapsed).sin() * 1500.0;
170172
}
171173
}
172174

examples/basic.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ fn main() {
1212
// Add the plugin to enable animations.
1313
// This makes the Assets<Animation> resource available to your systems.
1414
.add_plugins(SpritesheetAnimationPlugin)
15+
// For that example's sake, we split the setup in two separate systems to show how to retrieve animations with animation sets
1516
.add_systems(
1617
Startup,
1718
(create_animation, spawn_sprite.after(create_animation)),
1819
)
1920
.run();
2021
}
2122

22-
#[derive(Resource)]
23-
struct MyAnimation {
24-
handle: Handle<Animation>,
25-
}
23+
// Define an animation set to refer to our animation across systems.
24+
//
25+
// This is not mandatory at all.
26+
// However it is convenient for accessing
27+
animation_set!(MyAnimation [
28+
anim run
29+
]);
2630

2731
fn create_animation(mut commands: Commands, mut animations: ResMut<Assets<Animation>>) {
2832
commands.spawn(Camera2d);
@@ -42,33 +46,29 @@ fn create_animation(mut commands: Commands, mut animations: ResMut<Assets<Animat
4246

4347
let animation = Animation::from_clip(clip);
4448

45-
// Name the animation to retrieve it from other systems
46-
4749
let animation_handle = animations.add(animation);
4850

51+
// Store the animation to retrieve it from other systems
52+
4953
commands.insert_resource(MyAnimation {
50-
handle: animation_handle,
54+
run: animation_handle,
5155
});
5256
}
5357

54-
// We split the setup in two separate systems to show how to retrieve animations from their name
55-
5658
fn spawn_sprite(
5759
mut commands: Commands,
5860
assets: Res<AssetServer>,
5961
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
6062
my_animation: Res<MyAnimation>,
6163
) {
62-
// Retrieve our animation from the library
63-
6464
// Create an image and a texture atlas like you would for any Bevy sprite
6565
//
66-
// However, here we use the Spritesheet helper to easily generate the atlas.
66+
// Here we use the Spritesheet helper to easily generate the atlas.
6767
// This is optional and you may prefer to build the atlas manually.
6868

6969
let image = assets.load("character.png");
7070

71-
let spritesheet = Spritesheet::new(8, 8);
71+
let spritesheet = Spritesheet::new(8, 8); // TODO weird to have to recreate it
7272

7373
let atlas = TextureAtlas {
7474
layout: atlas_layouts.add(spritesheet.atlas_layout(96, 96)),
@@ -79,6 +79,6 @@ fn spawn_sprite(
7979

8080
commands.spawn((
8181
Sprite::from_atlas_image(image, atlas),
82-
SpritesheetAnimation::new(my_animation.handle.clone()),
82+
SpritesheetAnimation::new(my_animation.run.clone()),
8383
));
8484
}

examples/character.rs

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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"]
77
pub 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

5229
fn 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());

examples/common/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rand::Rng;
55
const DEFAULT_WINDOW_WIDTH: f32 = 1280.0;
66
const DEFAULT_WINDOW_HEIGHT: f32 = 720.0;
77

8+
// TODO move to single example using it
89
/// Returns the screen-space position of the nth item in a grid
910
pub fn grid_position(columns: u32, rows: u32, n: usize) -> Vec3 {
1011
const MARGIN: f32 = 100.0;
@@ -25,6 +26,7 @@ pub fn grid_position(columns: u32, rows: u32, n: usize) -> Vec3 {
2526
)
2627
}
2728

29+
// TODO move to single example using it
2830
/// Returns a random screen-space position
2931
pub fn random_position() -> Vec3 {
3032
let mut rng = rand::rng();

examples/composition.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This example shows how to create more sophisticated animations made of multiple clips.
1+
// This example shows how to create composite animations made of multiple clips.
22

33
#[path = "./common/mod.rs"]
44
pub mod common;
@@ -30,7 +30,7 @@ fn spawn_character(
3030
// - run 5 times
3131
// - shoot once
3232
//
33-
// The whole animation will repeat 5 times
33+
// The whole animation will repeat 2 times
3434

3535
let spritesheet = Spritesheet::new(8, 8);
3636

@@ -47,8 +47,7 @@ fn spawn_character(
4747
.with_repetitions(1);
4848

4949
let animation = Animation::from_clips([idle_clip, run_clip, shoot_clip])
50-
// Let's repeat it a few times and then stop
51-
.with_repetitions(AnimationRepeat::Times(5));
50+
.with_repetitions(AnimationRepeat::Times(2));
5251

5352
let animation_handle = animations.add(animation);
5453

@@ -57,7 +56,7 @@ fn spawn_character(
5756
let image = assets.load("character.png");
5857

5958
let atlas = TextureAtlas {
60-
layout: atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96)),
59+
layout: atlas_layouts.add(spritesheet.atlas_layout(96, 96)),
6160
..default()
6261
};
6362

examples/cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This example shows how to create a animated cursor.
1+
// This example shows how to create an animated cursor.
22
//
33
// The 'custom_cursor' feature must be enabled for custom cursors to be animated (enabled by default).
44

examples/events.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,11 @@ fn main() {
4444
.run();
4545
}
4646

47-
#[derive(Resource)]
48-
struct MyAnimations {
49-
left_foot_touches_ground_marker: MarkerId,
50-
right_foot_touches_ground_marker: MarkerId,
51-
bullet_out_marker: MarkerId,
52-
}
47+
animation_set!(MyMarkers [
48+
marker left_foot_touches_ground_marker,
49+
marker right_foot_touches_ground_marker,
50+
marker bullet_out_marker
51+
]);
5352

5453
fn spawn_character(
5554
mut commands: Commands,
@@ -89,7 +88,7 @@ fn spawn_character(
8988

9089
// TODO doc
9190

92-
commands.insert_resource(MyAnimations {
91+
commands.insert_resource(MyMarkers {
9392
left_foot_touches_ground_marker,
9493
right_foot_touches_ground_marker,
9594
bullet_out_marker,
@@ -100,7 +99,7 @@ fn spawn_character(
10099
let image = assets.load("character.png");
101100

102101
let atlas = TextureAtlas {
103-
layout: atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96)),
102+
layout: atlas_layouts.add(spritesheet.atlas_layout(96, 96)),
104103
..default()
105104
};
106105

@@ -221,7 +220,7 @@ fn spawn_visual_effects(
221220
mut meshes: ResMut<Assets<Mesh>>,
222221
mut materials: ResMut<Assets<ColorMaterial>>,
223222
mut messages: MessageReader<AnimationEvent>,
224-
my_animations: Res<MyAnimations>,
223+
my_animations: Res<MyMarkers>,
225224
) {
226225
for event in messages.read() {
227226
if let AnimationEvent::MarkerHit { marker_id, .. } = event {

examples/headless.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn spawn_animation(mut commands: Commands, mut animations: ResMut<Assets<Animati
3131

3232
// Spawn an entity with a SpritesheetAnimation component that references our animation
3333
//
34-
// We dont even need a Sprite since we aren't rendering anything
34+
// We don't even need a Sprite since we aren't rendering anything
3535

3636
commands.spawn(SpritesheetAnimation::new(animation_handle));
3737
}

examples/parameters.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This example shows the effect of each animation parameter.
1+
// This example shows the effect of different animation parameters.
22

33
#[path = "./common/mod.rs"]
44
pub mod common;
@@ -28,7 +28,7 @@ fn spawn_animations(
2828
) {
2929
commands.spawn(Camera2d);
3030

31-
// Create a clip
31+
// Create a clip that we'll reuse for each animation
3232

3333
let spritesheet = Spritesheet::new(1, 30);
3434

@@ -73,6 +73,7 @@ fn spawn_animations(
7373
];
7474

7575
// Easing
76+
7677
for variety in [
7778
EasingVariety::Quadratic,
7879
EasingVariety::Cubic,

examples/progress.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This example shows how to control the progress of an animation.
1+
// This example shows how to query and control the progress of an animation.
22

33
#[path = "./common/mod.rs"]
44
pub mod common;
@@ -41,7 +41,7 @@ fn spawn_character(
4141
let image = assets.load("character.png");
4242

4343
let atlas = TextureAtlas {
44-
layout: atlas_layouts.add(Spritesheet::new(8, 8).atlas_layout(96, 96)),
44+
layout: atlas_layouts.add(spritesheet.atlas_layout(96, 96)),
4545
..default()
4646
};
4747

@@ -63,19 +63,19 @@ fn control_animation(
6363
mut sprites: Query<&mut SpritesheetAnimation>,
6464
) {
6565
for mut sprite in &mut sprites {
66-
// Pause the current animation
66+
// Play/Pause the animation
6767

6868
if keyboard.just_pressed(KeyCode::KeyP) {
6969
sprite.playing = !sprite.playing;
7070
}
7171

72-
// Reset the current animation
72+
// Reset the animation
7373

7474
if keyboard.just_pressed(KeyCode::KeyR) {
7575
sprite.reset();
7676
}
7777

78-
// Go to a specific frame of the current animation
78+
// Go to a specific frame of the animation
7979

8080
let keys = [
8181
KeyCode::Numpad0,
@@ -93,3 +93,5 @@ fn control_animation(
9393
}
9494
}
9595
}
96+
97+
// TODO print frame index

0 commit comments

Comments
 (0)