Skip to content

Commit

Permalink
simplify some mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaeleragni committed Jun 22, 2024
1 parent 109d535 commit 86fc8ff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
9 changes: 3 additions & 6 deletions src/client/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ pub(crate) fn entity_created_on_client(
) {
for id in query.iter_mut() {
let uuid = Uuid::new_v4();
track.uuid_to_entity.insert(uuid, id);
track.entity_to_uuid.insert(id, uuid);
client.send_message(
DefaultChannel::ReliableOrdered,
bincode::serialize(&Message::EntitySpawn { id: uuid }).unwrap(),
);
cmd.entity(id)
.remove::<SyncMark>()
.insert(SyncEntity { uuid });
track.uuid_to_entity.insert(uuid, id);
track.entity_to_uuid.insert(id, uuid);
debug!("New entity tracked on client {}", uuid);
}
}
Expand Down Expand Up @@ -80,13 +80,10 @@ pub(crate) fn react_on_changed_components(
let Ok(bin) = reflect_to_bin(change.data.as_reflect(), &registry) else {
continue;
};
let Some(id) = track.entity_to_uuid.get(&change.change_id.id) else {
continue;
};
client.send_message(
DefaultChannel::ReliableOrdered,
bincode::serialize(&Message::ComponentUpdated {
id: *id,
id: change.change_id.id,
name: change.change_id.name,
data: bin,
})
Expand Down
13 changes: 7 additions & 6 deletions src/lib_priv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{

#[derive(PartialEq, Eq, Hash)]
pub(crate) struct ComponentChangeId {
pub(crate) id: Entity,
pub(crate) id: Uuid,
pub(crate) name: String,
}

Expand Down Expand Up @@ -64,10 +64,7 @@ pub(crate) fn sync_mesh_enabled(tracker: Res<SyncTrackerRes>) -> bool {
impl SyncTrackerRes {
pub(crate) fn signal_component_changed(&mut self, id: Uuid, data: Box<dyn Reflect>) {
let name = data.get_represented_type_info().unwrap().type_path().into();
let Some(id) = self.uuid_to_entity.get(&id) else {
return;
};
let change_id = ComponentChangeId { id: *id, name };
let change_id = ComponentChangeId { id, name };
if self.pushed_component_from_network.contains(&change_id) {
self.pushed_component_from_network.remove(&change_id);
return;
Expand Down Expand Up @@ -95,12 +92,16 @@ impl SyncTrackerRes {
let component_data = bin_to_reflect(data, &registry);
let registration = registry.get_with_type_path(name.as_str()).unwrap();
let reflect_component = registration.data::<ReflectComponent>().unwrap();
let Some(sync_entity) = world.entity(e_id).get::<SyncEntity>() else {
return false;
};
let uuid = sync_entity.uuid;
let previous_value = reflect_component.reflect(world.entity(e_id));
if equals(previous_value, &*component_data) {
world
.resource_mut::<SyncTrackerRes>()
.pushed_component_from_network
.insert(ComponentChangeId { id: e_id, name });
.insert(ComponentChangeId { id: uuid, name });
let entity = &mut world.entity_mut(e_id);
reflect_component.apply_or_insert(entity, component_data.as_reflect(), &registry);
true
Expand Down
12 changes: 6 additions & 6 deletions src/server/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ pub(crate) fn entity_created_on_server(
bincode::serialize(&Message::EntitySpawn { id: uuid }).unwrap(),
);
}
let mut entity = commands.entity(id);
track.uuid_to_entity.insert(uuid, id);
track.entity_to_uuid.insert(id, uuid);
entity.remove::<SyncMark>().insert(SyncEntity { uuid });
commands
.entity(id)
.remove::<SyncMark>()
.insert(SyncEntity { uuid });
debug!("New entity tracked on server {}", uuid);
}
}

Expand Down Expand Up @@ -91,11 +94,8 @@ pub(crate) fn react_on_changed_components(
let Ok(bin) = reflect_to_bin(change.data.as_reflect(), &registry) else {
continue;
};
let Some(id) = track.entity_to_uuid.get(&change.change_id.id) else {
continue;
};
let msg = &Message::ComponentUpdated {
id: *id,
id: change.change_id.id,
name: change.change_id.name.clone(),
data: bin,
};
Expand Down

0 comments on commit 86fc8ff

Please sign in to comment.