fix marbles not working in negative quadrants

This commit is contained in:
Crispy 2024-10-07 17:57:07 +02:00
parent 70d7256e9d
commit 4c976ab8e2
3 changed files with 46 additions and 36 deletions

View file

@ -6,5 +6,6 @@
"is_sandbox": true, "is_sandbox": true,
"init_board": null, "init_board": null,
"inputs": [], "inputs": [],
"outputs": [] "outputs": [],
"input_is_text": true
} }

View file

@ -35,14 +35,7 @@ impl Machine {
} }
pub fn set_board(&mut self, board: Board) { pub fn set_board(&mut self, board: Board) {
self.marbles.clear(); self.marbles = board.get_marbles();
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.board = board; self.board = board;
} }
@ -73,33 +66,33 @@ impl Machine {
pub fn draw_marble_values(&self, d: &mut RaylibDrawHandle, offset: Vector2, zoom: i32) { pub fn draw_marble_values(&self, d: &mut RaylibDrawHandle, offset: Vector2, zoom: i32) {
let tile_size = 16 << zoom; let tile_size = 16 << zoom;
for x in 0..self.board.width() { for marble in &self.marbles {
for y in 0..self.board.height() { let x = marble.x;
if let Some(tile) = self.board.get((x, y).into()) { let y = marble.y;
let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2; if let Some(tile) = self.board.get(*marble) {
let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2; let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2;
if let Tile::Marble { value, dir } = tile { let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2;
let fontsize = (zoom + 1) * 10; if let Tile::Marble { value, dir } = tile {
d.draw_text( let fontsize = (zoom + 1) * 10;
&format!("{value}"), d.draw_text(
px - tile_size / 2 + 2, &format!("{value}"),
py - tile_size / 2 + 2, px - tile_size / 2 + 2,
fontsize, py - tile_size / 2 + 2,
Color::MAGENTA, fontsize,
); Color::MAGENTA,
d.draw_text( );
match dir { d.draw_text(
Direction::Up => "^", match dir {
Direction::Down => "v", Direction::Up => "^",
Direction::Left => "<", Direction::Down => "v",
Direction::Right => ">", Direction::Left => "<",
}, Direction::Right => ">",
px - tile_size / 2 + 2, },
py - tile_size / 2 + fontsize, px - tile_size / 2 + 2,
fontsize, py - tile_size / 2 + fontsize,
Color::MAGENTA, fontsize,
); Color::MAGENTA,
} );
} }
} }
} }

View file

@ -283,6 +283,22 @@ impl Board {
self.height self.height
} }
pub fn get_marbles(&self) -> Vec<Pos> {
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) { pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) {
let tile_size = 16 << zoom; let tile_size = 16 << zoom;