Made wall smoothness adjustable
authorTomas Wenström <tomas.wenstrom@gmail.com>
Sat, 30 Jan 2021 17:01:03 +0000 (18:01 +0100)
committerTomas Wenström <tomas.wenstrom@gmail.com>
Sat, 30 Jan 2021 17:01:03 +0000 (18:01 +0100)
src/core/game.rs
src/core/level/lvlgen.rs

index 3dacac1..3e424fd 100644 (file)
@@ -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();
                }
            }
index a549166..4a22807 100644 (file)
@@ -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<Vec<bool>>, px: usize, py: usize, distance: usize) -> u8 {
@@ -154,7 +160,7 @@ impl LevelGenerator {
     }
 
     fn find_regions(&self, grid: &Grid<bool>) -> Vec<Region> {
-       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<bool>) {
        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);
            }
        }