From 6e5df25675604b88660a4faca97e99cb84280ed6 Mon Sep 17 00:00:00 2001 From: aligator Date: Thu, 11 Apr 2024 20:07:00 +0200 Subject: [PATCH] Add world root --- src/voxel_world_internal.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/voxel_world_internal.rs b/src/voxel_world_internal.rs index 10b03c5..2f9d028 100644 --- a/src/voxel_world_internal.rs +++ b/src/voxel_world_internal.rs @@ -61,9 +61,13 @@ pub(crate) struct NeedsMaterial(PhantomData); pub(crate) struct Internals(PhantomData); +#[derive(Component)] +pub struct WorldRoot(PhantomData); + impl Internals { /// Init the resources used internally by bevy_voxel_world pub fn setup(mut commands: Commands) { + commands.spawn(WorldRoot::(PhantomData)); commands.init_resource::>(); commands.init_resource::>(); commands.init_resource::>(); @@ -78,10 +82,14 @@ impl Internals { pub fn spawn_chunks( mut commands: Commands, mut chunk_map_write_buffer: ResMut>, + world_root: Query>>, chunk_map: Res>, configuration: Res, camera_info: CameraInfo, ) { + // Panic if no root exists as it is already inserted in the setup. + let world_root = world_root.get_single().unwrap(); + let (camera, cam_gtf) = camera_info.single(); let cam_pos = cam_gtf.translation().as_ivec3(); @@ -159,7 +167,7 @@ impl Internals { let has_chunk = ChunkMap::::contains_chunk(&chunk_position, &chunk_map_read_lock); if !has_chunk { - let chunk = Chunk::::new(chunk_position, commands.spawn(NeedsRemesh).id()); + let chunk = Chunk::::new(chunk_position, commands.entity(world_root).insert(NeedsRemesh).id()); chunk_map_write_buffer.push((chunk_position, ChunkData::with_entity(chunk.entity)));