use common::Point2D; use core::render::Renderer; use rand::Rng; use sprites::SpriteManager; ////////// LEVEL /////////////////////////////////////////////////////////////// #[derive(Default)] pub struct Level { pub gravity: Point2D, pub ground: f64, // just to have something pub grid: Grid, iterations: u8, } impl Level { pub fn new(gravity: Point2D, ground: f64) -> Self { Level { gravity, ground, grid: Grid::generate(10), iterations: 10 } } pub fn regenerate(&mut self) { self.grid = Grid::generate(self.iterations); } pub fn increase_iteration(&mut self) { self.iterations += 1; self.regenerate(); println!("iterate {} time(s)", self.iterations); } pub fn decrease_iteration(&mut self) { self.iterations -= 1; self.regenerate(); println!("iterate {} time(s)", self.iterations); } pub fn render(&mut self, renderer: &mut Renderer, _sprites: &SpriteManager) { let w = renderer.viewport().0 as i32; renderer.canvas().set_draw_color((64, 64, 64)); let size = self.grid.cell_size; for x in 0..self.grid.width { for y in 0..self.grid.height { if self.grid.cells[x][y] { renderer.canvas().fill_rect(sdl2::rect::Rect::new(x as i32 * size as i32, y as i32 * size as i32, size as u32, size as u32)).unwrap(); } } } for i in 1..11 { let y = (i * i - 1) as i32 + self.ground as i32; renderer.canvas().set_draw_color((255 - i * 20, 255 - i * 20, 0)); renderer.canvas().draw_line((0, y), (w, y)).unwrap(); } } } ////////// GRID //////////////////////////////////////////////////////////////// #[derive(Default)] pub struct Grid { pub width: usize, pub height: usize, pub cell_size: usize, pub cells: Vec>, } impl Grid { fn generate(iterations: u8) -> Grid { let cell_size = 20; let (width, height) = (2560 / cell_size, 1440 / cell_size); let mut grid = Grid { cell_size, width, height, cells: vec!(vec!(true; height); width), }; // start with some noise // grid.simplex_noise(); grid.random_noise(); // smooth with cellular automata grid.smooth(iterations); // grid.smooth_until_equilibrium(); // increase resolution for _i in 0..1 { grid = grid.subdivide(); grid.smooth(iterations); } grid } #[allow(dead_code)] fn simplex_noise(&mut self) { use noise::{NoiseFn, OpenSimplex, Seedable}; let noise = OpenSimplex::new().set_seed(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() as u32); self.set_each(|x, y| noise.get([x as f64 / 12.0, y as f64 / 12.0]) > 0.055, 1); } #[allow(dead_code)] fn random_noise(&mut self) { let mut rng = rand::thread_rng(); self.set_each(|_x, _y| rng.gen_range(0, 100) > 55, 1); } #[allow(dead_code)] fn smooth(&mut self, iterations: u8) { let distance = 1; for _i in 0..iterations { let mut next = vec!(vec!(true; self.height); self.width); for x in distance..(self.width - distance) { for y in distance..(self.height - distance) { match Grid::neighbours(&self.cells, x, y, distance) { n if n < 4 => next[x][y] = false, n if n > 4 => next[x][y] = true, _ => next[x][y] = self.cells[x][y] } } } if self.cells == next { break; // exit early } else { self.cells = next; } } } #[allow(dead_code)] fn smooth_until_equilibrium(&mut self) { let distance = 1; let mut count = 0; loop { count += 1; let mut next = vec!(vec!(true; self.height); self.width); for x in distance..(self.width - distance) { for y in distance..(self.height - distance) { match Grid::neighbours(&self.cells, x, y, distance) { n if n < 4 => next[x][y] = false, n if n > 4 => next[x][y] = true, _ => next[x][y] = self.cells[x][y] }; } } if self.cells == next { break; } else { self.cells = next; } } println!("{} iterations needed", count); } fn neighbours(grid: &Vec>, px: usize, py: usize, distance: usize) -> u8 { let mut count = 0; for x in (px - distance)..=(px + distance) { for y in (py - distance)..=(py + distance) { if !(x == px && y == py) && grid[x][y] { count += 1; } } } count } fn set_each bool>(&mut self, mut func: F, walls: usize) { for x in walls..(self.width - walls) { for y in walls..(self.height - walls) { self.cells[x][y] = func(x, y); } } } fn subdivide(&mut self) -> Grid { let (width, height) = (self.width * 2, self.height * 2); let mut cells = vec!(vec!(true; height); width); for x in 1..(width - 1) { for y in 1..(height - 1) { cells[x][y] = self.cells[x / 2][y / 2]; } } Grid { cell_size: self.cell_size / 2, width, height, cells } } }