summaryrefslogtreecommitdiff
path: root/src/content/world/mines.rs
blob: d750619e9afb7d99029dd1ef98523e1c03997e3e (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
49
50
51
52
53
54
55
use fastnoise_lite::{FastNoiseLite, FractalType};
use glam::IVec2;

use crate::{
    content::materials::MaterialId,
    proc_gen::{Biome, InterpolatedPixel},
    sim::cell::Cell,
};

const ROUGHNESS: f32 = 0.375;

pub struct MinesBiome {
    pub edge_roughness: FastNoiseLite,
}

impl MinesBiome {
    pub fn new() -> Self {
        let mut edge_roughness = FastNoiseLite::new();
        edge_roughness.set_fractal_type(Some(FractalType::Ridged));
        edge_roughness.set_fractal_octaves(Some(2));
        MinesBiome { edge_roughness }
    }
}

impl Biome for MinesBiome {
    fn fragment(&self, pixel: InterpolatedPixel, world: IVec2) -> Cell {
        let world = world.as_vec2();
        let displacement = ROUGHNESS * self.edge_roughness.get_noise_2d(world.x, world.y);

        match pixel.interpolated_pixel_index {
            0 => {
                if pixel.interpolated_solidity + displacement <= 0.0 {
                    return Cell::void();
                }

                Cell::from_material(MaterialId::Dirt)
            }
            12 => {
                if pixel.interpolated_solidity + displacement <= 0.0 {
                    return Cell::void();
                }

                Cell::from_material(MaterialId::Water)
            }
            13 => {
                if pixel.interpolated_solidity + displacement <= 0.0 {
                    return Cell::void();
                }

                Cell::from_material(MaterialId::Wood)
            }
            _ => Cell::void(),
        }
    }
}