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
use std::ops::RangeInclusive;
use std::time::Duration;
use bevy_ecs::prelude::*;
use bevy_reflect::{Reflect, TypeUuid};
#[derive(Debug, Clone, Default, Reflect, TypeUuid)]
#[reflect(Component)]
#[uuid = "6378e9c2-ecd1-4029-9cd5-801caf68517c"]
pub struct SpriteSheetAnimation {
pub frames: Vec<Frame>,
pub mode: AnimationMode,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Reflect)]
pub enum AnimationMode {
Once,
Repeat,
}
#[derive(Debug, Copy, Clone, Default, Reflect)]
pub struct Frame {
pub index: u32,
pub duration: Duration,
}
impl SpriteSheetAnimation {
#[must_use]
pub fn from_frames(frames: Vec<Frame>) -> Self {
Self {
frames,
mode: AnimationMode::default(),
}
}
#[must_use]
pub fn from_range(index_range: RangeInclusive<u32>, frame_duration: Duration) -> Self {
Self::from_frames(
index_range
.map(|index| Frame::new(index, frame_duration))
.collect(),
)
}
#[must_use]
pub fn once(mut self) -> Self {
self.mode = AnimationMode::Once;
self
}
#[must_use]
pub fn repeat(mut self) -> Self {
self.mode = AnimationMode::Repeat;
self
}
pub(crate) fn has_frames(&self) -> bool {
!self.frames.is_empty()
}
}
impl Default for AnimationMode {
#[inline]
fn default() -> Self {
Self::Repeat
}
}
impl Frame {
#[inline]
#[must_use]
pub fn new(index: u32, duration: Duration) -> Self {
Self { index, duration }
}
}