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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#![warn(
future_incompatible,
nonstandard_style,
rust_2018_idioms,
missing_docs,
clippy::pedantic
)]
#![allow(clippy::needless_pass_by_value, clippy::module_name_repetitions)]
#![cfg_attr(nightly, feature(doc_auto_cfg))]
#![cfg_attr(
feature = "bevy-07",
doc = "
```no_run
# use bevy::prelude::*;
use benimator::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(AnimationPlugin::default()) // <-- Enable sprite-sheet animations
.add_startup_system(spawn)
// ...
.run()
}
fn spawn() { /* ... */ }
```
"
)]
#![cfg_attr(
feature = "bevy-07",
doc = "
```
# use std::time::Duration;
# use bevy::prelude::*;
# use benimator::*;
fn spawn(mut commands: Commands, mut animations: ResMut<Assets<SpriteSheetAnimation>>) {
// Create an animation
let animation_handle = animations.add(SpriteSheetAnimation::from_range(
0..=2, // Indices of the sprite atlas
Duration::from_secs_f64(1.0 / 12.0), // Duration of each frame
));
commands
.spawn_bundle(SpriteSheetBundle {
// TODO: Configure the sprite sheet
..Default::default()
})
// Insert the asset handle of the animation
.insert(animation_handle)
// Start the animation immediately
.insert(Play);
}
```
"
)]
#![cfg_attr(
feature = "bevy-07",
doc = "
```
# use bevy::prelude::*;
# use benimator::SpriteSheetAnimationState;
fn restart_anim_from_start(mut query: Query<&mut SpriteSheetAnimationState>) {
for mut state in query.iter_mut() {
state.reset();
}
}
```
"
)]
#![cfg_attr(
feature = "bevy-07",
doc = r#"
And then load it with bevy's `AssetServer`:
```
# use bevy::prelude::*;
# use benimator::*;
# fn load(mut commands: Commands, asset_server: Res<AssetServer>) {
let handle: Handle<SpriteSheetAnimation> = asset_server.load("player_run.animation.yml");
# }
```
"#
)]
#[cfg(test)]
#[macro_use]
extern crate rstest;
use std::time::Duration;
pub use animation::{Frame, SpriteSheetAnimation};
pub use state::SpriteSheetAnimationState;
#[allow(deprecated)]
pub use animation::AnimationMode;
#[cfg(feature = "load-from-file")]
pub use animation::{AnimationParseError, SpriteSheetAnimationLoader};
mod animation;
pub mod integration;
mod state;
#[non_exhaustive]
#[derive(Default)]
pub struct AnimationPlugin;
#[derive(Debug, Copy, Clone, Default)]
pub struct Play;
#[derive(Debug, Copy, Clone)]
pub struct PlaySpeedMultiplier(f64);
impl PlaySpeedMultiplier {
#[must_use]
pub fn new(multiplier: f64) -> Self {
Self(multiplier)
}
#[allow(dead_code)]
fn transform(self, duration: Duration) -> Duration {
duration.mul_f64(self.0)
}
}
impl Default for PlaySpeedMultiplier {
fn default() -> Self {
Self(1.0)
}
}
impl From<f64> for PlaySpeedMultiplier {
fn from(mult: f64) -> Self {
Self(mult)
}
}
impl From<f32> for PlaySpeedMultiplier {
fn from(mult: f32) -> Self {
Self(mult.into())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn identity_transformation() {
assert_eq!(
PlaySpeedMultiplier::default().transform(Duration::from_secs(1)),
Duration::from_secs(1)
);
}
#[test]
fn double_speed() {
assert_eq!(
PlaySpeedMultiplier::from(2.0).transform(Duration::from_secs(2)),
Duration::from_secs(4)
);
}
}