Skip to content
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
28 changes: 28 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl Plugin for ServerPlugin {
.init_resource::<MessageBuffer>()
.init_resource::<RelatedEntities>()
.init_resource::<FilterRegistry>()
.register_required_components::<Replicated, TicksTracked>()
.configure_sets(
PreUpdate,
(ServerSystems::ReceivePackets, ServerSystems::Receive).chain(),
Expand All @@ -142,6 +143,7 @@ impl Plugin for ServerPlugin {
.add_observer(handle_disconnect)
.add_observer(check_mutation_ticks)
.add_observer(buffer_despawn)
.add_observer(cleanup_unreplicated)
.add_observer(cleanup_storage)
.add_systems(
PreUpdate,
Expand Down Expand Up @@ -330,6 +332,20 @@ fn buffer_despawn(
}
}

fn cleanup_unreplicated(
despawn: On<Despawn, TicksTracked>,
state: Res<State<ServerState>>,
replicated: Query<&Replicated>,
mut clients: Query<&mut ClientTicks>,
) {
if *state == ServerState::Running && !replicated.contains(despawn.entity) {
trace!("cleaning up ticks for despawned `{}`", despawn.entity);
for mut ticks in &mut clients {
ticks.entities.remove(&despawn.entity);
}
}
}

fn cleanup_acks(
mutations_timeout: Duration,
) -> impl FnMut(Query<&mut ClientTicks>, Res<Time<Real>>) {
Expand Down Expand Up @@ -977,3 +993,15 @@ pub struct AuthorizedClient;
/// See its documentation for more details.
#[derive(Component, Reflect, Deref, DerefMut, Default, Debug, Clone)]
pub struct PriorityMap(EntityHashMap<f32>);

/// Marker for entities stored in [`ClientTicks`].
///
/// Marked as required for [`Replicated`] by [`ServerPlugin`].
///
/// Despawned entities with [`Replicated`] are automatically removed
/// from all [`ClientTicks`] during [`collect_despawns`].
///
/// If [`Replicated`] was removed before despawning, the despawn is not
/// replicated, so this marker is used to clean up [`ClientTicks`].
#[derive(Component, Default)]
struct TicksTracked;