veloren/world/src/lib.rs

59 lines
1.3 KiB
Rust
Raw Normal View History

2019-01-15 15:13:11 +00:00
// Library
use vek::*;
2019-01-23 20:01:58 +00:00
use noise::{NoiseFn, Perlin};
2019-01-15 15:13:11 +00:00
// Project
use common::{
2019-01-23 20:01:58 +00:00
vol::{Vox, SizedVol, WriteVol},
2019-01-15 15:13:11 +00:00
terrain::{
Block,
TerrainChunk,
TerrainChunkMeta,
},
};
#[derive(Debug)]
pub enum Error {
Other(String),
}
pub struct World;
impl World {
pub fn new() -> Self {
Self
}
2019-01-23 20:01:58 +00:00
pub fn generate_chunk(&self, chunk_pos: Vec3<i32>) -> TerrainChunk {
let mut chunk = TerrainChunk::filled(Block::empty(), TerrainChunkMeta::void());
let air = Block::empty();
let stone = Block::new(1, Rgb::new(200, 220, 255));
let grass = Block::new(2, Rgb::new(50, 255, 0));
let perlin_nz = Perlin::new();
for lpos in chunk.iter_positions() {
let wpos = lpos + chunk_pos * chunk.get_size().map(|e| e as i32);
let wposf = wpos.map(|e| e as f64);
let freq = 1.0 / 32.0;
let ampl = 16.0;
let offs = 16.0;
let height = perlin_nz.get(Vec2::from(wposf * freq).into_array()) * ampl + offs;
chunk.set(lpos, if wposf.z < height {
if wposf.z < height - 1.0 {
stone
} else {
grass
}
} else {
air
});
}
chunk
2019-01-15 15:13:11 +00:00
}
}