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(),
};
let max_y = height / 10;
let max_y = height / rand::thread_rng().gen_range(2..10);
for y in 0..=max_y {
for x in 0..width {
initial.set_cell(x, y, Element::Water);
@ -121,29 +121,17 @@ impl Chunk {
}
}
pub fn place_sand_system(mut chunk: Query<&mut Chunk>) {
// We know for now there's only one chunk
let chunk = chunk.get_single_mut();
if chunk.is_err() {
return;
}
let mut chunk = chunk.unwrap();
pub fn place_sand_system(mut query: Query<&mut Chunk>) {
for mut chunk in &mut query {
let frac = chunk.width / 2;
let x = (chunk.width - frac) / 2 + rand::thread_rng().gen_range(0..frac);
let y = chunk.height - 1;
chunk.set_cell(x, y, Element::Sand);
}
pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut chunk: Query<&mut Chunk>) {
// We know for now there's only one chunk
let chunk = chunk.get_single_mut();
if chunk.is_err() {
return;
}
let mut chunk = chunk.unwrap();
pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut query: Query<&mut Chunk>) {
for mut chunk in &mut query {
// Determine which Margolus neighbourhood offset we're using this update
let offset = if chunk.step == 0 {
(0, 0)
@ -193,18 +181,13 @@ pub fn simulate_chunk_system(rules: Res<FallingSandRules>, mut chunk: Query<&mut
}
}
}
}
pub fn update_chunk_texture_system(
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
let chunk = chunk.get_single_mut();
if chunk.is_err() {
return;
}
let (mut chunk, image_handle) = chunk.unwrap();
for (mut chunk, image_handle) in &mut query {
if !chunk.dirty_rect.is_dirty() {
return;
}
@ -233,6 +216,7 @@ pub fn update_chunk_texture_system(
chunk.dirty_rect.reset();
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Element {