From c4378c85f546a0e9a53970ca676f8d3def8cf69c Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Mar 2025 11:53:58 +0100 Subject: [PATCH 1/3] auto-strip trailing whitespace in serialized grids --- src/board.rs | 4 ++-- src/marble_engine/grid.rs | 13 +++++++++---- tests/main.rs | 2 +- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/board.rs b/src/board.rs index dac08f4..688608b 100644 --- a/src/board.rs +++ b/src/board.rs @@ -39,7 +39,7 @@ impl From for Board { fn from(value: CompatBoard) -> Self { match value { CompatBoard::V1(string) => Self { - grid: Grid::parse(&string), + grid: Grid::from_ascii(&string), comments: Vec::new(), }, CompatBoard::V2 { grid, comments } => Self { grid, comments }, @@ -149,7 +149,7 @@ impl Board { pub fn from_user_str(source: &str) -> Self { serde_json::from_str(source).unwrap_or_else(|_| Self { - grid: Grid::parse(source), + grid: Grid::from_ascii(source), comments: Vec::new(), }) } diff --git a/src/marble_engine/grid.rs b/src/marble_engine/grid.rs index f292b13..a958be1 100644 --- a/src/marble_engine/grid.rs +++ b/src/marble_engine/grid.rs @@ -53,7 +53,7 @@ impl ResizeDeltas { } impl Grid { - pub fn parse(source: &str) -> Self { + pub fn from_ascii(source: &str) -> Self { let mut rows = Vec::new(); let mut width = 0; @@ -79,13 +79,18 @@ impl Grid { } } - pub fn to_str(&self) -> String { + pub fn to_ascii(&self) -> String { let mut out = String::new(); for y in 0..self.height { for x in 0..self.width { let tile = self.get((x, y).into()).unwrap(); out.push(tile.to_char()); } + if y > 0 { + while out.as_bytes().last() == Some(&b' ') { + out.pop(); + } + } out.push('\n'); } out @@ -269,12 +274,12 @@ impl Grid { impl From for Grid { fn from(value: String) -> Self { - Self::parse(&value) + Self::from_ascii(&value) } } impl From for String { fn from(val: Grid) -> String { - val.to_str() + val.to_ascii() } } diff --git a/tests/main.rs b/tests/main.rs index 336f92e..7b8e118 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -3,7 +3,7 @@ use marble_machinations::marble_engine::{grid::Grid, Machine}; #[test] fn creating_marbles_cause_indirect_claim() { let mut eng = Machine::new_empty(); - eng.set_grid(Grid::parse( + eng.set_grid(Grid::from_ascii( " I o 2 From f5b5356139d22e50228f36b311cce0bd2144ca6e Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Mar 2025 12:21:41 +0100 Subject: [PATCH 2/3] center first line of comments vertically in the grid --- src/board.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/board.rs b/src/board.rs index 688608b..e00a994 100644 --- a/src/board.rs +++ b/src/board.rs @@ -79,6 +79,7 @@ impl Board { for comment in &self.comments { let x = comment.x * tile_size + offset.x as i32; let y = comment.y * tile_size + offset.y as i32; + let y = y + (tile_size - font_size) / 2; // center vertically in the grid row for (i, line) in comment.text.lines().enumerate() { let y = y + line_space * i as i32; d.draw_text(line, x, y, font_size, Color::ORANGE); From fc1670f97d8be80680a16ecbc0e750316cd7c1fa Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 29 Mar 2025 12:23:15 +0100 Subject: [PATCH 3/3] refresh intro levels 2-4, making them more like a tutorial --- levels/chapter_01.json | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/levels/chapter_01.json b/levels/chapter_01.json index 81c6a2d..2ceca82 100644 --- a/levels/chapter_01.json +++ b/levels/chapter_01.json @@ -22,6 +22,14 @@ "id": "digits", "name": "Digits", "description": "place digits and use number keys to assign them values", + "init_board": { + "grid": " \n\n\n o\n o\n o\n 1\n 4 8 7\n\n I I I\n\n\n\n", + "comments": [ + { "text": "Digit tiles are consumed by marbles that pass over them,\n adding their value to the end of the marble's number", "x": 8, "y": 5 }, + { "text": "Try selecting this 7 with the digit tool (#) in your toolbar\n then change it to a 6 using your keyboard number keys", "x": 8, "y": 7 }, + { "text": "You can also use the arrow keys to move the selection around,\n if you need to type more numbers", "x": 8, "y": 10 } + ] + }, "stages": [{ "input": [], "output": [4, 8, 16] @@ -31,7 +39,14 @@ "id": "loop", "name": "Loop", "description": "repeated output", - "init_board": "\n \n o\n\n\n\n ^ \n\n", + "init_board": { + "grid": " \n\n\n v\n o\n\n *B I\n\n\n ^\n\n\n\n", + "comments": [ + { "text": "Arrows change the direction of marbles that collide with them", "x": 4, "y": 3 }, + { "text": " v Buttons are activated by marbles, and can power other machines", "x": 3, "y": 5 }, + { "text": "^ Silos create new marbles when powered", "x": 4, "y": 7 } + ] + }, "stages": [{ "input": [], "output": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] @@ -41,6 +56,12 @@ "id": "copy_input", "name": "Copy Cat", "description": "read input and output the same thing", + "init_board": { + "grid": " \n\n\n v\n o\n\n *I I\n\n\n ^\n\n\n\n", + "comments": [ + { "text": "^ When an input/output silo is powered, it creates a new marble,\n containing the next value from the level input as a number", "x": 4, "y": 7 } + ] + }, "stages": [{ "input": "Hello, world!", "output": "Hello, world!"