lightyear_frame_interpolation 0.29.0

IO primitives for the lightyear networking library
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
//! This module is not related to the interpolating between server updates.
//! Instead, it is responsible for interpolating between FixedUpdate ticks during the Update state.
//!
//! Usually, the simulation is run during the FixedUpdate schedule so that it doesn't depend on frame rate.
//! This can cause some visual inconsistencies (jitter) because the frames (Update schedule) don't correspond exactly to
//! the FixedUpdate schedule: there can be frames with several fixed-update ticks, and some frames with no fixed-update ticks.
//!
//! To solve this, we visually display the state of the game with 1 tick of delay.
//! For example if on the Update state we have an overstep of 0.7 and the current tick is 10,
//! we display the state of the game interpolated at 0.7 between tick 9 and tick 10.
//!
//! Another way to solve this would be to run an extra 'partial' simulation step with 0.7 dt and use this for the visual state.
//!
//! To enable FrameInterpolation:
//! - register an interpolation rule for the component or bundle. Existing
//!   interpolation rules are reused by default; if the component should not use
//!   delayed interpolation history, register it with
//!   [`InterpolationFns::no_history`](lightyear_interpolation::rules::InterpolationFns::no_history).
//! - FrameInterpolation is not enabled by default; add [`FrameInterpolationPlugin`] manually
//! - To enable FrameInterpolation on a given entity, add the type-erased [`FrameInterpolate`] marker to it manually
//!
//! Frame interpolation writes through Bevy's change-detection-aware component
//! access. Systems ordered after [`FrameInterpolationSystems::Interpolate`] can
//! therefore use `Changed<C>` to react to the visual value.
//! ```rust,ignore
//! # use lightyear_frame_interpolation::prelude::*;
//! # use lightyear_interpolation::prelude::*;
//! # use bevy_app::App;
//! # use bevy_ecs::prelude::*;
//!
//! # #[derive(Component, PartialEq, Clone, Debug)]
//! # struct Component1(f32);
//! # fn lerp_component(start: Component1, end: Component1, t: f32) -> Component1 {
//! #     Component1(start.0 + (end.0 - start.0) * t)
//! # }
//!
//! let mut app = App::new();
//! app.add_plugins(FrameInterpolationPlugin);
//! app.interpolate_with::<Component1>(InterpolationFns::no_history(lerp_component));
//!
//! fn spawn_entity(mut commands: Commands) {
//!     commands.spawn(FrameInterpolate);
//! }
//! ```

#![no_std]

extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

mod archetypes;

use crate::archetypes::FrameInterpolationWorld;
use bevy_app::prelude::*;
use bevy_ecs::{
    component::{ComponentId, ComponentIdFor},
    observer::Observer,
    prelude::*,
    schedule::common_conditions::not,
};
use bevy_math::{
    Curve,
    curve::{Ease, EaseFunction, EasingCurve},
};
use bevy_reflect::Reflect;
use bevy_time::{Fixed, Time};
pub use lightyear_core::frame_interpolation::{FrameInterpolate, FrameInterpolationHistory};
use lightyear_core::timeline::is_in_rollback;
use lightyear_interpolation::registry::InterpolationRegistry;
use lightyear_interpolation::rules::frame_interpolate::{
    FrameHistoryComponent, FrameInterpolationContext,
};
use lightyear_replication::ReplicationSystems;
use lightyear_replication::deferred_entity::DeferredEntityCommands;
use serde::{Deserialize, Serialize};
use smallvec::SmallVec;

/// System sets used by [`FrameInterpolationPlugin`].
///
/// These sets help order the systems responsible for:
/// - Restoring the actual component values before other game logic.
/// - Updating the history of component values used for interpolation.
/// - Performing the visual interpolation itself.
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone, Copy)]
pub enum FrameInterpolationSystems {
    /// Restore the correct component values when we start the FixedMainLoop.
    Restore,
    /// Update the previous/current component values used for visual interpolation.
    Update,
    /// Interpolate the visual state of the game with 1 tick of delay.
    Interpolate,
}

#[deprecated(note = "Use FrameInterpolationSystems instead")]
pub type FrameInterpolationSet = FrameInterpolationSystems;

/// If present, this marker indicates that we will skip applying frame interpolation.
///
/// This can be useful for example if a character teleports and you don't want
/// to interpolate between the two positions.
///
/// You can add this directly on the client-side, or you can also add it on the
/// sender-side and replicate the component.
#[derive(Component, PartialEq, Serialize, Deserialize, Clone, Debug, Reflect)]
pub struct SkipFrameInterpolation;

/// Linear frame interpolation for components implementing [`Ease`].
pub fn linear_frame_interpolation<C: Ease + Clone>(start: C, end: C, t: f32) -> C {
    let curve = EasingCurve::new(start, end, EaseFunction::Linear);
    curve.sample_unchecked(t)
}

#[derive(Resource, Debug, Default)]
struct FrameHistoryComponents {
    components: SmallVec<[FrameHistoryComponent; 8]>,
}

/// Ensures every registered `FrameInterpolationHistory<C>` exists when an
/// entity has both `FrameInterpolate` and the matching live `C`.
///
/// This is installed once by [`FrameInterpolationPlugin::finish`]. It watches
/// `FrameInterpolate` plus every registered live component id that owns frame
/// history. When `FrameInterpolate` is added, it backfills all matching
/// histories on the entity. When a watched component is added to an already
/// frame-interpolated entity, it only checks components from that trigger.
fn insert_frame_histories_on_frame_interpolate(
    trigger: On<Add>,
    frame_history_components: Res<FrameHistoryComponents>,
    frame_interpolate_id: ComponentIdFor<FrameInterpolate>,
    mut commands: Commands,
) {
    let Some(archetype) = trigger.trigger().new_archetype else {
        return;
    };
    if !archetype.contains(frame_interpolate_id.get()) {
        return;
    }

    let added_components = trigger.trigger().components;
    let frame_interpolate_added = added_components.contains(&frame_interpolate_id.get());
    for component in &frame_history_components.components {
        if !frame_interpolate_added && !added_components.contains(&component.live_component_id()) {
            continue;
        }
        if archetype.contains(component.live_component_id())
            && !archetype.contains(component.history_component_id())
        {
            (component.insert_history())(trigger.entity, &mut commands);
        }
    }
}

fn install_frame_history_observer(app: &mut App) {
    let frame_interpolate_id = app.world_mut().register_component::<FrameInterpolate>();
    let components = {
        let registry = app.world().resource::<InterpolationRegistry>();
        let mut components = SmallVec::<[FrameHistoryComponent; 8]>::new();
        for component in registry.frame_history_components() {
            if !components
                .iter()
                .any(|existing: &FrameHistoryComponent| existing.kind() == component.kind())
            {
                components.push(component);
            }
        }
        components
    };
    if components.is_empty() {
        return;
    }

    let mut watched_components = SmallVec::<[ComponentId; 8]>::new();
    watched_components.push(frame_interpolate_id);
    watched_components.extend(
        components
            .iter()
            .map(FrameHistoryComponent::live_component_id),
    );
    app.world_mut()
        .insert_resource(FrameHistoryComponents { components });
    app.world_mut().spawn(
        Observer::new(insert_frame_histories_on_frame_interpolate)
            .with_components(watched_components),
    );
}

/// Bevy plugin that enables type-erased frame interpolation.
///
/// This plugin adds systems to store fixed-update component values and then
/// interpolate between the previous and current fixed tick during `PostUpdate`,
/// using [`Time<Fixed>`]'s overstep. This helps smooth visuals when the
/// rendering framerate doesn't align perfectly with the fixed simulation rate.
///
/// To use this, register an interpolation rule in the [`InterpolationRegistry`]
/// and add [`FrameInterpolate`] to entities for which you want visual
/// interpolation.
///
/// # Examples
///
/// ```rust,ignore
/// use bevy_app::App;
/// use bevy_ecs::prelude::*;
/// use lightyear_frame_interpolation::prelude::*;
/// use lightyear_interpolation::prelude::*;
///
/// #[derive(Component, Clone, PartialEq)]
/// struct Position(f32);
///
/// fn lerp_position(start: Position, end: Position, t: f32) -> Position {
///     Position(start.0 + (end.0 - start.0) * t)
/// }
///
/// let mut app = App::new();
/// app.add_plugins(FrameInterpolationPlugin);
/// app.interpolate_with::<Position>(InterpolationFns::no_history(lerp_position));
/// ```
#[derive(Default)]
pub struct FrameInterpolationPlugin;

impl Plugin for FrameInterpolationPlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<InterpolationRegistry>();
        // SETS
        app.configure_sets(
            RunFixedMainLoop,
            FrameInterpolationSystems::Restore.in_set(RunFixedMainLoopSystems::BeforeFixedMainLoop),
        );
        // We don't update frame interpolation history during rollback because
        // that would be a waste to do for each rollback frame.
        // At the end of rollback, lightyear_prediction manually updates the
        // FrameInterpolationHistory component.
        app.configure_sets(
            FixedPostUpdate,
            FrameInterpolationSystems::Update.run_if(not(is_in_rollback)),
        );
        app.configure_sets(
            PostUpdate,
            FrameInterpolationSystems::Interpolate
                .before(bevy_transform::TransformSystems::Propagate)
                // We don't want the visual interpolation value to be the one replicated.
                .after(ReplicationSystems::Send),
        );

        // SYSTEMS
        app.add_systems(
            RunFixedMainLoop,
            restore_frame_interpolation.in_set(FrameInterpolationSystems::Restore),
        );
        app.add_systems(
            FixedPostUpdate,
            update_frame_interpolation_histories.in_set(FrameInterpolationSystems::Update),
        );
        app.add_systems(
            PostUpdate,
            apply_frame_interpolation.in_set(FrameInterpolationSystems::Interpolate),
        );
    }

    fn finish(&self, app: &mut App) {
        install_frame_history_observer(app);
    }
}

pub(crate) fn restore_frame_interpolation(
    mut frame_world: FrameInterpolationWorld,
    interpolation_registry: Res<InterpolationRegistry>,
) {
    frame_world.update_archetypes(&interpolation_registry);
    let world = frame_world.world;
    for (archetype, cached_archetype) in frame_world.iter_archetypes() {
        for component in cached_archetype.history_components() {
            (component.restore_frame_history())(world, archetype, component);
        }
    }
}

pub(crate) fn update_frame_interpolation_histories(
    mut frame_world: FrameInterpolationWorld,
    interpolation_registry: Res<InterpolationRegistry>,
    mut commands: Commands,
) {
    let mut deferred_apply = DeferredEntityCommands::default();

    frame_world.update_archetypes(&interpolation_registry);
    let world = frame_world.world;
    for (archetype, cached_archetype) in frame_world.iter_archetypes() {
        for component in cached_archetype.history_components() {
            (component.update_frame_history())(world, archetype, component, &mut deferred_apply);
        }
    }

    deferred_apply.apply(&mut commands);
}

pub(crate) fn apply_frame_interpolation(
    time: Res<Time<Fixed>>,
    mut frame_world: FrameInterpolationWorld,
    interpolation_registry: Res<InterpolationRegistry>,
    mut commands: Commands,
) {
    let mut deferred_apply = DeferredEntityCommands::default();
    let ctx = FrameInterpolationContext {
        overstep: time.overstep_fraction().clamp(0.0, 1.0),
        sample_delta_secs: Some(time.timestep().as_secs_f32()),
    };

    frame_world.update_archetypes(&interpolation_registry);
    let world = frame_world.world;
    for (archetype, cached_archetype) in frame_world.iter_archetypes() {
        for component in cached_archetype.apply_rules() {
            (component.apply_frame_interpolation())(
                world,
                archetype,
                &interpolation_registry,
                component.rule_id(),
                ctx,
                cached_archetype.skip_interpolation(),
                &mut deferred_apply,
            );
        }
    }

    deferred_apply.apply(&mut commands);
}

/// Common frame interpolation exports.
pub mod prelude {
    #[allow(deprecated)]
    pub use crate::{
        FrameInterpolate, FrameInterpolationHistory, FrameInterpolationPlugin,
        FrameInterpolationSet, FrameInterpolationSystems, SkipFrameInterpolation,
        linear_frame_interpolation,
    };
}

#[cfg(test)]
mod unit_tests {
    use super::*;
    use bevy_app::{App, FixedPostUpdate, PostUpdate, RunFixedMainLoop};
    use bevy_ecs::observer::Observer;
    use bevy_replicon::prelude::RepliconSharedPlugin;
    use bevy_state::app::StatesPlugin;
    use core::time::Duration;
    use lightyear_core::prelude::ConfirmedHistory;
    use lightyear_interpolation::registry::AppInterpolationExt;
    use lightyear_interpolation::rules::{
        InterpolationFns, InterpolationFnsExt, InterpolationSampleContext,
    };
    use lightyear_replication::prelude::AppComponentExt;
    use serde::{Deserialize, Serialize};

    #[derive(Component, Clone, Debug, PartialEq, Serialize, Deserialize)]
    struct FrameA(f32);

    #[derive(Component, Clone, Debug, PartialEq)]
    struct FrameB(f32);

    #[derive(Component, Clone, Debug, PartialEq)]
    #[component(storage = "SparseSet")]
    struct SparseFrame(f32);

    #[derive(Resource, Default)]
    struct ObservedFrameInterpolationChanges {
        table: bool,
        sparse_set: bool,
    }

    fn observe_frame_interpolation_changes(
        table: Query<(), Changed<FrameA>>,
        sparse_set: Query<(), Changed<SparseFrame>>,
        mut observed: ResMut<ObservedFrameInterpolationChanges>,
    ) {
        observed.table = !table.is_empty();
        observed.sparse_set = !sparse_set.is_empty();
    }

    fn bundle_lerp(start: (FrameA, FrameB), end: (FrameA, FrameB), t: f32) -> (FrameA, FrameB) {
        (
            FrameA(100.0 + start.0.0 + (end.0.0 - start.0.0) * t),
            FrameB(200.0 + start.1.0 + (end.1.0 - start.1.0) * t),
        )
    }

    fn bundle_context_lerp(
        _start: (FrameA, FrameB),
        _end: (FrameA, FrameB),
        context: InterpolationSampleContext,
    ) -> (FrameA, FrameB) {
        (
            FrameA(context.t),
            FrameB(context.sample_delta_secs.unwrap_or_default()),
        )
    }

    #[test]
    fn frame_history_observer_inserts_history_when_frame_interpolate_is_added() {
        let mut app = App::new();
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|start, end, t| {
            FrameA(start.0 + (end.0 - start.0) * t)
        }));
        app.finish();

        let entity = app.world_mut().spawn(FrameA(1.0)).id();
        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameA>>(entity)
                .is_none()
        );

        app.world_mut().entity_mut(entity).insert(FrameInterpolate);

        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameA>>(entity)
                .is_some()
        );
    }

    #[test]
    fn frame_history_observer_inserts_history_when_component_is_added() {
        let mut app = App::new();
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|start, end, t| {
            FrameA(start.0 + (end.0 - start.0) * t)
        }));
        app.finish();

        let entity = app.world_mut().spawn(FrameInterpolate).id();
        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameA>>(entity)
                .is_none()
        );

        app.world_mut().entity_mut(entity).insert(FrameA(1.0));

        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameA>>(entity)
                .is_some()
        );
    }

    #[test]
    fn frame_history_observer_inserts_history_when_both_are_spawned() {
        let mut app = App::new();
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|start, end, t| {
            FrameA(start.0 + (end.0 - start.0) * t)
        }));
        app.finish();

        let entity = app.world_mut().spawn((FrameA(1.0), FrameInterpolate)).id();

        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameA>>(entity)
                .is_some()
        );
    }

    #[test]
    fn frame_history_observer_is_shared_by_registered_components() {
        let mut app = App::new();
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|start, end, t| {
            FrameA(start.0 + (end.0 - start.0) * t)
        }));
        app.interpolate_with::<FrameB>(InterpolationFns::no_history(|start, end, t| {
            FrameB(start.0 + (end.0 - start.0) * t)
        }));
        app.finish();

        let observer_count = app
            .world_mut()
            .query::<&Observer>()
            .iter(app.world())
            .count();
        assert_eq!(observer_count, 1);

        let entity = app
            .world_mut()
            .spawn((FrameA(1.0), FrameB(2.0), FrameInterpolate))
            .id();

        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameA>>(entity)
                .is_some()
        );
        assert!(
            app.world()
                .get::<FrameInterpolationHistory<FrameB>>(entity)
                .is_some()
        );
    }

    #[test]
    fn frame_bundle_interpolation_uses_tuple_rule() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)));
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .accumulate_overstep(Duration::from_millis(500));
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_bundle_with::<(FrameA, FrameB)>(InterpolationFns::no_history(bundle_lerp));

        let entity = app
            .world_mut()
            .spawn((
                FrameInterpolate,
                FrameA(-1.0),
                FrameB(-1.0),
                FrameInterpolationHistory::<FrameA> {
                    previous_value: Some(FrameA(0.0)),
                    current_value: Some(FrameA(10.0)),
                },
                FrameInterpolationHistory::<FrameB> {
                    previous_value: Some(FrameB(0.0)),
                    current_value: Some(FrameB(20.0)),
                },
            ))
            .id();
        app.world_mut().clear_trackers();
        let first_changed = app
            .world()
            .entity(entity)
            .get_change_ticks::<FrameA>()
            .unwrap()
            .changed;
        let second_changed = app
            .world()
            .entity(entity)
            .get_change_ticks::<FrameB>()
            .unwrap()
            .changed;

        app.world_mut().run_schedule(PostUpdate);

        assert_eq!(app.world().get::<FrameA>(entity), Some(&FrameA(105.0)));
        assert_eq!(app.world().get::<FrameB>(entity), Some(&FrameB(210.0)));
        assert_ne!(
            app.world()
                .entity(entity)
                .get_change_ticks::<FrameA>()
                .unwrap()
                .changed,
            first_changed
        );
        assert_ne!(
            app.world()
                .entity(entity)
                .get_change_ticks::<FrameB>()
                .unwrap()
                .changed,
            second_changed
        );
    }

    #[test]
    fn frame_bundle_interpolation_receives_fixed_timestep() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(2)));
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .accumulate_overstep(Duration::from_millis(500));
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_bundle_with::<(FrameA, FrameB)>(InterpolationFns::no_history_with_context(
            bundle_context_lerp,
        ));

        let entity = app
            .world_mut()
            .spawn((
                FrameInterpolate,
                FrameA(-1.0),
                FrameB(-1.0),
                FrameInterpolationHistory::<FrameA> {
                    previous_value: Some(FrameA(0.0)),
                    current_value: Some(FrameA(10.0)),
                },
                FrameInterpolationHistory::<FrameB> {
                    previous_value: Some(FrameB(0.0)),
                    current_value: Some(FrameB(20.0)),
                },
            ))
            .id();

        app.world_mut().run_schedule(PostUpdate);

        assert_eq!(app.world().get::<FrameA>(entity), Some(&FrameA(0.25)));
        assert_eq!(app.world().get::<FrameB>(entity), Some(&FrameB(2.0)));
    }

    #[test]
    fn frame_interpolation_reuses_no_history_interpolation_rule() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)));
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .accumulate_overstep(Duration::from_millis(250));
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|start, end, t| {
            FrameA(10.0 + start.0 + (end.0 - start.0) * t)
        }));

        assert!(
            app.world()
                .components()
                .component_id::<ConfirmedHistory<FrameA>>()
                .is_none()
        );

        let entity = app
            .world_mut()
            .spawn((
                FrameInterpolate,
                FrameA(-1.0),
                FrameInterpolationHistory::<FrameA> {
                    previous_value: Some(FrameA(0.0)),
                    current_value: Some(FrameA(8.0)),
                },
            ))
            .id();
        app.world_mut().clear_trackers();
        let changed = app
            .world()
            .entity(entity)
            .get_change_ticks::<FrameA>()
            .unwrap()
            .changed;

        app.world_mut().run_schedule(PostUpdate);

        assert_eq!(app.world().get::<FrameA>(entity), Some(&FrameA(12.0)));
        assert_ne!(
            app.world()
                .entity(entity)
                .get_change_ticks::<FrameA>()
                .unwrap()
                .changed,
            changed
        );
    }

    #[test]
    fn frame_interpolation_reuses_history_only_interpolation_rule() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)));
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .accumulate_overstep(Duration::from_millis(250));
        app.add_plugins((StatesPlugin, RepliconSharedPlugin::default()));
        app.component::<FrameA>().replicate();
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(
            InterpolationFns::history_only()
                .interpolate(|start, end, t| FrameA(20.0 + start.0 + (end.0 - start.0) * t)),
        );

        assert!(
            app.world()
                .components()
                .component_id::<ConfirmedHistory<FrameA>>()
                .is_some()
        );

        let entity = app
            .world_mut()
            .spawn((
                FrameInterpolate,
                FrameA(-1.0),
                FrameInterpolationHistory::<FrameA> {
                    previous_value: Some(FrameA(4.0)),
                    current_value: Some(FrameA(12.0)),
                },
            ))
            .id();

        app.world_mut().run_schedule(PostUpdate);

        assert_eq!(app.world().get::<FrameA>(entity), Some(&FrameA(26.0)));
    }

    #[test]
    fn frame_interpolation_uses_filtered_rule_for_frame_interpolate_marker() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)));
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .accumulate_overstep(Duration::from_millis(250));
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|start, end, t| {
            FrameA(10.0 + start.0 + (end.0 - start.0) * t)
        }));
        app.interpolate_with_priority_filtered::<FrameA, With<FrameInterpolate>>(
            100,
            InterpolationFns::no_history(|start, end, t| {
                FrameA(20.0 + start.0 + (end.0 - start.0) * t)
            }),
        );

        let entity = app
            .world_mut()
            .spawn((
                FrameInterpolate,
                FrameA(-1.0),
                FrameInterpolationHistory::<FrameA> {
                    previous_value: Some(FrameA(4.0)),
                    current_value: Some(FrameA(12.0)),
                },
            ))
            .id();

        app.world_mut().run_schedule(PostUpdate);

        assert_eq!(app.world().get::<FrameA>(entity), Some(&FrameA(26.0)));
    }

    #[test]
    fn frame_interpolation_supports_sparse_set_live_component() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)));
        app.world_mut()
            .resource_mut::<Time<Fixed>>()
            .accumulate_overstep(Duration::from_millis(500));
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<SparseFrame>(InterpolationFns::no_history(|start, end, t| {
            SparseFrame(start.0 + (end.0 - start.0) * t)
        }));

        let entity = app
            .world_mut()
            .spawn((FrameInterpolate, SparseFrame(1.0)))
            .id();

        app.world_mut().run_schedule(FixedPostUpdate);
        assert_eq!(
            app.world()
                .get::<FrameInterpolationHistory<SparseFrame>>(entity)
                .and_then(|history| history.current_value.as_ref()),
            Some(&SparseFrame(1.0))
        );

        app.world_mut().entity_mut(entity).insert(SparseFrame(3.0));
        app.world_mut().run_schedule(FixedPostUpdate);
        let history = app
            .world()
            .get::<FrameInterpolationHistory<SparseFrame>>(entity)
            .unwrap();
        assert_eq!(history.previous_value, Some(SparseFrame(1.0)));
        assert_eq!(history.current_value, Some(SparseFrame(3.0)));
        app.world_mut().clear_trackers();
        let changed = app
            .world()
            .entity(entity)
            .get_change_ticks::<SparseFrame>()
            .unwrap()
            .changed;

        app.world_mut().run_schedule(PostUpdate);
        assert_eq!(
            app.world().get::<SparseFrame>(entity),
            Some(&SparseFrame(2.0))
        );
        assert_ne!(
            app.world()
                .entity(entity)
                .get_change_ticks::<SparseFrame>()
                .unwrap()
                .changed,
            changed
        );

        app.world_mut().run_schedule(RunFixedMainLoop);
        assert_eq!(
            app.world().get::<SparseFrame>(entity),
            Some(&SparseFrame(3.0))
        );
    }

    #[test]
    fn frame_interpolation_updates_change_detection_for_table_and_sparse_set_components() {
        let mut app = App::new();
        app.insert_resource(Time::<Fixed>::from_duration(Duration::from_secs(1)));
        app.add_plugins(FrameInterpolationPlugin);
        app.interpolate_with::<FrameA>(InterpolationFns::no_history(|_, end, _| end));
        app.interpolate_with::<SparseFrame>(InterpolationFns::no_history(|_, end, _| end));
        app.init_resource::<ObservedFrameInterpolationChanges>();
        app.add_systems(
            PostUpdate,
            observe_frame_interpolation_changes.after(FrameInterpolationSystems::Interpolate),
        );

        app.world_mut().spawn((
            FrameInterpolate,
            FrameA(0.0),
            SparseFrame(0.0),
            FrameInterpolationHistory::<FrameA> {
                previous_value: Some(FrameA(0.0)),
                current_value: Some(FrameA(1.0)),
            },
            FrameInterpolationHistory::<SparseFrame> {
                previous_value: Some(SparseFrame(0.0)),
                current_value: Some(SparseFrame(1.0)),
            },
        ));

        // Warm up the observer so the next run cannot observe the components' spawn ticks.
        app.world_mut().run_schedule(PostUpdate);
        *app.world_mut()
            .resource_mut::<ObservedFrameInterpolationChanges>() = Default::default();

        app.world_mut().run_schedule(PostUpdate);

        let observed = app.world().resource::<ObservedFrameInterpolationChanges>();
        assert!(observed.table);
        assert!(observed.sparse_set);
    }
}