From aba0c23bc7e86716f9834934528d3ebd57c46f7a Mon Sep 17 00:00:00 2001 From: Imbris Date: Fri, 11 Aug 2023 20:09:10 -0400 Subject: [PATCH] Make sync_me based of PresenceKind rather than being an independent field. --- common/src/comp/presence.rs | 21 +++++++++++++-------- common/src/region.rs | 4 ++-- server/src/events/player.rs | 3 +-- server/src/state_ext.rs | 12 +++--------- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/common/src/comp/presence.rs b/common/src/comp/presence.rs index d08cba3cf6..578d5f91f6 100644 --- a/common/src/comp/presence.rs +++ b/common/src/comp/presence.rs @@ -13,23 +13,16 @@ pub struct Presence { /// updated! pub kind: PresenceKind, pub lossy_terrain_compression: bool, - /// Controls whether this entity is synced to other clients. - /// - /// Note, if it ends up being useful this could be generalized to an - /// independent component that is required for any entity to be synced - /// (as an independent component it could use NullStorage). - pub sync_me: bool, } impl Presence { - pub fn new(view_distances: ViewDistances, kind: PresenceKind, sync_me: bool) -> Self { + pub fn new(view_distances: ViewDistances, kind: PresenceKind) -> Self { let now = Instant::now(); Self { terrain_view_distance: ViewDistance::new(view_distances.terrain, now), entity_view_distance: ViewDistance::new(view_distances.entity, now), kind, lossy_terrain_compression: false, - sync_me, } } } @@ -62,6 +55,18 @@ impl PresenceKind { None } } + + /// Controls whether this entity is synced to other clients. + /// + /// Note, if it ends up being useful this could be generalized to an + /// independent component that is required for any entity to be synced + /// (as an independent component it could use NullStorage). + pub fn sync_me(&self) -> bool { + match self { + Self::Spectator | Self::LoadingCharacter(_) => false, + Self::Character(_) | Self::Possessor => true, + } + } } #[derive(PartialEq, Debug, Clone, Copy)] diff --git a/common/src/region.rs b/common/src/region.rs index d6905eb94f..33f4762c7c 100644 --- a/common/src/region.rs +++ b/common/src/region.rs @@ -114,7 +114,7 @@ impl RegionMap { // Add any untracked entities for (pos, id) in (&pos, &entities, presence.maybe(), !&self.tracked_entities) .join() - .filter(|(_, _, presence, _)| presence.map_or(true, |p| p.sync_me)) + .filter(|(_, _, presence, _)| presence.map_or(true, |p| p.kind.sync_me())) .map(|(pos, e, _, _)| (pos, e.id())) .collect::>() { @@ -143,7 +143,7 @@ impl RegionMap { ) .join() { - let should_sync = maybe_presence.map_or(true, |p| p.sync_me); + let should_sync = maybe_presence.map_or(true, |p| p.kind.sync_me()); match maybe_pos { // Switch regions for entities which need switching // TODO don't check every tick (use velocity) (and use id to stagger) diff --git a/server/src/events/player.rs b/server/src/events/player.rs index 7f7bdc2536..91f10f54d3 100644 --- a/server/src/events/player.rs +++ b/server/src/events/player.rs @@ -130,7 +130,7 @@ pub fn handle_exit_ingame(server: &mut Server, entity: EcsEntity, skip_persisten let (maybe_character, sync_me) = state .read_storage::() .get(entity) - .map(|p| (p.kind.character_id(), p.sync_me)) + .map(|p| (p.kind.character_id(), p.kind.sync_me())) .unzip(); let maybe_rtsim = state.read_component_copied::(entity); state.mut_resource::().remove_entity( @@ -421,7 +421,6 @@ pub fn handle_possess(server: &mut Server, possessor_uid: Uid, possessee_uid: Ui // from overwriting original character info with stuff from the new character. kind: PresenceKind::Possessor, lossy_terrain_compression: presence.lossy_terrain_compression, - sync_me: presence.sync_me, }) } else { None diff --git a/server/src/state_ext.rs b/server/src/state_ext.rs index 2e499896d8..3b67a9a98e 100644 --- a/server/src/state_ext.rs +++ b/server/src/state_ext.rs @@ -604,7 +604,6 @@ impl StateExt for State { entity: view_distance, }, PresenceKind::Spectator, - false, )) } @@ -645,11 +644,7 @@ impl StateExt for State { self.write_component_ignore_entity_dead( entity, - Presence::new( - view_distances, - PresenceKind::LoadingCharacter(character_id), - false, - ), + Presence::new(view_distances, PresenceKind::LoadingCharacter(character_id)), ); // Tell the client its request was successful. @@ -674,7 +669,7 @@ impl StateExt for State { self.write_component_ignore_entity_dead( entity, - Presence::new(view_distances, PresenceKind::Spectator, false), + Presence::new(view_distances, PresenceKind::Spectator), ); // Tell the client its request was successful. @@ -709,7 +704,6 @@ impl StateExt for State { self.ecs() .write_resource::() .add_character(id, entity); - presence.sync_me = true; Ok(()) } else { Err("PresenceKind is not LoadingCharacter") @@ -1199,7 +1193,7 @@ impl StateExt for State { let (maybe_character, sync_me) = self .read_storage::() .get(entity) - .map(|p| (p.kind.character_id(), p.sync_me)) + .map(|p| (p.kind.character_id(), p.kind.sync_me())) .unzip(); let maybe_rtsim = self.read_component_copied::(entity);