Don't despawn entities so eagerly

This commit is contained in:
Joshua Barretto 2020-10-05 19:52:54 +01:00 committed by Snowram
parent 87edbf02ab
commit bb6ca9fcba

View File

@ -54,6 +54,7 @@ use common::{
},
outcome::Outcome,
recipe::default_recipe_book,
spiral::Spiral2d,
state::{State, TimeOfDay},
sync::WorldSyncExt,
terrain::TerrainChunkSize,
@ -525,7 +526,14 @@ impl Server {
!&self.state.ecs().read_storage::<comp::Player>(),
)
.join()
.filter(|(_, pos, _)| terrain.get(pos.0.map(|e| e.floor() as i32)).is_err())
.filter(|(_, pos, _)| {
let chunk_key = terrain.pos_key(pos.0.map(|e| e.floor() as i32));
// Check not only this chunk, but also all neighbours to avoid immediate
// despawning if the entity walks outside of a valid chunk
// briefly. If the entity isn't even near a loaded chunk then we get
// rid of it.
Spiral2d::new().all(|offs| terrain.get_key(chunk_key + offs).is_none())
})
.map(|(entity, _, _)| entity)
.collect::<Vec<_>>()
};