From 86548a8b0d6cfb91da9f7715bfed34b00e6f8302 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 5 Oct 2024 15:01:06 +0200 Subject: [PATCH] add digit sprites, remove comment tiles --- assets/tiles/digit_0.png | Bin 0 -> 162 bytes assets/tiles/digit_1.png | Bin 0 -> 163 bytes assets/tiles/digit_2.png | Bin 0 -> 180 bytes assets/tiles/digit_3.png | Bin 0 -> 170 bytes assets/tiles/digit_4.png | Bin 0 -> 185 bytes assets/tiles/digit_5.png | Bin 0 -> 175 bytes assets/tiles/digit_6.png | Bin 0 -> 171 bytes assets/tiles/digit_7.png | Bin 0 -> 160 bytes assets/tiles/digit_8.png | Bin 0 -> 163 bytes assets/tiles/digit_9.png | Bin 0 -> 168 bytes src/main.rs | 15 ++++--- src/marble_engine.rs | 87 ++++++++++++++++---------------------- src/marble_engine/tile.rs | 38 +++++++++-------- 13 files changed, 65 insertions(+), 75 deletions(-) create mode 100644 assets/tiles/digit_0.png create mode 100644 assets/tiles/digit_1.png create mode 100644 assets/tiles/digit_2.png create mode 100644 assets/tiles/digit_3.png create mode 100644 assets/tiles/digit_4.png create mode 100644 assets/tiles/digit_5.png create mode 100644 assets/tiles/digit_6.png create mode 100644 assets/tiles/digit_7.png create mode 100644 assets/tiles/digit_8.png create mode 100644 assets/tiles/digit_9.png diff --git a/assets/tiles/digit_0.png b/assets/tiles/digit_0.png new file mode 100644 index 0000000000000000000000000000000000000000..eed0adb6f8fe8954990b48f9c9849559c7a0ec47 GIT binary patch literal 162 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|nVv3=AsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc#LBs^kgsKLTBGkpF6EoJa@ L^>bP0l+XkKUd=f~ literal 0 HcmV?d00001 diff --git a/assets/tiles/digit_1.png b/assets/tiles/digit_1.png new file mode 100644 index 0000000000000000000000000000000000000000..239009601021314b0fab9eea606ab3f61bf4466c GIT binary patch literal 163 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|S)MMAAsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc2fJo)8!3~fvP9X+Qay#Kvi(xotmdWTNylE L{an^LB{Ts5`+Yj9 literal 0 HcmV?d00001 diff --git a/assets/tiles/digit_2.png b/assets/tiles/digit_2.png new file mode 100644 index 0000000000000000000000000000000000000000..d0fb66456fcf0be9177bb11c2337e26d61e8b28a GIT binary patch literal 180 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|)t)YnAsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc#LBsSaUSsOa T^*S;aXgPzYtDnm{r-UW|_d7og literal 0 HcmV?d00001 diff --git a/assets/tiles/digit_4.png b/assets/tiles/digit_4.png new file mode 100644 index 0000000000000000000000000000000000000000..765bb921a950d27de9a8ec0b56a1fb5d48111c24 GIT binary patch literal 185 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|4W2HJAsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bcIF)h@@N3v!rb*L*L@Qw|L9<6 ZSYK?){vmPF2A~TVJYD@<);T3K0RRHAK#l+a literal 0 HcmV?d00001 diff --git a/assets/tiles/digit_6.png b/assets/tiles/digit_6.png new file mode 100644 index 0000000000000000000000000000000000000000..06c690d5bd620236bbcef3f3a13f8f050e87c6ca GIT binary patch literal 171 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|MV>B>AsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc#LBs7Fi*AsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc#LBs|3Mum&3O$A!X;OXk; Jvd$@?2>@JWIluq_ literal 0 HcmV?d00001 diff --git a/assets/tiles/digit_8.png b/assets/tiles/digit_8.png new file mode 100644 index 0000000000000000000000000000000000000000..4f18e6093b373ccf529012af0155acd070d76854 GIT binary patch literal 163 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61SBU+%rFB|S)MMAAsQ2tb0W)o8buBqI&h%i zo8ab+ckX*@t33!@E*{7bc#LBs = 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), } }