From 4c976ab8e29807d301faf45e8aa2f1cbd8fe9239 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 7 Oct 2024 17:57:07 +0200 Subject: [PATCH] fix marbles not working in negative quadrants --- levels/sandbox.json | 3 +- src/marble_engine.rs | 63 +++++++++++++++++--------------------- src/marble_engine/board.rs | 16 ++++++++++ 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/levels/sandbox.json b/levels/sandbox.json index fa7bad4..b32b787 100644 --- a/levels/sandbox.json +++ b/levels/sandbox.json @@ -6,5 +6,6 @@ "is_sandbox": true, "init_board": null, "inputs": [], - "outputs": [] + "outputs": [], + "input_is_text": true } \ No newline at end of file diff --git a/src/marble_engine.rs b/src/marble_engine.rs index 9f7c2fd..2252505 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -35,14 +35,7 @@ impl Machine { } pub fn set_board(&mut self, board: Board) { - self.marbles.clear(); - for y in 0..board.height() { - for x in 0..board.width() { - if let Some(Tile::Marble { value: _, dir: _ }) = board.get((x, y).into()) { - self.marbles.push((x, y).into()); - } - } - } + self.marbles = board.get_marbles(); self.board = board; } @@ -73,33 +66,33 @@ impl Machine { pub fn draw_marble_values(&self, d: &mut RaylibDrawHandle, offset: Vector2, zoom: i32) { let tile_size = 16 << zoom; - for x in 0..self.board.width() { - for y in 0..self.board.height() { - if let Some(tile) = self.board.get((x, y).into()) { - let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2; - let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2; - if let Tile::Marble { value, dir } = tile { - let fontsize = (zoom + 1) * 10; - d.draw_text( - &format!("{value}"), - px - tile_size / 2 + 2, - py - tile_size / 2 + 2, - fontsize, - Color::MAGENTA, - ); - d.draw_text( - match dir { - Direction::Up => "^", - Direction::Down => "v", - Direction::Left => "<", - Direction::Right => ">", - }, - px - tile_size / 2 + 2, - py - tile_size / 2 + fontsize, - fontsize, - Color::MAGENTA, - ); - } + for marble in &self.marbles { + let x = marble.x; + let y = marble.y; + if let Some(tile) = self.board.get(*marble) { + let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2; + let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2; + if let Tile::Marble { value, dir } = tile { + let fontsize = (zoom + 1) * 10; + d.draw_text( + &format!("{value}"), + px - tile_size / 2 + 2, + py - tile_size / 2 + 2, + fontsize, + Color::MAGENTA, + ); + d.draw_text( + match dir { + Direction::Up => "^", + Direction::Down => "v", + Direction::Left => "<", + Direction::Right => ">", + }, + px - tile_size / 2 + 2, + py - tile_size / 2 + fontsize, + fontsize, + Color::MAGENTA, + ); } } } diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index d86786e..0487607 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -283,6 +283,22 @@ impl Board { self.height } + pub fn get_marbles(&self) -> Vec { + let mut out = Vec::new(); + for y in 0..self.height { + for x in 0..self.width { + if let Tile::Marble { value: _, dir: _ } = self.rows[y][x] { + out.push(Pos { + x: x as isize - self.offset_x, + y: y as isize - self.offset_y, + }); + } + } + } + dbg!(&out); + out + } + pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) { let tile_size = 16 << zoom;