diff --git a/src/main.rs b/src/main.rs index 13a6d94..f31e381 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod world; + use noise::{Perlin, ScalePoint, Add, NoiseFn, Multiply, ScaleBias}; use noise::utils::{NoiseMapBuilder, PlaneMapBuilder}; use image::{RgbImage, Rgb}; @@ -72,31 +74,14 @@ fn main() { let mut img = RgbImage::new(map_size as u32, map_size as u32); + let mut world = world::World::new(map_size); + for x in 0..map_size { for y in 0..map_size { let h = plane.get_value(x, y); - if h < -2000.0 { - // deep ocean - img.put_pixel(x as u32, y as u32, Rgb([20, 60, 255])); - } else if h < 0.0 { - // ocean - img.put_pixel(x as u32, y as u32, Rgb([20, 120, 255])); - } else if h < 10.0 { - // beach - img.put_pixel(x as u32, y as u32, Rgb([255, 255, 100])); - } else if h < 1000.0 { - // grass - img.put_pixel(x as u32, y as u32, Rgb([0, 204, 0])); - } else if h < 2000.0 { - // hills - img.put_pixel(x as u32, y as u32, Rgb([102, 153, 0])); - } else if h < 3500.0 { - // mountains - img.put_pixel(x as u32, y as u32, Rgb([153, 153, 102])); - } else { - // snow - img.put_pixel(x as u32, y as u32, Rgb([230, 230, 230])); - } + let t = world::Terrain::from_height(h); + world.map[x][y] = t; + img.put_pixel(x as u32, y as u32, Rgb(t.to_color())); } } img.save("world.png").unwrap(); diff --git a/src/world.rs b/src/world.rs new file mode 100644 index 0000000..e1a5c85 --- /dev/null +++ b/src/world.rs @@ -0,0 +1,65 @@ + +#[derive(Debug, Clone, Copy)] +pub enum Terrain { + DeepOcean, + Ocean, + Beach, + Flats, + Hills, + Mountains, + HighMountains, +} + +pub struct World { + pub map: Vec>, + pub size: usize, +} + +impl World { + pub fn new(size: usize) -> World { + let mut map = Vec::new(); + for _ in 0..size { + let mut row = Vec::new(); + for _ in 0..size { + row.push(Terrain::DeepOcean); + } + map.push(row); + } + World { + map: map, + size: size, + } + } +} + +impl Terrain { + pub fn from_height(height: f64) -> Terrain { + if height < -2000.0 { + Terrain::DeepOcean + } else if height < 0.0 { + Terrain::Ocean + } else if height < 50.0 { + Terrain::Beach + } else if height < 1000.0 { + Terrain::Flats + } else if height < 2000.0 { + Terrain::Hills + } else if height < 3500.0 { + Terrain::Mountains + } else { + Terrain::HighMountains + } + } + + pub fn to_color(self) -> [u8; 3] { + match self { + Terrain::DeepOcean => [20, 60, 255], + Terrain::Ocean => [20, 120, 255], + Terrain::Beach => [255, 255, 100], + Terrain::Flats => [0, 204, 0], + Terrain::Hills => [102, 153, 0], + Terrain::Mountains => [153, 153, 102], + Terrain::HighMountains => [230, 230, 230], + } + } +}