Use ndarray for chunk cells

This commit is contained in:
Jarrod Doyle 2024-02-04 10:38:54 +00:00
parent 93ca0bc404
commit 1a702b7fd4
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
4 changed files with 61 additions and 9 deletions

51
Cargo.lock generated
View File

@ -2051,7 +2051,9 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"criterion", "criterion",
"ndarray",
"rand", "rand",
"rayon",
] ]
[[package]] [[package]]
@ -2406,6 +2408,16 @@ dependencies = [
"regex-automata 0.1.10", "regex-automata 0.1.10",
] ]
[[package]]
name = "matrixmultiply"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7574c1cf36da4798ab73da5b215bbf444f50718207754cb522201d78d1cd0ff2"
dependencies = [
"autocfg",
"rawpointer",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.7.1" version = "2.7.1"
@ -2496,6 +2508,20 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "ndarray"
version = "0.15.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32"
dependencies = [
"matrixmultiply",
"num-complex",
"num-integer",
"num-traits",
"rawpointer",
"rayon",
]
[[package]] [[package]]
name = "ndk" name = "ndk"
version = "0.7.0" version = "0.7.0"
@ -2582,6 +2608,15 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "num-complex"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ba157ca0885411de85d6ca030ba7e2a83a28636056c7c699b07c8b6f7383214"
dependencies = [
"num-traits",
]
[[package]] [[package]]
name = "num-derive" name = "num-derive"
version = "0.3.3" version = "0.3.3"
@ -2593,6 +2628,16 @@ dependencies = [
"syn 1.0.109", "syn 1.0.109",
] ]
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.17" version = "0.2.17"
@ -2984,6 +3029,12 @@ version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9" checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "rawpointer"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60a357793950651c4ed0f3f52338f53b2f809f32d83a07f72909fa13e4c6c1e3"
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.8.1" version = "1.8.1"

View File

@ -8,7 +8,9 @@ edition = "2021"
[dependencies] [dependencies]
bevy = "0.12.1" bevy = "0.12.1"
criterion = "0.5.1" criterion = "0.5.1"
ndarray = { version = "0.15.6", features = ["rayon"] }
rand = "0.8.5" rand = "0.8.5"
rayon = "1.8.1"
[profile.dev.package."*"] [profile.dev.package."*"]
opt-level = 3 opt-level = 3

View File

@ -15,6 +15,7 @@ use bevy::{
sprite::SpriteBundle, sprite::SpriteBundle,
transform::components::Transform, transform::components::Transform,
}; };
use ndarray::Array2;
use rand::Rng; use rand::Rng;
use crate::util::DirtyRect; use crate::util::DirtyRect;
@ -75,7 +76,7 @@ pub struct Chunk {
step: usize, step: usize,
width: usize, width: usize,
height: usize, height: usize,
cells: Vec<Element>, cells: Array2<Element>,
dirty_rect: DirtyRect, dirty_rect: DirtyRect,
} }
@ -85,7 +86,7 @@ impl Chunk {
step: 0, step: 0,
width, width,
height, height,
cells: vec![Element::Air; width * height], cells: Array2::from_elem((width, height), Element::Air),
dirty_rect: DirtyRect::default(), dirty_rect: DirtyRect::default(),
}; };
@ -103,7 +104,7 @@ impl Chunk {
return; return;
} }
self.cells[x + y * self.width] = element; self.cells[(x, y)] = element;
self.dirty_rect.add_point(x, y); self.dirty_rect.add_point(x, y);
} }
@ -112,9 +113,7 @@ impl Chunk {
return; return;
} }
let i0 = x0 + y0 * self.width; self.cells.swap((x0, y0), (x1, y1));
let i1 = x1 + y1 * self.width;
self.cells.swap(i0, i1);
self.dirty_rect.add_point(x0, y0); self.dirty_rect.add_point(x0, y0);
self.dirty_rect.add_point(x1, y1); self.dirty_rect.add_point(x1, y1);
} }
@ -124,6 +123,6 @@ impl Chunk {
return None; return None;
} }
Some(self.cells[x + y * self.width]) Some(self.cells[(x, y)])
} }
} }

View File

@ -79,7 +79,7 @@ pub fn update_chunk_texture(
for y in chunk.dirty_rect.range_y() { for y in chunk.dirty_rect.range_y() {
for x in chunk.dirty_rect.range_x() { for x in chunk.dirty_rect.range_x() {
let mut colour = (0, 0, 0); let mut colour = (0, 0, 0);
if let Some(element) = chunk.cells.get(x + y * chunk.width) { if let Some(element) = chunk.get_cell(x, y) {
match element { match element {
Element::Air => colour = (25, 24, 26), Element::Air => colour = (25, 24, 26),
Element::Sand => colour = (255, 216, 102), Element::Sand => colour = (255, 216, 102),