/
parameters.rs
164 lines (143 loc) · 5.25 KB
/
parameters.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// This example illustrates the effect of each clip/stage/animation parameter.
#[path = "./common/mod.rs"]
pub mod common;
use bevy::prelude::*;
use bevy_spritesheet_animation::{
easing::{Easing, EasingVariety},
prelude::*,
};
use common::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(SpritesheetAnimationPlugin)
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut atlas_layouts: ResMut<Assets<TextureAtlasLayout>>,
mut library: ResMut<SpritesheetLibrary>,
assets: Res<AssetServer>,
) {
commands.spawn(Camera2dBundle::default());
// Load assets for the sprite
let texture = assets.load("ball.png");
let layout = atlas_layouts.add(TextureAtlasLayout::from_grid(
Vec2::new(100.0, 20.0),
1,
30,
None,
None,
));
// Create an animation clip
let clip_id = library.new_clip(|clip| {
clip.push_frame_indices(Spritesheet::new(1, 30).column(0))
.set_default_duration(AnimationDuration::PerCycle(1000));
});
// Create an animated sprite for each parameter set
let mut parameters = vec![
// Duration
(Some(AnimationDuration::PerFrame(50)), None, None, None),
(Some(AnimationDuration::PerFrame(500)), None, None, None),
(Some(AnimationDuration::PerFrame(2000)), None, None, None),
(Some(AnimationDuration::PerCycle(2000)), None, None, None),
(Some(AnimationDuration::PerCycle(100)), None, None, None),
// Repeat
(None, Some(AnimationRepeat::Cycles(10)), None, None),
(None, Some(AnimationRepeat::Cycles(100)), None, None),
(None, Some(AnimationRepeat::Loop), None, None),
// Direction
(None, None, Some(AnimationDirection::Forwards), None),
(None, None, Some(AnimationDirection::Backwards), None),
(None, None, Some(AnimationDirection::PingPong), None),
];
// Easing
for variety in [
EasingVariety::Quadratic,
EasingVariety::Cubic,
EasingVariety::Quartic,
EasingVariety::Quintic,
EasingVariety::Sin,
EasingVariety::Exponential,
EasingVariety::Circular,
] {
parameters.push((None, None, None, Some(Easing::In(variety))));
parameters.push((None, None, None, Some(Easing::Out(variety))));
parameters.push((None, None, None, Some(Easing::InOut(variety))));
}
for (index, (duration, repeat, direction, easing)) in parameters.iter().enumerate() {
let animation_id = library.new_animation(|animation| {
animation.add_stage(clip_id.into());
if let &Some(duration) = duration {
animation.set_duration(duration);
}
if let &Some(repeat) = repeat {
animation.set_repeat(repeat);
}
if let &Some(direction) = direction {
animation.set_direction(direction);
}
if let &Some(easing) = easing {
animation.set_easing(easing);
}
});
commands
.spawn((
SpriteSheetBundle {
texture: texture.clone(),
atlas: TextureAtlas {
layout: layout.clone(),
..default()
},
transform: Transform::from_translation(grid_position(6, 6, index as u32)),
..default()
},
SpritesheetAnimation::from_id(animation_id),
))
// Add a label describing the parameters
.with_children(|builder| {
let mut description = String::new();
duration.inspect(|x| description.push_str(&format!("duration: {:?}\n", x)));
repeat.inspect(|x| description.push_str(&format!("repeat: {:?}\n", x)));
direction.inspect(|x| description.push_str(&format!("direction: {:?}\n", x)));
easing.inspect(|x| description.push_str(&format!("easing: {:?}\n", x)));
builder.spawn(Text2dBundle {
text: Text::from_section(
&description,
TextStyle {
font_size: 15.0,
color: Color::WHITE,
..default()
},
),
transform: Transform::from_xyz(0.0, -50.0, 0.0),
..default()
});
});
}
}
// The spritesheet has been generated with this Javascript code:
//
// const frames = 30;
// const radius = 10;
// const distance = 100;
//
// const canvas = document.getElementById('canvas');
// canvas.width = distance + radius * 2;
// canvas.height = frames * radius * 2;
// canvas.style.width = canvas.width + "px";
// canvas.style.height = canvas.height + "px";
// canvas.style.imageRendering = "pixelated";
//
// const ctx = canvas.getContext('2d');
// ctx.fillStyle = 'red';
//
// for (var frame = 0; frame < frames; ++frame) {
// var x = radius + (distance - 2 * radius) * frame / (frames - 1);
// var y = radius + frame * 2 * radius;
//
// ctx.beginPath();
// ctx.arc(x, y, radius, 0, Math.PI * 2, true);
// ctx.fill();
// }