Simplify swap code

This commit is contained in:
Jarrod Doyle 2024-01-29 16:55:16 +00:00
parent b1ea38febe
commit 759479cc21
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
1 changed files with 11 additions and 19 deletions

View File

@ -132,34 +132,26 @@ pub fn simulate_chunk_system(mut chunk: Query<&mut Chunk>) {
// Simulate sand // Simulate sand
for y in 0..chunk.height { for y in 0..chunk.height {
for x in 0..chunk.width { for x in 0..chunk.width {
let element = chunk.get_cell(x, y).unwrap(); match chunk.get_cell(x, y) {
match element { Some(&Element::Air) => {}
Element::Air => {} Some(&Element::Sand) => {
Element::Sand => {
if y == 0 { if y == 0 {
continue; continue;
} }
// Bottom let bottom = chunk.get_cell(x, y - 1);
if *chunk.get_cell(x, y - 1).unwrap() == Element::Air { let bottom_left = chunk.get_cell(x - 1, y - 1);
let bottom_right = chunk.get_cell(x + 1, y - 1);
if bottom == Some(&Element::Air) {
chunk.swap_cells(x, y, x, y - 1); chunk.swap_cells(x, y, x, y - 1);
continue; } else if x != 0 && bottom_left == Some(&Element::Air) {
}
// Bottom left
if x != 0 && *chunk.get_cell(x - 1, y - 1).unwrap() == Element::Air {
chunk.swap_cells(x, y, x - 1, y - 1); chunk.swap_cells(x, y, x - 1, y - 1);
continue; } else if x != chunk.width - 1 && bottom_right == Some(&Element::Air) {
}
// Bottom right
if x != chunk.width - 1
&& *chunk.get_cell(x + 1, y - 1).unwrap() == Element::Air
{
chunk.swap_cells(x, y, x + 1, y - 1); chunk.swap_cells(x, y, x + 1, y - 1);
continue;
} }
} }
None => {}
} }
} }
} }