if more than 1 player is in the area of a newly created chunk, skip additional copies

This commit is contained in:
Marcel Märtens 2021-03-29 12:21:07 +02:00
parent 3f10d003f9
commit fdd1eac6ed

View File

@ -77,6 +77,7 @@ impl<'a> System<'a> for Sys {
},
};
// Send the chunk to all nearby players.
let mut lazy_msg = None;
for (presence, pos, client) in (&presences, &positions, &clients).join() {
let chunk_pos = terrain.pos_key(pos.0.map(|e| e as i32));
// Subtract 2 from the offset before computing squared magnitude
@ -87,10 +88,13 @@ impl<'a> System<'a> for Sys {
.magnitude_squared();
if adjusted_dist_sqr <= presence.view_distance.pow(2) {
client.send_fallible(ServerGeneral::TerrainChunkUpdate {
key,
chunk: Ok(Box::new(chunk.clone())),
});
if lazy_msg.is_none() {
lazy_msg = Some(client.prepare(ServerGeneral::TerrainChunkUpdate {
key,
chunk: Ok(Box::new(chunk.clone())),
}));
}
lazy_msg.as_ref().map(|ref msg| client.send_prepared(&msg));
}
}