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
56
57
58
59
60
61
62
63
64
65
use nalgebra::Point3;

// TODO: Get inspiration on multiarray crate (https://github.com/sellibitze/multiarray) to make chunk 2d and 3d friendly

#[derive(Debug, Clone, Builder, Getters, Setters)]
pub struct Chunk {
    #[get = "pub"] #[set = "pub"]
    position: Point3<f64>,
    #[get = "pub"] #[set = "pub"]
    width: usize,
    #[get = "pub"] #[set = "pub"]
    height: usize,
    #[get = "pub"] #[set = "pub"]
    depth: usize,
    #[get = "pub"] #[set = "pub"]
    values: Vec<f32>,
}

impl Chunk {
    pub fn new(position: [f64; 3], width: usize, height: usize, depth: usize) -> Self {
        Chunk {
            position: Point3::new(position[0], position[1], position[2]),
            width,
            height,
            depth,
            values: vec![0.0; depth * height * width],
        }
    }

    pub fn clone(&self) -> Self {
        Chunk {
            position: self.position.clone(),
            width: self.width,
            height: self.height,
            depth: self.depth,
            values: self.values.clone(),
        }
    }

    pub fn is_air(&self, x: usize, y: usize, z: usize) -> bool {
        if x >= self.width || y >= self.height || z >= self.depth {
            true
        } else {
            self.values[self.index(x, y, z)] == 0.0
        }
    }

    pub fn get(&self, x: usize, y: usize, z: usize) -> f32 {
        self.values[self.index(x, y, z)]
    }

    pub fn set(&mut self, x: usize, y: usize, z: usize, value: f32) {
        let index = self.index(x, y, z);
        self.values[index] = value;
    }

    fn index(&self, x: usize, y: usize, z: usize) -> usize {
        x + y * self.width + z * self.width * self.height
    }

    // TODO: This will add  the neighbor data at the border of the chunk, so we can calculate correctly  the normals, heights, etc without need to worry to query each time to get that data
    pub fn update_neighbor_data(&self, neighbor: &Chunk) {
        unimplemented!();
    }
}