terrain an world

This commit is contained in:
Niko Abeler 2022-12-30 21:13:35 +01:00
parent d19dbf96fd
commit 44e6a99843
2 changed files with 72 additions and 22 deletions

View File

@ -1,3 +1,5 @@
mod world;
use noise::{Perlin, ScalePoint, Add, NoiseFn, Multiply, ScaleBias}; use noise::{Perlin, ScalePoint, Add, NoiseFn, Multiply, ScaleBias};
use noise::utils::{NoiseMapBuilder, PlaneMapBuilder}; use noise::utils::{NoiseMapBuilder, PlaneMapBuilder};
use image::{RgbImage, Rgb}; 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 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 x in 0..map_size {
for y in 0..map_size { for y in 0..map_size {
let h = plane.get_value(x, y); let h = plane.get_value(x, y);
if h < -2000.0 { let t = world::Terrain::from_height(h);
// deep ocean world.map[x][y] = t;
img.put_pixel(x as u32, y as u32, Rgb([20, 60, 255])); img.put_pixel(x as u32, y as u32, Rgb(t.to_color()));
} 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]));
}
} }
} }
img.save("world.png").unwrap(); img.save("world.png").unwrap();

65
src/world.rs Normal file
View File

@ -0,0 +1,65 @@
#[derive(Debug, Clone, Copy)]
pub enum Terrain {
DeepOcean,
Ocean,
Beach,
Flats,
Hills,
Mountains,
HighMountains,
}
pub struct World {
pub map: Vec<Vec<Terrain>>,
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],
}
}
}