use crate::{game::chunk::Chunk, vec3, vector::Vector3};

use super::{Mesh, block_mesher::mesh_block};

const NEIGHBOURS: [Vector3<i32>; 6] = [
    vec3!(0, 1, 0),
    vec3!(0, -1, 0),
    vec3!(1, 0, 0),
    vec3!(-1, 0, 0),
    vec3!(0, 0, 1),
    vec3!(0, 0, -1),
];

impl Chunk {
    pub fn mesh(&self, mesh: &mut Mesh) {
        for (block, b_pos) in self.blocks().filter(|(b, _)| !b.is_transparent()) {
            let mut sides = [false; 6];
            sides
                .iter_mut()
                .zip(NEIGHBOURS.iter().copied())
                .for_each(|(f, n)| {
                    *f = self
                        .get_block(b_pos + n)
                        .map(|b| b.is_transparent())
                        .unwrap_or(true)
                });
            mesh_block(mesh, block, b_pos + (self.chunk_coord * 16), sides);
        }
    }
}
