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
use std::time::Duration;
use amethyst_assets::AssetStorage;
use amethyst_core::{duration_to_nanos, duration_to_secs, nanos_to_duration, secs_to_duration,
LocalTransform, Time};
use specs::{Fetch, Join, System, WriteStorage};
use interpolation::Interpolate;
use resources::{ControlState, EndControl, RestState, Sampler, SamplerControl, SamplerControlSet};
#[derive(Default)]
pub struct SamplerInterpolationSystem;
impl SamplerInterpolationSystem {
pub fn new() -> Self {
Default::default()
}
}
impl<'a> System<'a> for SamplerInterpolationSystem {
type SystemData = (
Fetch<'a, Time>,
Fetch<'a, AssetStorage<Sampler>>,
WriteStorage<'a, SamplerControlSet>,
WriteStorage<'a, LocalTransform>,
);
fn run(&mut self, (time, samplers, mut controls, mut transforms): Self::SystemData) {
for (control_set, transform) in (&mut controls, &mut transforms).join() {
if let Some((ref mut control, sampler)) = control_set
.translation
.as_mut()
.and_then(|c| samplers.get(&c.sampler).and_then(|s| Some((c, s))))
{
process_sampler(control, sampler, transform, &time);
}
if let Some((ref mut control, sampler)) = control_set
.rotation
.as_mut()
.and_then(|c| samplers.get(&c.sampler).and_then(|s| Some((c, s))))
{
process_sampler(control, sampler, transform, &time);
}
if let Some((ref mut control, sampler)) = control_set
.scale
.as_mut()
.and_then(|c| samplers.get(&c.sampler).and_then(|s| Some((c, s))))
{
process_sampler(control, sampler, transform, &time);
}
}
}
}
fn process_sampler(
control: &mut SamplerControl,
sampler: &Sampler,
transform: &mut LocalTransform,
time: &Time,
) {
use resources::ControlState::*;
let (new_state, new_end) = update_duration_and_check(&control, sampler, time);
if let Some(end) = new_end {
control.end = end.clone();
}
match new_state {
Running(duration) | Paused(duration) => {
do_sampling(&sampler, &duration, transform);
}
Done => do_end_control(&control, transform),
_ => {}
}
control.state = new_state;
}
fn do_end_control(control: &SamplerControl, transform: &mut LocalTransform) {
match control.end {
EndControl::Normal => match control.after {
RestState::Translation(tr) => transform.translation = tr.into(),
RestState::Rotation(r) => transform.rotation = r.into(),
RestState::Scale(s) => transform.scale = s.into(),
},
_ => {}
}
}
fn update_duration_and_check(
control: &SamplerControl,
sampler: &Sampler,
time: &Time,
) -> (ControlState, Option<EndControl>) {
use resources::ControlState::*;
match control.state {
Requested => (Running(Duration::from_secs(0)), None),
Abort => (Done, None),
Running(duration) => {
let zero = Duration::from_secs(0);
let current_dur = duration + time.delta_time();
let last_frame = sampler
.input
.last()
.cloned()
.map(secs_to_duration)
.unwrap_or(zero.clone());
if last_frame != zero && current_dur > last_frame {
match control.end {
EndControl::Loop(Some(i)) if i <= 1 => (Done, Some(EndControl::Normal)),
EndControl::Loop(None) => {
(Running(next_duration(last_frame, current_dur).0), None)
}
EndControl::Loop(Some(i)) => {
let (next_dur, loops_removed) = next_duration(last_frame, current_dur);
let remaining_loops = i - loops_removed;
if remaining_loops <= 1 {
(Done, Some(EndControl::Normal))
} else {
(
Running(next_dur),
Some(EndControl::Loop(Some(remaining_loops))),
)
}
}
_ => (Done, None),
}
} else {
(Running(current_dur), None)
}
}
ref state => (state.clone(), None),
}
}
fn next_duration(last_frame: Duration, duration: Duration) -> (Duration, u32) {
let animation_duration = duration_to_nanos(last_frame);
let current_duration = duration_to_nanos(duration);
let remain_duration = current_duration % animation_duration;
let loops = current_duration / animation_duration;
(nanos_to_duration(remain_duration), loops as u32)
}
fn do_sampling(sampler: &Sampler, duration: &Duration, local_transform: &mut LocalTransform) {
use resources::AnimationOutput::*;
let dur_s = duration_to_secs(*duration);
match sampler.output {
Translation(ref ts) => {
local_transform.translation = sampler
.ty
.interpolate(dur_s, &sampler.input, ts, false)
.into()
}
Rotation(ref rs) => {
local_transform.rotation = sampler
.ty
.interpolate(dur_s, &sampler.input, rs, true)
.into()
}
Scale(ref ss) => {
local_transform.scale = sampler
.ty
.interpolate(dur_s, &sampler.input, ss, false)
.into()
}
}
}