summaryrefslogtreecommitdiff
path: root/src/sim/cell_sim/chunk.rs
blob: c7d619ba16e719db00a6485bc6c9c0e277e85c74 (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
use crate::{
    config::{CELLS_IN_CHUNK, CHUNK_SIZE},
    sim::{
        cell::{cell::Cell, materials::MaterialId},
        lib::marching_squares::Marchable,
    },
};

pub struct Chunk {
    pub cells: Box<[Cell; CELLS_IN_CHUNK]>,
    pub sleeping: bool,
    pub needs_texture_update: bool,
}

impl Chunk {
    #[inline]
    pub fn get_cell_at_local_position(&self, x: u8, y: u8) -> Cell {
        self.cells[x as usize + y as usize * CHUNK_SIZE as usize]
    }
    #[inline]
    pub fn set_cell_at_local_position(&mut self, x: u8, y: u8, cell: Cell) {
        self.cells[x as usize + y as usize * CHUNK_SIZE as usize] = cell;
    }

    pub fn void() -> Self {
        Chunk {
            cells: Box::new([Cell::void(); CELLS_IN_CHUNK]),
            sleeping: true,
            needs_texture_update: true,
        }
    }
}

impl Marchable for Chunk {
    fn occupied(&self, x: i32, y: i32) -> bool {
        if x < 0 || x >= CHUNK_SIZE || y < 0 || y >= CHUNK_SIZE {
            false
        } else {
            self.get_cell_at_local_position(x as u8, y as u8).material != MaterialId::Void
        }
    }
    fn size(&self) -> (i32, i32) {
        (CHUNK_SIZE, CHUNK_SIZE)
    }
}