use glam::IVec2; use crate::{ config::{CELLS_IN_CHUNK, CHUNK_SIZE}, content::materials::{MaterialForm, MaterialId}, sim::{cell::Cell, 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, } } pub fn floor() -> Self { let mut c = Chunk { cells: Box::new([Cell::void(); CELLS_IN_CHUNK]), sleeping: true, needs_texture_update: true, }; for dy in 1..5 { for x in 0..CHUNK_SIZE { c.set_cell_at_local_position( x as u8, CHUNK_SIZE as u8 - dy, Cell::from_material(MaterialId::Steel), ); } } c } } impl Marchable for Chunk { fn occupied(&self, pos: IVec2) -> bool { if pos.x < 0 || pos.x >= CHUNK_SIZE || pos.y < 0 || pos.y >= CHUNK_SIZE { false } else { // we only build a path for solid cells or settled powder cells let cell = self.get_cell_at_local_position(pos.x as u8, pos.y as u8); let material = cell.material.def(); material.form == MaterialForm::Solid || material.form == MaterialForm::Powder && cell.settled() > 4 } } fn size(&self) -> IVec2 { IVec2::new(CHUNK_SIZE, CHUNK_SIZE) } }