Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
assign despawnmarker frames on-demand
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Sep 8, 2023
1 parent a4fabe8 commit 65a49ca
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
5 changes: 0 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,6 @@ impl Plugin for TimewarpPlugin {
.before(TimewarpSet::RollbackInitiated),
),
)
.add_systems(
self.config.schedule(),
systems::add_frame_to_freshly_added_despawn_markers
.in_set(TimewarpSet::RecordComponentValues),
)
.add_systems(
self.config.schedule(),
systems::check_for_rollback_completion.in_set(TimewarpSet::RollbackUnderwayGlobal),
Expand Down
28 changes: 12 additions & 16 deletions src/systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -509,24 +509,15 @@ pub(crate) fn check_for_rollback_completion(
}
// we keep track of the previous rollback mainly for integration tests
commands.insert_resource(PreviousRollback(rb.as_ref().clone()));
debug!("🛼🛼 Rollback complete. {:?}, resetting period", rb);
debug!(
"🛼🛼 Rollback complete. {:?}, frames: {}",
rb,
rb.range.end - rb.range.start
);
fx.period = rb.original_period.unwrap();
commands.remove_resource::<Rollback>();
}

/// despawn markers often added using DespawnMarker::new() for convenience, we fill them
/// with the current frame here.
pub(crate) fn add_frame_to_freshly_added_despawn_markers(
mut q: Query<&mut DespawnMarker, Added<DespawnMarker>>,
game_clock: Res<GameClock>,
) {
for mut despawn_marker in q.iter_mut() {
if despawn_marker.0.is_none() {
despawn_marker.0 = Some(game_clock.frame());
}
}
}

/// despawn marker means remove all useful components, pending actual despawn after
/// ROLLBACK_WINDOW frames have elapsed.
pub(crate) fn remove_components_from_despawning_entities<T: TimewarpComponent>(
Expand All @@ -545,13 +536,18 @@ pub(crate) fn remove_components_from_despawning_entities<T: TimewarpComponent>(
}

/// Once a [`DespawnMarker`] has been around for `rollback_frames`, do the actual despawn.
/// also for new DespawnMarkers that don't have a frame yet, add one.
pub(crate) fn despawn_entities_with_elapsed_despawn_marker(
q: Query<(Entity, &DespawnMarker)>,
mut q: Query<(Entity, &mut DespawnMarker)>,
mut commands: Commands,
game_clock: Res<GameClock>,
timewarp_config: Res<TimewarpConfig>,
) {
for (entity, marker) in q.iter() {
for (entity, mut marker) in q.iter_mut() {
if marker.0.is_none() {
marker.0 = Some(game_clock.frame());
continue;
}
if (marker.0.expect("Despawn marker should have a frame!")
+ timewarp_config.rollback_window)
== game_clock.frame()
Expand Down
3 changes: 1 addition & 2 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ impl TimewarpTraits for App {
record_component_history::<T>,
insert_components_at_prior_frames::<T>,
remove_components_from_despawning_entities::<T>
.after(record_component_history::<T>)
.after(add_frame_to_freshly_added_despawn_markers),
.after(record_component_history::<T>),
)
.in_set(TimewarpSet::RecordComponentValues),
)
Expand Down

0 comments on commit 65a49ca

Please sign in to comment.