From: Tomas Wenström Date: Sat, 30 Jan 2021 17:01:03 +0000 (+0100) Subject: Made wall smoothness adjustable X-Git-Url: http://git.dolda2000.com/gitweb/?a=commitdiff_plain;h=9a6d1261d88ddaec58166d83b44b134948a9fabc;p=kaka%2Frust-sdl-test.git Made wall smoothness adjustable --- diff --git a/src/core/game.rs b/src/core/game.rs index 3dacac1..3e424fd 100644 --- a/src/core/game.rs +++ b/src/core/game.rs @@ -26,7 +26,7 @@ pub struct GameState { impl GameState { pub fn new() -> Self { - let lvlgen = LevelGenerator::new(0, 5); + let lvlgen = LevelGenerator::new(0); GameState { world: World::new(lvlgen.generate()), lvlgen, @@ -66,13 +66,21 @@ impl AppState for GameState { } Event::KeyDown { keycode: Some(Keycode::KpPlus), .. } => { self.lvlgen.iterations += 1; - println!("{} iteration(s) of cellular automata", self.lvlgen.iterations); self.world.level = self.lvlgen.generate(); } Event::KeyDown { keycode: Some(Keycode::KpMinus), .. } => { if self.lvlgen.iterations > 0 { self.lvlgen.iterations -= 1; - println!("{} iteration(s) of cellular automata", self.lvlgen.iterations); + self.world.level = self.lvlgen.generate(); + } + } + Event::KeyDown { keycode: Some(Keycode::KpMultiply), .. } => { + self.lvlgen.wall_smooth_radius += 1; + self.world.level = self.lvlgen.generate(); + } + Event::KeyDown { keycode: Some(Keycode::KpDivide), .. } => { + if self.lvlgen.wall_smooth_radius > 0 { + self.lvlgen.wall_smooth_radius -= 1; self.world.level = self.lvlgen.generate(); } } diff --git a/src/core/level/lvlgen.rs b/src/core/level/lvlgen.rs index a549166..4a22807 100644 --- a/src/core/level/lvlgen.rs +++ b/src/core/level/lvlgen.rs @@ -6,19 +6,25 @@ use rand::Rng; ////////// LEVEL GENERATOR ///////////////////////////////////////////////////// -#[derive(Default)] +#[derive(Debug, Default)] pub struct LevelGenerator { pub seed: u32, pub iterations: u8, + pub wall_smooth_radius: u8, } impl LevelGenerator { - pub fn new(seed: u32, iterations: u8) -> Self{ - LevelGenerator { seed, iterations } + pub fn new(seed: u32) -> Self{ + LevelGenerator { + seed, + iterations: 5, + wall_smooth_radius: 2, + } } pub fn generate(&self) -> Level { - time_scope!("grid generation"); + println!("new level from {:?}", self); + time_scope!("level generation"); let cell_size = 20; let (width, height) = (2560 / cell_size, 1440 / cell_size); @@ -114,7 +120,7 @@ impl LevelGenerator { grid.cells = next; } } - println!("{} iterations needed", count); + println!(" {} iterations needed", count); } fn neighbours(&self, grid: &Vec>, px: usize, py: usize, distance: usize) -> u8 { @@ -154,7 +160,7 @@ impl LevelGenerator { } fn find_regions(&self, grid: &Grid) -> Vec { - time_scope!("finding all regions"); + time_scope!(" finding all regions"); let mut regions = vec!(); let mut marked = vec!(vec!(false; grid.height); grid.width); for x in 0..grid.width { @@ -198,14 +204,14 @@ impl LevelGenerator { 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); + 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); // delete all smaller wall regions for r in self.find_regions(grid).iter().filter(|r| r.value) { let percent = r.cells.len() as f64 / (grid.width * grid.height) as f64; if percent < min_wall_size { - // println!("delete wall region of size {}", r.cells.len()); + // println!(" delete wall region of size {}", r.cells.len()); self.delete_region(grid, r); } }