summaryrefslogtreecommitdiff
path: root/src/sim/board.rs
diff options
context:
space:
mode:
authorKai Stevenson <kai@kaistevenson.com>2026-08-09 16:30:29 -0700
committerKai Stevenson <kai@kaistevenson.com>2026-08-09 16:30:29 -0700
commit167e31655b63aa4d4548532cf0410cea9fd145ca (patch)
tree2b9b3d3832404979fecfa3972464c37613d43db2 /src/sim/board.rs
parent2eff3a26da27f68355ee2316f956e1bdfb6f1b68 (diff)
drawing
Diffstat (limited to 'src/sim/board.rs')
-rw-r--r--src/sim/board.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/sim/board.rs b/src/sim/board.rs
new file mode 100644
index 0000000..4cc5756
--- /dev/null
+++ b/src/sim/board.rs
@@ -0,0 +1,72 @@
+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<Cell>,
+}
+
+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<usize> {
+ 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<Cell> = vec![Cell::empty(); (size_x * size_y) as usize];
+
+ Board {
+ size_x,
+ size_y,
+ cells,
+ }
+ }
+}