Make sync_me based of PresenceKind rather than being an independent

field.
This commit is contained in:
Imbris
2023-08-11 20:09:10 -04:00
parent ecb27deeae
commit aba0c23bc7
4 changed files with 19 additions and 21 deletions

View File

@ -13,23 +13,16 @@ pub struct Presence {
/// updated! /// updated!
pub kind: PresenceKind, pub kind: PresenceKind,
pub lossy_terrain_compression: bool, 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 { 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(); let now = Instant::now();
Self { Self {
terrain_view_distance: ViewDistance::new(view_distances.terrain, now), terrain_view_distance: ViewDistance::new(view_distances.terrain, now),
entity_view_distance: ViewDistance::new(view_distances.entity, now), entity_view_distance: ViewDistance::new(view_distances.entity, now),
kind, kind,
lossy_terrain_compression: false, lossy_terrain_compression: false,
sync_me,
} }
} }
} }
@ -62,6 +55,18 @@ impl PresenceKind {
None 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)] #[derive(PartialEq, Debug, Clone, Copy)]

View File

@ -114,7 +114,7 @@ impl RegionMap {
// Add any untracked entities // Add any untracked entities
for (pos, id) in (&pos, &entities, presence.maybe(), !&self.tracked_entities) for (pos, id) in (&pos, &entities, presence.maybe(), !&self.tracked_entities)
.join() .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())) .map(|(pos, e, _, _)| (pos, e.id()))
.collect::<Vec<_>>() .collect::<Vec<_>>()
{ {
@ -143,7 +143,7 @@ impl RegionMap {
) )
.join() .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 { match maybe_pos {
// Switch regions for entities which need switching // Switch regions for entities which need switching
// TODO don't check every tick (use velocity) (and use id to stagger) // TODO don't check every tick (use velocity) (and use id to stagger)

View File

@ -130,7 +130,7 @@ pub fn handle_exit_ingame(server: &mut Server, entity: EcsEntity, skip_persisten
let (maybe_character, sync_me) = state let (maybe_character, sync_me) = state
.read_storage::<Presence>() .read_storage::<Presence>()
.get(entity) .get(entity)
.map(|p| (p.kind.character_id(), p.sync_me)) .map(|p| (p.kind.character_id(), p.kind.sync_me()))
.unzip(); .unzip();
let maybe_rtsim = state.read_component_copied::<common::rtsim::RtSimEntity>(entity); let maybe_rtsim = state.read_component_copied::<common::rtsim::RtSimEntity>(entity);
state.mut_resource::<IdMaps>().remove_entity( state.mut_resource::<IdMaps>().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. // from overwriting original character info with stuff from the new character.
kind: PresenceKind::Possessor, kind: PresenceKind::Possessor,
lossy_terrain_compression: presence.lossy_terrain_compression, lossy_terrain_compression: presence.lossy_terrain_compression,
sync_me: presence.sync_me,
}) })
} else { } else {
None None

View File

@ -604,7 +604,6 @@ impl StateExt for State {
entity: view_distance, entity: view_distance,
}, },
PresenceKind::Spectator, PresenceKind::Spectator,
false,
)) ))
} }
@ -645,11 +644,7 @@ impl StateExt for State {
self.write_component_ignore_entity_dead( self.write_component_ignore_entity_dead(
entity, entity,
Presence::new( Presence::new(view_distances, PresenceKind::LoadingCharacter(character_id)),
view_distances,
PresenceKind::LoadingCharacter(character_id),
false,
),
); );
// Tell the client its request was successful. // Tell the client its request was successful.
@ -674,7 +669,7 @@ impl StateExt for State {
self.write_component_ignore_entity_dead( self.write_component_ignore_entity_dead(
entity, entity,
Presence::new(view_distances, PresenceKind::Spectator, false), Presence::new(view_distances, PresenceKind::Spectator),
); );
// Tell the client its request was successful. // Tell the client its request was successful.
@ -709,7 +704,6 @@ impl StateExt for State {
self.ecs() self.ecs()
.write_resource::<IdMaps>() .write_resource::<IdMaps>()
.add_character(id, entity); .add_character(id, entity);
presence.sync_me = true;
Ok(()) Ok(())
} else { } else {
Err("PresenceKind is not LoadingCharacter") Err("PresenceKind is not LoadingCharacter")
@ -1199,7 +1193,7 @@ impl StateExt for State {
let (maybe_character, sync_me) = self let (maybe_character, sync_me) = self
.read_storage::<Presence>() .read_storage::<Presence>()
.get(entity) .get(entity)
.map(|p| (p.kind.character_id(), p.sync_me)) .map(|p| (p.kind.character_id(), p.kind.sync_me()))
.unzip(); .unzip();
let maybe_rtsim = self.read_component_copied::<RtSimEntity>(entity); let maybe_rtsim = self.read_component_copied::<RtSimEntity>(entity);