pub struct ChunkManager {
pub load_distance: f32,
pub unload_distance: f32,
/* private fields */
}Expand description
Central registry that tracks the state of every chunk in the streaming system.
ChunkManager is inserted as a Bevy Resource by WorldPlugin and
updated by systems::streaming_system every frame. It maintains two
internal sets:
- Loaded — chunks that have a live Bevy entity with a built mesh. Query
membership with
is_loadedand retrieve the entity withget_entity. - Pending — chunk coordinates for which a
ChunkLoadRequesthas been emitted but whose generation is not yet complete. Query membership withis_pending.
The two public distance fields, load_distance
and unload_distance, are read every frame
by the streaming system. They default to DEFAULT_LOAD_DISTANCE and
DEFAULT_UNLOAD_DISTANCE respectively, and can be overridden at runtime
or by loading a config::WorldConfig.
§Querying chunks
use bevy::prelude::*;
use voxel_framework::world::ChunkManager;
fn inspect_chunks(manager: Res<ChunkManager>) {
println!("loaded: {}, pending: {}", manager.loaded_count(), manager.pending_count());
for (coord, entity) in manager.iter_loaded() {
println!("chunk {:?} → entity {:?}", coord, entity);
}
}§Registering a generated chunk
After generating voxel data and spawning the entity, call
register to promote the coord from pending to
loaded:
use bevy::prelude::*;
use voxel_framework::world::ChunkManager;
fn finish_generation(mut manager: ResMut<ChunkManager>, mut commands: Commands) {
let coord = IVec3::new(1, 0, 0);
let entity = commands.spawn_empty().id();
manager.register(coord, entity);
}Fields§
§load_distance: f32Distance (world units) within which chunks are loaded.
unload_distance: f32Distance (world units) beyond which chunks are unloaded.
Implementations§
Source§impl ChunkManager
impl ChunkManager
Sourcepub fn new(load_distance: f32, unload_distance: f32) -> Self
pub fn new(load_distance: f32, unload_distance: f32) -> Self
Creates a new ChunkManager with the given streaming distances.
Sourcepub fn is_loaded(&self, coord: IVec3) -> bool
pub fn is_loaded(&self, coord: IVec3) -> bool
Returns true if the chunk at coord is fully loaded.
Sourcepub fn is_pending(&self, coord: IVec3) -> bool
pub fn is_pending(&self, coord: IVec3) -> bool
Returns true if the chunk at coord is queued or in-flight.
Sourcepub fn get_entity(&self, coord: IVec3) -> Option<Entity>
pub fn get_entity(&self, coord: IVec3) -> Option<Entity>
Returns the Bevy entity for a fully loaded chunk, if any.
Sourcepub fn mark_pending(&mut self, coord: IVec3)
pub fn mark_pending(&mut self, coord: IVec3)
Marks a chunk coord as pending load.
No-op if the chunk is already loaded or already pending.
Sourcepub fn register(&mut self, coord: IVec3, entity: Entity)
pub fn register(&mut self, coord: IVec3, entity: Entity)
Registers a fully loaded chunk with its Bevy entity.
Removes the coord from the pending set if it was there.
Sourcepub fn unregister(&mut self, coord: IVec3) -> Option<Entity>
pub fn unregister(&mut self, coord: IVec3) -> Option<Entity>
Unregisters a chunk, removing it from loaded and pending sets.
Returns the entity if it was loaded, None otherwise.
Sourcepub fn iter_loaded(&self) -> impl Iterator<Item = (IVec3, Entity)> + '_
pub fn iter_loaded(&self) -> impl Iterator<Item = (IVec3, Entity)> + '_
Returns an iterator over all loaded chunk coordinates and their entities.
Sourcepub fn loaded_count(&self) -> usize
pub fn loaded_count(&self) -> usize
Returns the number of fully loaded chunks.
Sourcepub fn pending_count(&self) -> usize
pub fn pending_count(&self) -> usize
Returns the number of chunks queued for loading.
Trait Implementations§
Source§impl Debug for ChunkManager
impl Debug for ChunkManager
Source§impl Default for ChunkManager
impl Default for ChunkManager
impl Resource for ChunkManager
Auto Trait Implementations§
impl Forget for ChunkManager
impl Freeze for ChunkManager
impl Move for ChunkManager
impl RefUnwindSafe for ChunkManager
impl Send for ChunkManager
impl Sync for ChunkManager
impl Unpin for ChunkManager
impl UnsafeUnpin for ChunkManager
impl UnwindSafe for ChunkManager
Blanket Implementations§
Source§impl<T, U> AsBindGroupShaderType<U> for T
impl<T, U> AsBindGroupShaderType<U> for T
Source§fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U
T ShaderType for self. When used in AsBindGroup
derives, it is safe to assume that all images in self exist.Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ConditionalSend for Twhere
T: Send,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
impl<S, T> Duplex<S> for Twhere
T: FromSample<S> + ToSample<S>,
Source§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> FromWorld for Twhere
T: Default,
impl<T> FromWorld for Twhere
T: Default,
Source§fn from_world(_world: &mut World) -> T
fn from_world(_world: &mut World) -> T
Creates Self using default().
Source§impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
impl<T, W> HasTypeWitness<W> for Twhere
W: MakeTypeWitness<Arg = T>,
T: ?Sized,
Source§impl<T> Identity for Twhere
T: ?Sized,
impl<T> Identity for Twhere
T: ?Sized,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more