Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Root Node & init_root #20

Merged
merged 6 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
.DS_Store
assets/.DS_Store
.VSCodeCounter
.VSCodeCounter
.idea
2 changes: 2 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub trait VoxelWorldConfig: Resource + Default + Clone {
fn init_custom_materials(&self) -> bool {
true
}

fn init_root(&self, mut _commands: Commands, _root: Entity) {}
}

#[derive(Resource, Clone, Default)]
Expand Down
28 changes: 25 additions & 3 deletions src/voxel_world_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ pub(crate) struct NeedsMaterial<C>(PhantomData<C>);

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

impl<C: VoxelWorldConfig> Internals<C> {
#[derive(Component)]
pub struct WorldRoot<C>(PhantomData<C>);

impl<C: VoxelWorldConfig> Internals<C>
where
C: VoxelWorldConfig,
{
/// Init the resources used internally by bevy_voxel_world
pub fn setup(mut commands: Commands) {
pub fn setup(mut commands: Commands, configuration: Res<C>) {
commands.init_resource::<ChunkMap<C>>();
commands.init_resource::<ChunkMapInsertBuffer<C>>();
commands.init_resource::<ChunkMapUpdateBuffer<C>>();
Expand All @@ -72,16 +78,30 @@ impl<C: VoxelWorldConfig> Internals<C> {
commands.init_resource::<MeshCacheInsertBuffer<C>>();
commands.init_resource::<ModifiedVoxels<C>>();
commands.init_resource::<VoxelWriteBuffer<C>>();

// Create the root node and allow to modify it by the configuration.
let world_root = commands
.spawn((
WorldRoot::<C>(PhantomData),
VisibilityBundle::default(),
TransformBundle::default(),
))
.id();
configuration.init_root(commands, world_root)
}

/// Find and spawn chunks in need of spawning
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<C>,
) {
// 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 +179,9 @@ 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_entity = commands.spawn(NeedsRemesh).id();
commands.entity(world_root).add_child(chunk_entity);
let chunk = Chunk::<C>::new(chunk_position, chunk_entity);

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

Expand Down
Loading