From 37f3e1edd969b4ede9dd033895ff806477f6e866 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tomas=20Wenstr=C3=B6m?= Date: Sat, 30 Jan 2021 16:25:33 +0100 Subject: [PATCH] Generic grids --- src/core/level/lvlgen.rs | 22 +++++++++++----------- src/core/level/mod.rs | 6 +++--- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/core/level/lvlgen.rs b/src/core/level/lvlgen.rs index d2a389e..e39f232 100644 --- a/src/core/level/lvlgen.rs +++ b/src/core/level/lvlgen.rs @@ -56,13 +56,13 @@ impl LevelGenerator { } #[allow(dead_code)] - fn simplex_noise(&self, grid: &mut Grid) { + fn simplex_noise(&self, grid: &mut Grid) { let noise = OpenSimplex::new().set_seed(self.seed); self.set_each(grid, |x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1); } #[allow(dead_code)] - fn random_noise(&self, grid: &mut Grid) { + fn random_noise(&self, grid: &mut Grid) { let mut rng: rand::prelude::StdRng = rand::SeedableRng::seed_from_u64(self.seed as u64); let noise = OpenSimplex::new().set_seed(self.seed); self.set_each(grid, |_x, _y| rng.gen_range(0, 100) > (45 + (150.0 * noise.get([_x as f64 / 40.0, _y as f64 / 10.0])) as usize), 1); // more horizontal platforms @@ -71,7 +71,7 @@ impl LevelGenerator { } #[allow(dead_code)] - fn smooth(&self, grid: &mut Grid) { + fn smooth(&self, grid: &mut Grid) { let distance = 1; for _i in 0..self.iterations { let mut next = vec!(vec!(true; grid.height); grid.width); @@ -93,7 +93,7 @@ impl LevelGenerator { } #[allow(dead_code)] - fn smooth_until_equilibrium(&self, grid: &mut Grid) { + fn smooth_until_equilibrium(&self, grid: &mut Grid) { let distance = 1; let mut count = 0; loop { @@ -129,7 +129,7 @@ impl LevelGenerator { count } - fn set_each bool>(&self, grid: &mut Grid, mut func: F, walls: usize) { + fn set_each bool>(&self, grid: &mut Grid, mut func: F, walls: usize) { for x in walls..(grid.width - walls) { for y in walls..(grid.height - walls) { grid.cells[x][y] = func(x, y); @@ -137,7 +137,7 @@ impl LevelGenerator { } } - fn subdivide(&self, grid: &mut Grid) -> Grid { + fn subdivide(&self, grid: &mut Grid) -> Grid { let (width, height) = (grid.width * 2, grid.height * 2); let mut cells = vec!(vec!(true; height); width); for x in 1..(width - 1) { @@ -153,7 +153,7 @@ impl LevelGenerator { } } - fn find_regions(&self, grid: &Grid) -> Vec { + fn find_regions(&self, grid: &Grid) -> Vec { time_scope!("finding all regions"); let mut regions = vec!(); let mut marked = vec!(vec!(false; grid.height); grid.width); @@ -167,7 +167,7 @@ impl LevelGenerator { regions } - fn get_region_at_point(&self, grid: &Grid, x: usize, y: usize, marked: &mut Vec>) -> Region { + fn get_region_at_point(&self, grid: &Grid, x: usize, y: usize, marked: &mut Vec>) -> Region { let value = grid.cells[x][y]; let mut cells = vec!(); let mut queue = vec!((x, y)); @@ -190,13 +190,13 @@ impl LevelGenerator { Region { value, cells } } - fn delete_region(&self, grid: &mut Grid, region: &Region) { + fn delete_region(&self, grid: &mut Grid, region: &Region) { for c in ®ion.cells { grid.cells[c.0][c.1] = !region.value; } } - fn filter_regions(&self, grid: &mut Grid) { + fn filter_regions(&self, grid: &mut Grid) { let min_wall_size = 0.0015; println!("grid size: ({}, {}) = {} cells", grid.width, grid.height, grid.width * grid.height); println!("min wall size: {}", (grid.width * grid.height) as f64 * min_wall_size); @@ -220,7 +220,7 @@ impl LevelGenerator { } } - fn find_walls(&self, grid: &Grid) -> Vec>> { + fn find_walls(&self, grid: &Grid) -> Vec>> { let mut walls = vec!(); for r in self.find_regions(&grid) { if r.value { diff --git a/src/core/level/mod.rs b/src/core/level/mod.rs index 78fb199..3336b95 100644 --- a/src/core/level/mod.rs +++ b/src/core/level/mod.rs @@ -11,7 +11,7 @@ pub use self::lvlgen::LevelGenerator; #[derive(Default)] pub struct Level { pub gravity: Point, - pub grid: Grid, + pub grid: Grid, walls: Vec>>, } @@ -48,9 +48,9 @@ impl Level { ////////// GRID //////////////////////////////////////////////////////////////// #[derive(Default)] -pub struct Grid { +pub struct Grid { pub width: usize, pub height: usize, pub cell_size: usize, - pub cells: Vec>, + pub cells: Vec>, } -- 2.11.0