Skip to content

Spawn multiple chunks in the tilemap_chunk example #19911

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion crates/bevy_sprite/src/tilemap_chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ fn on_insert_tilemap_chunk(mut world: DeferredWorld, HookContext { entity, .. }:
"Invalid indices length for tilemap chunk {} of size {}. Expected {}, got {}",
entity,
chunk_size,
expected_indices_length,
indices.len(),
expected_indices_length
);
return;
}
Expand Down
53 changes: 34 additions & 19 deletions examples/2d/tilemap_chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,37 +28,52 @@ fn setup(mut commands: Commands, assets: Res<AssetServer>) {

let chunk_size = UVec2::splat(64);
let tile_display_size = UVec2::splat(8);
let indices: Vec<Option<u16>> = (0..chunk_size.element_product())
.map(|_| rng.gen_range(0..5))
.map(|i| if i == 0 { None } else { Some(i - 1) })
.collect();

commands.spawn((
TilemapChunk {
chunk_size,
tile_display_size,
tileset: assets.load("textures/array_texture.png"),
..default()
},
TilemapChunkIndices(indices),
UpdateTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
));
let tileset = assets.load("textures/array_texture.png");

for x in 0..2 {
for y in 0..2 {
let indices: Vec<Option<u16>> = (0..chunk_size.element_product())
.map(|_| rng.gen_range(0..5))
.map(|i| if i == 0 { None } else { Some(i - 1) })
.collect();
commands.spawn((
TilemapChunk {
chunk_size,
tile_display_size,
tileset: tileset.clone(),
..default()
},
TilemapChunkIndices(indices),
Transform::from_translation(Vec3::new(
x as f32 * 512.0 - 256.0,
y as f32 * 512.0 - 256.0,
0.0,
)),
UpdateTimer(Timer::from_seconds(0.1, TimerMode::Repeating)),
));
}
}

commands.spawn(Camera2d);

commands.insert_resource(SeededRng(rng));
}

fn update_tileset_image(
chunk_query: Single<&TilemapChunk>,
chunk_query: Query<&TilemapChunk>,
mut events: EventReader<AssetEvent<Image>>,
mut images: ResMut<Assets<Image>>,
) {
let chunk = *chunk_query;
for event in events.read() {
if event.is_loaded_with_dependencies(chunk.tileset.id()) {
let image = images.get_mut(&chunk.tileset).unwrap();
image.reinterpret_stacked_2d_as_array(4);
if let AssetEvent::LoadedWithDependencies { id } = event {
for chunk in chunk_query.iter() {
if chunk.tileset.id() == *id {
let image = images.get_mut(&chunk.tileset).unwrap();
image.reinterpret_stacked_2d_as_array(4);
break;
}
}
}
}
}
Expand Down