impl GameState {
pub fn new() -> Self {
- let lvlgen = LevelGenerator::new(0, 5);
+ let lvlgen = LevelGenerator::new(0);
GameState {
world: World::new(lvlgen.generate()),
lvlgen,
}
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();
}
}
////////// 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);
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 {
}
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 {
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);
}
}