From 8c75efa08a6e716decdb18287bf045b0f7cfcd36 Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Mon, 29 Jan 2024 15:21:17 +0000 Subject: [PATCH] Add Chunk set_cell function --- src/falling_sand/mod.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/falling_sand/mod.rs b/src/falling_sand/mod.rs index 1f7e146..3845d28 100644 --- a/src/falling_sand/mod.rs +++ b/src/falling_sand/mod.rs @@ -75,6 +75,15 @@ impl Chunk { dirty_rect: DirtyRect::default(), } } + + pub fn set_cell(&mut self, x: usize, y: usize, element: Element) { + if x >= self.width || y >= self.height { + return; + } + + self.cells[x + y * self.width] = element; + self.dirty_rect.add_point(x, y); + } } pub fn place_sand_system(mut chunk: Query<&mut Chunk>) { @@ -88,9 +97,7 @@ pub fn place_sand_system(mut chunk: Query<&mut Chunk>) { let frac = chunk.width / 2; let x = (chunk.width - frac) / 2 + rand::thread_rng().gen_range(0..frac); let y = chunk.height - 1; - let index = x + y * chunk.width; - chunk.cells[index] = Element::Sand; - chunk.dirty_rect.add_point(x, y); + chunk.set_cell(x, y, Element::Sand); } pub fn simulate_chunk_system(mut chunk: Query<&mut Chunk>) {