refactor, add simulation controls and board resetting

This commit is contained in:
Crispy 2024-10-04 22:10:00 +02:00
parent a9eefcd836
commit 880993762e
4 changed files with 193 additions and 83 deletions

View file

@ -2,8 +2,8 @@ use std::collections::HashMap;
use raylib::prelude::*;
mod board;
mod tile;
pub mod board;
pub mod tile;
use board::{Board, Pos};
use tile::*;
@ -30,6 +30,28 @@ impl Machine {
}
}
pub fn reset(&mut self) {
self.steps = 0;
self.input_index = 0;
self.output.clear();
}
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.board = board;
}
pub fn board(&self) -> &Board {
&self.board
}
pub fn output(&self) -> &[u8] {
&self.output
}
@ -46,40 +68,22 @@ impl Machine {
self.input = bytes;
}
pub fn new(grid: Board, input: Vec<u8>) -> Self {
// let (grid, marbles) = parse(source);
let mut marbles = Vec::new();
for y in 0..grid.height() {
for x in 0..grid.width() {
if let Some(Tile::Marble { value: _, dir: _ }) = grid.get((x, y).into()) {
marbles.push((x, y).into());
}
}
}
Self {
board: grid,
marbles,
input,
input_index: 0,
output: Vec::new(),
steps: 0,
}
}
pub fn draw(
&self,
d: &mut RaylibDrawHandle,
textures: &HashMap<String, Texture2D>,
offset: Vector2,
zoom: i32,
) {
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;
tile.draw(d, textures, px, py, tile_size, zoom);
if let Tile::Marble { value, dir } = tile{
d.draw_text(
&format!("{value}"),
px - tile_size / 2 + 2,
py - tile_size / 2 + 2,
20,
Color::MAGENTA,
)
}
}
}
}