Support multiple non-interacting chunk entities

This commit is contained in:
Jarrod Doyle 2024-01-31 16:43:58 +00:00
parent c31ddd9bea
commit b1fd987955
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 75 additions and 91 deletions

View File

@ -82,7 +82,7 @@ impl Chunk {
dirty_rect: DirtyRect::default(), dirty_rect: DirtyRect::default(),
}; };
let max_y = height / 10; let max_y = height / rand::thread_rng().gen_range(2..10);
for y in 0..=max_y { for y in 0..=max_y {
for x in 0..width { for x in 0..width {
initial.set_cell(x, y, Element::Water); initial.set_cell(x, y, Element::Water);
@ -121,29 +121,17 @@ impl Chunk {
} }
} }
pub fn place_sand_system(mut chunk: Query<&mut Chunk>) { pub fn place_sand_system(mut query: Query<&mut Chunk>) {
// We know for now there's only one chunk for mut chunk in &mut query {
let chunk = chunk.get_single_mut();
if chunk.is_err() {
return;
}
let mut chunk = chunk.unwrap();
let frac = chunk.width / 2; let frac = chunk.width / 2;
let x = (chunk.width - frac) / 2 + rand::thread_rng().gen_range(0..frac); let x = (chunk.width - frac) / 2 + rand::thread_rng().gen_range(0..frac);
let y = chunk.height - 1; let y = chunk.height - 1;
chunk.set_cell(x, y, Element::Sand); chunk.set_cell(x, y, Element::Sand);
}
} }
pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut chunk: Query<&mut Chunk>) { pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut query: Query<&mut Chunk>) {
// We know for now there's only one chunk for mut chunk in &mut query {
let chunk = chunk.get_single_mut();
if chunk.is_err() {
return;
}
let mut chunk = chunk.unwrap();
// Determine which Margolus neighbourhood offset we're using this update // Determine which Margolus neighbourhood offset we're using this update
let offset = if chunk.step == 0 { let offset = if chunk.step == 0 {
(0, 0) (0, 0)
@ -192,19 +180,14 @@ pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut chunk: Query<&mut
} }
} }
} }
}
} }
pub fn update_chunk_texture_system( pub fn update_chunk_texture_system(
mut images: ResMut<Assets<Image>>, mut images: ResMut<Assets<Image>>,
mut chunk: Query<(&mut Chunk, &Handle<Image>)>, mut query: Query<(&mut Chunk, &Handle<Image>)>,
) { ) {
// We know for now there's only one chunk for (mut chunk, image_handle) in &mut query {
let chunk = chunk.get_single_mut();
if chunk.is_err() {
return;
}
let (mut chunk, image_handle) = chunk.unwrap();
if !chunk.dirty_rect.is_dirty() { if !chunk.dirty_rect.is_dirty() {
return; return;
} }
@ -232,6 +215,7 @@ pub fn update_chunk_texture_system(
} }
chunk.dirty_rect.reset(); chunk.dirty_rect.reset();
}
} }
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]