From 465b5c40d16ccf6c26ff1f340051da5fa70881e9 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 5 Oct 2024 19:45:25 +0200 Subject: [PATCH] group wires into one tool option --- src/main.rs | 57 ++++++++++++++++++++++---------------- src/marble_engine.rs | 54 ------------------------------------ src/marble_engine/board.rs | 19 +++++++++++++ src/marble_engine/tile.rs | 37 +++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 78 deletions(-) diff --git a/src/main.rs b/src/main.rs index f683a9b..48b4f93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,8 @@ use std::{fs::read_to_string, ops::Rem}; use marble_engine::{ board::Board, - parse, - tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile}, - tile_from_char, Machine, + tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType}, + Machine, }; use raylib::prelude::*; @@ -26,6 +25,7 @@ struct Game { tool_menu_gate: GateType, tool_menu_arrow: Direction, tool_menu_mirror: MirrorType, + tool_menu_wire: WireType, input_text_selected: bool, sim_speed: f32, time_since_step: f32, @@ -37,6 +37,7 @@ enum Tool { SetTile(Tile), Math, Gate, + Wire, Arrow, Mirror, Number, @@ -65,7 +66,7 @@ fn main() { textures.load_dir("assets/tiles", &mut rl, &thread); let mut game = Game::new_sandbox(); - let board = parse(&read_to_string("boards/adder.mbl").unwrap()); + let board = Board::parse(&read_to_string("boards/adder.mbl").unwrap()); game.load_board(board); while !rl.window_should_close() { @@ -94,6 +95,7 @@ impl Game { tool_menu_gate: GateType::Equal, tool_menu_arrow: Direction::Right, tool_menu_mirror: MirrorType::Forward, + tool_menu_wire: WireType::Vertical, } } @@ -138,6 +140,13 @@ impl Game { MirrorType::Back => MirrorType::Forward, } } + Tool::Wire => { + self.tool_menu_wire = match self.tool_menu_wire { + WireType::Vertical => WireType::Horizontal, + WireType::Horizontal => WireType::Cross, + WireType::Cross => WireType::Vertical, + } + } Tool::None => (), Tool::SetTile(_) => (), Tool::Number => (), @@ -272,45 +281,40 @@ impl Game { border, ); }; - tool_button((0, -1), "eraser", Tool::SetTile(tile_from_char(' '))); + tool_button((0, -1), "eraser", Tool::SetTile(Tile::from_char(' '))); tool_button((1, -1), "transparent", Tool::None); - tool_button((0, 0), "marble", Tool::SetTile(tile_from_char('o'))); - tool_button((0, 1), "block", Tool::SetTile(tile_from_char('#'))); - tool_button((0, 2), "bag_off", Tool::SetTile(tile_from_char('B'))); - tool_button((0, 3), "trigger_off", Tool::SetTile(tile_from_char('*'))); - tool_button((0, 4), "input_off", Tool::SetTile(tile_from_char('I'))); - tool_button((0, 5), "output_off", Tool::SetTile(tile_from_char('P'))); - tool_button((0, 6), "flipper_off", Tool::SetTile(tile_from_char('F'))); + tool_button((0, 0), "block", Tool::SetTile(Tile::from_char('#'))); + tool_button((0, 1), "bag_off", Tool::SetTile(Tile::from_char('B'))); + tool_button((0, 2), "trigger_off", Tool::SetTile(Tile::from_char('*'))); + tool_button((0, 3), "input_off", Tool::SetTile(Tile::from_char('I'))); + tool_button((0, 4), "output_off", Tool::SetTile(Tile::from_char('P'))); + tool_button((0, 5), "flipper_off", Tool::SetTile(Tile::from_char('F'))); - tool_button( - (1, 0), - "wire_horizontal_off", - Tool::SetTile(tile_from_char('-')), - ); + tool_button((1, 0), "marble", Tool::SetTile(Tile::from_char('o'))); tool_button( (1, 1), - "wire_vertical_off", - Tool::SetTile(tile_from_char('|')), + &Tile::Powerable(PTile::Wire(self.tool_menu_wire), false).texture(), + Tool::Wire, ); - tool_button((1, 2), "wire_cross_off", Tool::SetTile(tile_from_char('+'))); + tool_button( - (1, 3), + (1, 2), &Tile::Arrow(self.tool_menu_arrow).texture(), Tool::Arrow, ); tool_button( - (1, 4), + (1, 3), &Tile::Mirror(self.tool_menu_mirror).texture(), Tool::Mirror, ); tool_button( - (1, 5), + (1, 4), &Tile::Powerable(PTile::Math(self.tool_menu_math), false).texture(), Tool::Math, ); tool_button( - (1, 6), + (1, 5), &Tile::Powerable(PTile::Gate(self.tool_menu_gate), false).texture(), Tool::Gate, ); @@ -339,6 +343,7 @@ impl Game { } Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()), Tool::Gate => format!("{}_off", self.tool_menu_gate.texture_name()), + Tool::Wire => format!("{}_off", self.tool_menu_wire.texture_name()), Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(), Tool::Mirror => self.tool_menu_mirror.texture_name().into(), Tool::Number => todo!(), @@ -364,6 +369,10 @@ impl Game { tile_pos.into(), Tile::Powerable(PTile::Gate(self.tool_menu_gate), false), ), + Tool::Wire => self.source_board.set( + tile_pos.into(), + Tile::Powerable(PTile::Wire(self.tool_menu_wire), false), + ), Tool::Arrow => self .source_board .set(tile_pos.into(), Tile::Arrow(self.tool_menu_arrow)), diff --git a/src/marble_engine.rs b/src/marble_engine.rs index 29af792..4157771 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -315,57 +315,3 @@ impl Machine { } } -pub fn parse(source: &str) -> Board { - let mut rows = Vec::new(); - - let mut width = 0; - for line in source.lines() { - width = width.max(line.len()); - let mut tiles = Vec::new(); - for char in line.chars() { - tiles.push(tile_from_char(char)); - } - rows.push(tiles); - } - for line in &mut rows { - line.resize(width, Tile::Blank); - } - - Board::new(rows) -} - -pub const fn tile_from_char(c: char) -> Tile { - match c { - 'o' => Tile::Marble { - value: 0, - dir: Direction::Down, - }, - '*' => Tile::Powerable(PTile::Trigger, false), - '-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false), - '|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false), - '+' => Tile::Powerable(PTile::Wire(WireType::Cross), false), - '/' => Tile::Mirror(MirrorType::Forward), - '\\' => Tile::Mirror(MirrorType::Back), - '^' => Tile::Arrow(Direction::Up), - 'v' => Tile::Arrow(Direction::Down), - '<' => Tile::Arrow(Direction::Left), - '>' => Tile::Arrow(Direction::Right), - '=' => Tile::Powerable(PTile::Gate(GateType::Equal), false), - '!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false), - 'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false), - 'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false), - 'P' => Tile::Powerable(PTile::Output, false), - 'I' => Tile::Powerable(PTile::Input, false), - 'F' => Tile::Powerable(PTile::Flipper, false), - 'A' => Tile::Powerable(PTile::Math(MathOp::Add), false), - 'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false), - 'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false), - 'D' => Tile::Powerable(PTile::Math(MathOp::Div), false), - 'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false), - 'B' => Tile::Powerable(PTile::Bag, false), - d @ '0'..='9' => Tile::Digit(d as u8 - b'0'), - '#' => Tile::Block, - ' ' => Tile::Blank, - _ => Tile::Blank, - } -} diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index 7566725..08e4e5b 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -35,6 +35,25 @@ pub struct Board { } impl Board { + pub fn parse(source: &str) -> Self { + let mut rows = Vec::new(); + + let mut width = 0; + for line in source.lines() { + width = width.max(line.len()); + let mut tiles = Vec::new(); + for char in line.chars() { + tiles.push(Tile::from_char(char)); + } + rows.push(tiles); + } + for line in &mut rows { + line.resize(width, Tile::Blank); + } + + Board::new(rows) + } + pub fn new_empty(width: usize, height: usize) -> Self { let rows = vec![vec![Tile::Blank; width]; height]; Self { diff --git a/src/marble_engine/tile.rs b/src/marble_engine/tile.rs index a8abcd1..b379dc9 100644 --- a/src/marble_engine/tile.rs +++ b/src/marble_engine/tile.rs @@ -68,6 +68,42 @@ pub enum Direction { } impl Tile { + pub const fn from_char(c: char) -> Tile { + match c { + 'o' => Tile::Marble { + value: 0, + dir: Direction::Down, + }, + '*' => Tile::Powerable(PTile::Trigger, false), + '-' => Tile::Powerable(PTile::Wire(WireType::Horizontal), false), + '|' => Tile::Powerable(PTile::Wire(WireType::Vertical), false), + '+' => Tile::Powerable(PTile::Wire(WireType::Cross), false), + '/' => Tile::Mirror(MirrorType::Forward), + '\\' => Tile::Mirror(MirrorType::Back), + '^' => Tile::Arrow(Direction::Up), + 'v' => Tile::Arrow(Direction::Down), + '<' => Tile::Arrow(Direction::Left), + '>' => Tile::Arrow(Direction::Right), + '=' => Tile::Powerable(PTile::Gate(GateType::Equal), false), + '!' => Tile::Powerable(PTile::Gate(GateType::NotEqual), false), + 'L' => Tile::Powerable(PTile::Gate(GateType::LessThan), false), + 'G' => Tile::Powerable(PTile::Gate(GateType::GreaterThan), false), + 'P' => Tile::Powerable(PTile::Output, false), + 'I' => Tile::Powerable(PTile::Input, false), + 'F' => Tile::Powerable(PTile::Flipper, false), + 'A' => Tile::Powerable(PTile::Math(MathOp::Add), false), + 'S' => Tile::Powerable(PTile::Math(MathOp::Sub), false), + 'M' => Tile::Powerable(PTile::Math(MathOp::Mul), false), + 'D' => Tile::Powerable(PTile::Math(MathOp::Div), false), + 'R' => Tile::Powerable(PTile::Math(MathOp::Rem), false), + 'B' => Tile::Powerable(PTile::Bag, false), + d @ '0'..='9' => Tile::Digit(d as u8 - b'0'), + '#' => Tile::Block, + ' ' => Tile::Blank, + _ => Tile::Blank, + } + } + pub fn is_blank(&self) -> bool { matches!(self, Tile::Blank) } @@ -223,3 +259,4 @@ impl GateType { } } +