Newer
Older
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
use bevy::{asset::Asset, ecs::component::Component, prelude::*};
use crate::{Animator, AnimatorState, AssetAnimator, TweeningType};
/// Plugin to add systems related to tweening
#[derive(Debug, Clone, Copy)]
pub struct TweeningPlugin;
impl Plugin for TweeningPlugin {
fn build(&self, app: &mut App) {
app.add_system(component_animator_system::<Transform>)
.add_system(component_animator_system::<Text>)
.add_system(component_animator_system::<Style>)
.add_system(component_animator_system::<Sprite>)
.add_system(asset_animator_system::<ColorMaterial>);
}
}
pub fn component_animator_system<T: Component>(
time: Res<Time>,
mut query: Query<(&mut T, &mut Animator<T>)>,
) {
for (ref mut target, ref mut animator) in query.iter_mut() {
if animator.state == AnimatorState::Playing {
animator.timer.tick(time.delta());
}
if animator.paused {
if animator.timer.just_finished() {
match animator.tweening_type {
TweeningType::Once { duration } => {
animator.timer.set_duration(duration);
}
TweeningType::Loop { duration, .. } => {
animator.timer.set_duration(duration);
}
TweeningType::PingPong { duration, .. } => {
animator.timer.set_duration(duration);
}
}
animator.timer.reset();
animator.paused = false;
}
} else {
if animator.timer.duration().as_secs_f32() != 0. {
let progress = animator.progress();
let factor = animator.ease_function.sample(progress);
animator.apply(target, factor);
}
if animator.timer.finished() {
match animator.tweening_type {
TweeningType::Once { .. } => {
//commands.entity(entity).remove::<Animator>();
}
TweeningType::Loop { pause, .. } => {
if let Some(pause) = pause {
animator.timer.set_duration(pause);
animator.paused = true;
}
animator.timer.reset();
}
TweeningType::PingPong { pause, .. } => {
if let Some(pause) = pause {
animator.timer.set_duration(pause);
animator.paused = true;
}
animator.timer.reset();
animator.direction = !animator.direction;
}
}
}
}
}
}
pub fn asset_animator_system<T: Asset>(
time: Res<Time>,
mut assets: ResMut<Assets<T>>,
mut query: Query<&mut AssetAnimator<T>>,
) {
for ref mut animator in query.iter_mut() {
if animator.state == AnimatorState::Playing {
animator.timer.tick(time.delta());
}
if animator.paused {
if animator.timer.just_finished() {
match animator.tweening_type {
TweeningType::Once { duration } => {
animator.timer.set_duration(duration);
}
TweeningType::Loop { duration, .. } => {
animator.timer.set_duration(duration);
}
TweeningType::PingPong { duration, .. } => {
animator.timer.set_duration(duration);
}
}
animator.timer.reset();
animator.paused = false;
}
} else {
if animator.timer.duration().as_secs_f32() != 0. {
let progress = animator.progress();
let factor = animator.ease_function.sample(progress);
if let Some(target) = assets.get_mut(animator.handle()) {
animator.apply(target, factor);
}
}
if animator.timer.finished() {
match animator.tweening_type {
TweeningType::Once { .. } => {
//commands.entity(entity).remove::<Animator>();
}
TweeningType::Loop { pause, .. } => {
if let Some(pause) = pause {
animator.timer.set_duration(pause);
animator.paused = true;
}
animator.timer.reset();
}
TweeningType::PingPong { pause, .. } => {
if let Some(pause) = pause {
animator.timer.set_duration(pause);
animator.paused = true;
}
animator.timer.reset();
animator.direction = !animator.direction;
}
}
}
}
}
}