summaryrefslogtreecommitdiff
path: root/src/proc_gen/mod.rs
blob: 8c965fbfb9644a2b336ac82719395d13aac33016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use fastnoise_lite::FastNoiseLite;
use glam::{IVec2, Vec2};

use crate::{
    Config,
    config::CHUNK_SIZE,
    content::materials::MaterialId,
    sim::{cell::Cell, cell_manager::chunk::Chunk},
};

pub struct WorldGenerator {
    cave_noise: FastNoiseLite,
}

impl WorldGenerator {
    pub fn generate_chunk(&mut self, chunk_position: IVec2) -> Chunk {
        let mut c = Chunk::void();
        let position = chunk_position * CHUNK_SIZE;
        for x in 0..CHUNK_SIZE {
            for y in 0..CHUNK_SIZE {
                let cell_position = position.as_vec2() + Vec2::new(x as f32, y as f32);
                let cnv = self
                    .cave_noise
                    .get_noise_2d(cell_position.x, cell_position.y);
                if cnv < 0.5 {
                    c.set_cell_at_local_position(
                        x as u8,
                        y as u8,
                        Cell::from_material(MaterialId::Wood),
                    );
                }
            }
        }
        c
    }

    pub fn update_params(&mut self, config: &Config) {
        self.cave_noise = FastNoiseLite::with_seed(config.proc_gen_seed + 50);
        self.cave_noise.set_frequency(Some(config.cave_noise_freq));
    }

    pub fn new(config: &Config) -> Self {
        let cave_noise = FastNoiseLite::with_seed(config.proc_gen_seed + 50);
        let mut wg = WorldGenerator { cave_noise };
        wg.update_params(config);
        wg
    }
}