Use the spatial index in client updates

This commit is contained in:
Ryan 2022-06-19 01:01:48 -07:00
parent d709eb5ec8
commit 004f84370d
2 changed files with 35 additions and 18 deletions

View file

@ -29,7 +29,8 @@ use crate::slotmap::{Key, SlotMap};
use crate::util::{chunks_in_view_distance, is_chunk_in_view_distance};
use crate::var_int::VarInt;
use crate::{
ident, ChunkPos, Chunks, Entities, EntityId, Server, Text, Ticks, WorldMeta, LIBRARY_NAMESPACE,
ident, ChunkPos, Chunks, Entities, EntityId, Server, SpatialIndex, Text, Ticks, WorldMeta,
LIBRARY_NAMESPACE,
};
pub struct Clients {
@ -332,6 +333,7 @@ impl<'a> ClientMut<'a> {
&mut self,
server: &Server,
entities: &Entities,
spatial_index: &SpatialIndex,
chunks: &Chunks,
meta: &WorldMeta,
) {
@ -608,24 +610,33 @@ impl<'a> ClientMut<'a> {
});
}
// Spawn new entities within the view distance.
// TODO: use BVH
for (id, entity) in entities.iter() {
if self.position().distance(entity.position()) <= view_dist as f64 * 16.0
&& entity.typ() != EntityType::Marker
&& self.0.loaded_entities.insert(id)
{
self.send_packet(
entity
.spawn_packet(id)
.expect("should not be a marker entity"),
);
if let Some(meta) = entity.initial_metadata_packet(id) {
self.send_packet(meta);
let pos = self.position();
spatial_index.query::<_, _, ()>(
|bb| {
bb.projected_point(pos)
.distance(pos)
<= view_dist as f64 * 16.0
},
|id, _| {
if self.0.loaded_entities.insert(id) {
let entity = entities.get(id).unwrap();
if entity.typ() != EntityType::Marker {
self.send_packet(
entity
.spawn_packet(id)
.expect("should not be a marker entity"),
);
if let Some(meta) = entity.initial_metadata_packet(id) {
self.send_packet(meta);
}
}
}
}
}
None
},
);
self.0.old_position = self.0.new_position;
}

View file

@ -375,7 +375,13 @@ fn do_update_loop(server: Server, mut worlds: WorldsMut) -> ShutdownResult {
world.spatial_index.update(world.entities.reborrow());
world.clients.par_iter_mut().for_each(|(_, mut client)| {
client.update(&server, &world.entities, &world.chunks, &world.meta);
client.update(
&server,
&world.entities,
&world.spatial_index,
&world.chunks,
&world.meta,
);
});
world.entities.update();