Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Carry time delta between tweens in a Sequence #39

Merged
merged 2 commits into from
Aug 3, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 52 additions & 15 deletions src/tweenable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,28 +660,26 @@ impl<T> Tweenable<T> for Sequence<T> {

fn tick(
&mut self,
delta: Duration,
mut delta: Duration,
target: &mut T,
entity: Entity,
event_writer: &mut EventWriter<TweenCompleted>,
) -> TweenState {
if self.index < self.tweens.len() {
let mut state = TweenState::Active;
self.time = (self.time + delta).min(self.duration);
self.time = (self.time + delta).min(self.duration);
while self.index < self.tweens.len() {
let tween = &mut self.tweens[self.index];
let tween_state = tween.tick(delta, target, entity, event_writer);
if tween_state == TweenState::Completed {
tween.rewind();
self.index += 1;
if self.index >= self.tweens.len() {
state = TweenState::Completed;
self.times_completed = 1;
}
let tween_remaining = tween.duration().mul_f32(1.0 - tween.progress());
djeedai marked this conversation as resolved.
Show resolved Hide resolved
if let TweenState::Active = tween.tick(delta, target, entity, event_writer) {
return TweenState::Active;
}
state
} else {
TweenState::Completed

tween.rewind();
delta -= tween_remaining;
self.index += 1;
}

self.times_completed = 1;
TweenState::Completed
}

fn times_completed(&self) -> u32 {
Expand Down Expand Up @@ -1245,6 +1243,45 @@ mod tests {
}
}

/// Test crossing tween boundaries in one tick.
#[test]
fn seq_tick_boundaries() {
let mut seq = Sequence::new((0..3).map(|i| {
Tween::new(
EaseMethod::Linear,
TweeningType::Once,
Duration::from_secs(1),
TransformPositionLens {
start: Vec3::splat(i as f32),
end: Vec3::splat((i + 1) as f32),
},
)
}));
let mut transform = Transform::default();

// Dummy world and event writer
let mut world = World::new();
world.insert_resource(Events::<TweenCompleted>::default());
let mut system_state: SystemState<EventWriter<TweenCompleted>> =
SystemState::new(&mut world);
let mut event_writer = system_state.get_mut(&mut world);

// Tick halfway through the first tween, then in one tick:
// - Finish the first tween
// - Start and finish the second tween
// - Start the third tween
for delta in [0.5, 2.0] {
seq.tick(
Duration::from_secs_f32(delta),
&mut transform,
Entity::from_raw(0),
&mut event_writer,
);
}
assert_eq!(seq.index(), 2);
assert!(transform.translation.abs_diff_eq(Vec3::splat(2.5), 1e-5));
}

/// Sequence::new() and various Sequence-specific methods
#[test]
fn seq_iter() {
Expand Down