Skip to content

Commit

Permalink
Do not send despawns for hidden entities (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shatur authored Nov 27, 2024
1 parent 5ed1d19 commit a8e10a0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/core/replication/replicated_clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl ReplicatedClient {
///
/// Internal cleanup happens lazily during the iteration.
pub(crate) fn drain_lost_visibility(&mut self) -> impl Iterator<Item = Entity> + '_ {
self.visibility.drain_lost_visibility().inspect(|entity| {
self.visibility.drain_lost().inspect(|entity| {
self.mutation_ticks.remove(entity);
})
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/replication/replicated_clients/client_visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl ClientVisibility {
}

/// Drains all entities for which visibility was lost during this tick.
pub(super) fn drain_lost_visibility(&mut self) -> impl Iterator<Item = Entity> + '_ {
pub(super) fn drain_lost(&mut self) -> impl Iterator<Item = Entity> + '_ {
match &mut self.filter {
VisibilityFilter::All { .. } => VisibilityLostIter::AllVisible,
VisibilityFilter::Blacklist { added, .. } => VisibilityLostIter::Lost(added.drain()),
Expand Down Expand Up @@ -219,14 +219,14 @@ impl ClientVisibility {

/// Checks if a specific entity is visible.
pub fn is_visible(&self, entity: Entity) -> bool {
match self.visibility_state(entity) {
match self.state(entity) {
Visibility::Hidden => false,
Visibility::Gained | Visibility::Visible => true,
}
}

/// Returns visibility of a specific entity.
pub(crate) fn visibility_state(&self, entity: Entity) -> Visibility {
pub(crate) fn state(&self, entity: Entity) -> Visibility {
match &self.filter {
VisibilityFilter::All => Visibility::Visible,
VisibilityFilter::Blacklist { list, .. } => match list.get(&entity) {
Expand Down
7 changes: 5 additions & 2 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,11 @@ fn collect_despawns(
for entity in despawn_buffer.drain(..) {
let entity_range = serialized.write_entity(entity)?;
for ((message, _), client) in messages.iter_mut().zip(replicated_clients.iter_mut()) {
let visibility = client.visibility().state(entity);
if visibility != Visibility::Hidden {
message.add_despawn(entity_range.clone());
}
client.remove_despawned(entity);
message.add_despawn(entity_range.clone());
}
}

Expand Down Expand Up @@ -487,7 +490,7 @@ fn collect_changes(
for ((change_message, mutate_message), client) in
messages.iter_mut().zip(replicated_clients.iter())
{
let visibility = client.visibility().visibility_state(entity.id());
let visibility = client.visibility().state(entity.id());
change_message.start_entity_changes(visibility);
mutate_message.start_entity_mutations();
}
Expand Down

0 comments on commit a8e10a0

Please sign in to comment.