@@ -254,10 +254,10 @@ pub struct InLoadedChunk;
254254/// Update the [`InLoadedChunk`] component for all entities in the world.
255255pub fn update_in_loaded_chunk (
256256 mut commands : bevy_ecs:: system:: Commands ,
257- query : Query < ( Entity , & WorldName , & Position ) > ,
257+ query : Query < ( Entity , & WorldName , & Position , Option < & InLoadedChunk > ) > ,
258258 worlds : Res < Worlds > ,
259259) {
260- for ( entity, world_name, position) in & query {
260+ for ( entity, world_name, position, last_in_loaded_chunk ) in & query {
261261 let player_chunk_pos = ChunkPos :: from ( position) ;
262262 let Some ( world_lock) = worlds. get ( world_name) else {
263263 commands. entity ( entity) . remove :: < InLoadedChunk > ( ) ;
@@ -266,9 +266,13 @@ pub fn update_in_loaded_chunk(
266266
267267 let in_loaded_chunk = world_lock. read ( ) . chunks . get ( & player_chunk_pos) . is_some ( ) ;
268268 if in_loaded_chunk {
269- commands. entity ( entity) . insert ( InLoadedChunk ) ;
269+ if last_in_loaded_chunk. is_none ( ) {
270+ commands. entity ( entity) . insert ( InLoadedChunk ) ;
271+ }
270272 } else {
271- commands. entity ( entity) . remove :: < InLoadedChunk > ( ) ;
273+ if last_in_loaded_chunk. is_some ( ) {
274+ commands. entity ( entity) . remove :: < InLoadedChunk > ( ) ;
275+ }
272276 }
273277 }
274278}
0 commit comments