WIP towns

This commit is contained in:
Niko Abeler 2022-12-30 21:30:12 +01:00
parent 44e6a99843
commit f64a757ec6
3 changed files with 70 additions and 6 deletions

View File

@ -1,4 +1,5 @@
mod world; mod world;
mod state;
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};
@ -37,6 +38,8 @@ impl NoiseFn<f64, 2> for BorderNoise {
fn main() { fn main() {
let N_TOWNS = 10;
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
let map_size = 256; let map_size = 256;
@ -80,11 +83,31 @@ fn main() {
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);
let t = world::Terrain::from_height(h); let t = world::Terrain::from_height(h);
world.map[x][y] = t; world.map[x][y] = world::WorldCell::new(t);
img.put_pixel(x as u32, y as u32, Rgb(t.to_color())); img.put_pixel(x as u32, y as u32, Rgb(t.to_color()));
} }
} }
img.save("world.png").unwrap();
let mut placed = 0;
while placed < N_TOWNS {
let x = rng.gen_range(0..map_size);
let y = rng.gen_range(0..map_size);
if
world.map[x][y].terrain == world::Terrain::Flats ||
world.map[x][y].terrain == world::Terrain::Hills
{
world.add_structure(x, y, world::Structure::Town(world::Town::new()));
placed += 1;
img.put_pixel(x as u32, y as u32, Rgb([255, 0, 0]));
}
}
let scaled = image::imageops::resize(
&img,
map_size as u32 * 4, map_size as u32 * 4,
image::imageops::FilterType::Nearest);
scaled.save("world.png").unwrap();

4
src/state.rs Normal file
View File

@ -0,0 +1,4 @@
use crate::world::World;
pub struct GameState {
pub world: World,
}

View File

@ -1,6 +1,6 @@
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy)]
pub enum Terrain { pub enum Terrain {
Void,
DeepOcean, DeepOcean,
Ocean, Ocean,
Beach, Beach,
@ -10,18 +10,41 @@ pub enum Terrain {
HighMountains, HighMountains,
} }
pub struct Town {
pub name: String,
pub population: u32,
}
pub enum Structure {
Town(Town),
}
pub struct WorldCell {
pub terrain: Terrain,
pub structure: Option<Structure>,
}
pub struct World { pub struct World {
pub map: Vec<Vec<Terrain>>, pub map: Vec<Vec<WorldCell>>,
pub size: usize, pub size: usize,
} }
impl WorldCell {
pub fn new(terrain: Terrain) -> WorldCell {
WorldCell {
terrain: terrain,
structure: None,
}
}
}
impl World { impl World {
pub fn new(size: usize) -> World { pub fn new(size: usize) -> World {
let mut map = Vec::new(); let mut map = Vec::new();
for _ in 0..size { for _ in 0..size {
let mut row = Vec::new(); let mut row = Vec::new();
for _ in 0..size { for _ in 0..size {
row.push(Terrain::DeepOcean); row.push(WorldCell::new(Terrain::Void));
} }
map.push(row); map.push(row);
} }
@ -30,6 +53,10 @@ impl World {
size: size, size: size,
} }
} }
pub fn add_structure(&mut self, x: usize, y: usize, structure: Structure) {
self.map[x][y].structure = Some(structure);
}
} }
impl Terrain { impl Terrain {
@ -53,6 +80,7 @@ impl Terrain {
pub fn to_color(self) -> [u8; 3] { pub fn to_color(self) -> [u8; 3] {
match self { match self {
Terrain::Void => [0, 0, 0],
Terrain::DeepOcean => [20, 60, 255], Terrain::DeepOcean => [20, 60, 255],
Terrain::Ocean => [20, 120, 255], Terrain::Ocean => [20, 120, 255],
Terrain::Beach => [255, 255, 100], Terrain::Beach => [255, 255, 100],
@ -63,3 +91,12 @@ impl Terrain {
} }
} }
} }
impl Town {
pub fn new() -> Town {
Town {
name: "Town".to_string(),
population: 100,
}
}
}