Changed project name (placeholder), some renames
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
use cgmath::Point3;
|
||||
use noise::{Perlin, NoiseFn};
|
||||
use rand::{Rng, prelude::ThreadRng};
|
||||
|
||||
use crate::game::world::{block, block::WorldBlock};
|
||||
|
||||
const SCALE: f64 = 150.0;
|
||||
|
||||
|
||||
pub struct WorldGen {
|
||||
|
||||
}
|
||||
|
||||
impl WorldGen {
|
||||
|
||||
/*
|
||||
* Generate height map for given block position
|
||||
*/
|
||||
fn gen_height_map(x: usize, z: usize, rand: &mut ThreadRng, noise: &Perlin) -> ([[usize;block::SIZE]; block::SIZE], usize) {
|
||||
let x = x * block::SIZE;
|
||||
let z = z * block::SIZE;
|
||||
|
||||
// Generate map
|
||||
let mut max: usize = 0;
|
||||
let mut map = [[0_usize; block::SIZE]; block::SIZE];
|
||||
for lx in 0..block::SIZE {
|
||||
for lz in 0..block::SIZE {
|
||||
let value = (noise.get([(x + lx) as f64 / SCALE, (z + lz) as f64 /SCALE]) + 1.0) / 2.0;
|
||||
let mut h = (value * 30.0) as usize;
|
||||
h += (rand.gen::<f32>() * 2.0) as usize;
|
||||
if h == 0 { h = 1; }
|
||||
if h > max { max = h; }
|
||||
map[lx][lz] = h;
|
||||
}
|
||||
}
|
||||
(map, max)
|
||||
}
|
||||
|
||||
/*
|
||||
* Generate block
|
||||
*/
|
||||
pub fn generate(block_pos: &Point3<u32>, block: &mut WorldBlock) {
|
||||
let mut rng = rand::thread_rng();
|
||||
let noise = Perlin::new();
|
||||
|
||||
// Generate height map
|
||||
let (map, max) = WorldGen::gen_height_map(block_pos.x as usize, block_pos.z as usize, &mut rng, &noise);
|
||||
|
||||
// Fill
|
||||
for x in 0..block::SIZE {
|
||||
for z in 0..block::SIZE {
|
||||
let h = map[x][z];
|
||||
//let h = 16; // testing
|
||||
for y in 0..h {
|
||||
block.set(Point3{x, y, z}, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user