diff --git a/assets/tiles/digit_0.png b/assets/tiles/digit_0.png new file mode 100644 index 0000000..eed0adb Binary files /dev/null and b/assets/tiles/digit_0.png differ diff --git a/assets/tiles/digit_1.png b/assets/tiles/digit_1.png new file mode 100644 index 0000000..2390096 Binary files /dev/null and b/assets/tiles/digit_1.png differ diff --git a/assets/tiles/digit_2.png b/assets/tiles/digit_2.png new file mode 100644 index 0000000..d0fb664 Binary files /dev/null and b/assets/tiles/digit_2.png differ diff --git a/assets/tiles/digit_3.png b/assets/tiles/digit_3.png new file mode 100644 index 0000000..f0e26f2 Binary files /dev/null and b/assets/tiles/digit_3.png differ diff --git a/assets/tiles/digit_4.png b/assets/tiles/digit_4.png new file mode 100644 index 0000000..765bb92 Binary files /dev/null and b/assets/tiles/digit_4.png differ diff --git a/assets/tiles/digit_5.png b/assets/tiles/digit_5.png new file mode 100644 index 0000000..faf1ee8 Binary files /dev/null and b/assets/tiles/digit_5.png differ diff --git a/assets/tiles/digit_6.png b/assets/tiles/digit_6.png new file mode 100644 index 0000000..06c690d Binary files /dev/null and b/assets/tiles/digit_6.png differ diff --git a/assets/tiles/digit_7.png b/assets/tiles/digit_7.png new file mode 100644 index 0000000..dc72d18 Binary files /dev/null and b/assets/tiles/digit_7.png differ diff --git a/assets/tiles/digit_8.png b/assets/tiles/digit_8.png new file mode 100644 index 0000000..4f18e60 Binary files /dev/null and b/assets/tiles/digit_8.png differ diff --git a/assets/tiles/digit_9.png b/assets/tiles/digit_9.png new file mode 100644 index 0000000..7ba2933 Binary files /dev/null and b/assets/tiles/digit_9.png differ diff --git a/src/main.rs b/src/main.rs index 74f3eca..9520b2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use std::{ ops::Rem, }; -use marble_engine::{board::Board, parse, tile::Tile, Machine}; +use marble_engine::{board::Board, parse, tile::Tile, tile_to_char, Machine}; use raylib::prelude::*; mod marble_engine; @@ -26,7 +26,7 @@ struct Game { time_since_step: f32, } -#[derive(Debug)] +#[derive(Debug, Clone, PartialEq)] enum Tool { None, SetTile(Tile), @@ -51,11 +51,11 @@ fn main() { let mut textures: HashMap = HashMap::new(); for d in read_dir("assets/tiles").unwrap().flatten() { - let name = d.file_name(); - if d.path().is_file() { - let name = name.to_string_lossy(); + let path = d.path(); + if path.is_file() { + let name = path.file_stem().unwrap().to_string_lossy(); let texture = rl - .load_texture(&thread, &format!("assets/tiles/{name}")) + .load_texture(&thread, &format!("assets/tiles/{name}.png")) .unwrap(); textures.insert(name.to_string(), texture); } @@ -203,11 +203,12 @@ impl Game { let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string(); if text_input( d, - Rectangle::new(350., footer_top + 60., 200., 25.), + Rectangle::new(5., footer_top + 60., 200., 25.), &mut input_text, &mut self.input_text_selected, ) { self.machine.set_input(input_text.into_bytes()); } + } } diff --git a/src/marble_engine.rs b/src/marble_engine.rs index 1952294..87a5706 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -327,57 +327,8 @@ pub fn parse(source: &str) -> Board { for line in source.lines() { width = width.max(line.len()); let mut tiles = Vec::new(); - let mut in_comment = false; for char in line.chars() { - if in_comment { - if char == ')' { - in_comment = false; - } - if char == ' ' { - // allow marbles to pass through gaps in comments - tiles.push(Tile::Blank); - } else { - tiles.push(Tile::Comment(char as u8)); - } - continue; - } - tiles.push(match char { - '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), - '#' => Tile::Block, - ' ' => Tile::Blank, - '(' => { - in_comment = true; - Tile::Comment(b'(') - } - _ => Tile::Blank, - }); + tiles.push(tile_to_char(char)); } rows.push(tiles); } @@ -387,3 +338,39 @@ pub fn parse(source: &str) -> Board { Board::new(rows) } + +pub const fn tile_to_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), + '#' => Tile::Block, + ' ' => Tile::Blank, + _ => Tile::Blank, + } +} diff --git a/src/marble_engine/tile.rs b/src/marble_engine/tile.rs index 1fd4550..a037786 100644 --- a/src/marble_engine/tile.rs +++ b/src/marble_engine/tile.rs @@ -6,12 +6,11 @@ use super::board::Pos; pub type MarbleValue = u32; -#[derive(Debug, Default, Clone, Copy)] +#[derive(Debug, Default, Clone, Copy, PartialEq)] pub enum Tile { #[default] Blank, Block, - Comment(u8), Marble { value: MarbleValue, dir: Direction, @@ -22,7 +21,7 @@ pub enum Tile { Powerable(PTile, bool), } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum PTile { Trigger, Wire(WireType), @@ -34,13 +33,13 @@ pub enum PTile { Output, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum MirrorType { Forward, Back, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum MathOp { Add, Sub, @@ -49,7 +48,7 @@ pub enum MathOp { Rem, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum GateType { LessThan, GreaterThan, @@ -57,7 +56,7 @@ pub enum GateType { NotEqual, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, PartialEq)] pub enum WireType { Vertical, Horizontal, @@ -97,9 +96,20 @@ impl Tile { let tex_name = match self { Tile::Blank => "", Tile::Block => "block", - Tile::Comment(_) => "", Tile::Marble { value: _, dir: _ } => "marble", - Tile::Digit(_) => "", + Tile::Digit(n) => match n { + b'0' => "digit_0", + b'1' => "digit_1", + b'2' => "digit_2", + b'3' => "digit_3", + b'4' => "digit_4", + b'5' => "digit_5", + b'6' => "digit_6", + b'7' => "digit_7", + b'8' => "digit_8", + b'9' => "digit_9", + _ => unreachable!("invalid digit"), + }, Tile::Mirror(mirror) => match mirror { MirrorType::Forward => "mirror_forward", MirrorType::Back => "mirror_back", @@ -139,8 +149,7 @@ impl Tile { &format!("{t}_{}", if *state { "on" } else { "off" }) } }; - let tex_name = format!("{tex_name}.png"); - if let Some(texture) = textures.get(&tex_name) { + if let Some(texture) = textures.get(tex_name) { d.draw_texture_ex( texture, Vector2::new((x - size / 2) as f32, (y - size / 2) as f32), @@ -153,13 +162,6 @@ impl Tile { match self { Tile::Blank => (), - Tile::Comment(c) => { - d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::DIMGRAY); - d.draw_text(&format!("{}", *c as char), x - 10, y - 10, 20, Color::WHITE); - } - Tile::Digit(n) => { - d.draw_text(&String::from(*n as char), x - 10, y - 10, 20, Color::ORANGE) - } _ => d.draw_rectangle(x - size / 2, y - size / 2, size, size, Color::RED), } }