Skip to content

Commit

Permalink
Add world root
Browse files Browse the repository at this point in the history
  • Loading branch information
aligator committed Apr 11, 2024
1 parent 5188465 commit 6e5df25
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/voxel_world_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ pub(crate) struct NeedsMaterial<C>(PhantomData<C>);

pub(crate) struct Internals<C>(PhantomData<C>);

#[derive(Component)]
pub struct WorldRoot<C>(PhantomData<C>);

impl<C: VoxelWorldConfig> Internals<C> {
/// Init the resources used internally by bevy_voxel_world
pub fn setup(mut commands: Commands) {
commands.spawn(WorldRoot::<C>(PhantomData));
commands.init_resource::<ChunkMap<C>>();
commands.init_resource::<ChunkMapInsertBuffer<C>>();
commands.init_resource::<ChunkMapUpdateBuffer<C>>();
Expand All @@ -78,10 +82,14 @@ impl<C: VoxelWorldConfig> Internals<C> {
pub fn spawn_chunks(
mut commands: Commands,
mut chunk_map_write_buffer: ResMut<ChunkMapInsertBuffer<C>>,
world_root: Query<Entity, With<WorldRoot<C>>>,
chunk_map: Res<ChunkMap<C>>,
configuration: Res<C>,
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();

Expand Down Expand Up @@ -159,7 +167,7 @@ impl<C: VoxelWorldConfig> Internals<C> {
let has_chunk = ChunkMap::<C>::contains_chunk(&chunk_position, &chunk_map_read_lock);

if !has_chunk {
let chunk = Chunk::<C>::new(chunk_position, commands.spawn(NeedsRemesh).id());
let chunk = Chunk::<C>::new(chunk_position, commands.entity(world_root).insert(NeedsRemesh).id());

chunk_map_write_buffer.push((chunk_position, ChunkData::with_entity(chunk.entity)));

Expand Down

0 comments on commit 6e5df25

Please sign in to comment.