use crate::config::{PIXEL_BUFFER_HEIGHT, PIXEL_BUFFER_WIDTH}; type MaterialId = u16; #[derive(Clone, Copy)] pub struct Cell { pub material: MaterialId, pub velocity_x: i8, pub velocity_y: i8, pub flags: u8, } impl Cell { fn empty() -> Cell { Cell { material: 0, velocity_x: 0, velocity_y: 0, flags: 0, } } } pub struct Board { pub size_x: u32, pub size_y: u32, pub cells: Vec, } impl Board { pub fn index_to_position(&self, idx: usize) -> (i32, i32) { let y = idx / self.size_x as usize; let x = idx % self.size_x as usize; let board_x = x as i32 - self.size_x as i32 / 2; let board_y = y as i32 - self.size_x as i32 / 2; return (board_x, board_y); } pub fn set_cell_at_position(&mut self, x: i32, y: i32, c: Cell) { // TODO: option? let idx = self.position_to_index(x, y).unwrap(); self.cells[idx] = c; } pub fn cell_at_position(&self, x: i32, y: i32) -> Option<&Cell> { Some(&self.cells[self.position_to_index(x, y)?]) } pub fn position_to_index(&self, x: i32, y: i32) -> Option { let board_x = x + self.size_x as i32 / 2; let board_y = y + self.size_y as i32 / 2; let on_board = board_x >= 0 && board_x < self.size_x as i32 && board_y >= 0 && board_y < self.size_y as i32; if !on_board { return None; } return Some((board_y * self.size_x as i32 + board_x) as usize); } pub fn empty() -> Board { let size_x = PIXEL_BUFFER_WIDTH * 2; let size_y = PIXEL_BUFFER_HEIGHT * 2; let cells: Vec = vec![Cell::empty(); (size_x * size_y) as usize]; Board { size_x, size_y, cells, } } }