From 8b62aec760d2ebac5eca168ecd97019f688ea289 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 22 Dec 2024 18:03:52 +0100 Subject: [PATCH] show level count for each chapter --- README.md | 48 +++--------------------------------------------- src/main.rs | 35 +++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 61 deletions(-) diff --git a/README.md b/README.md index df716cc..2f7fa49 100644 --- a/README.md +++ b/README.md @@ -35,11 +35,8 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble ``` - assets/ - levels/ - - 01_intro/ - - 01_output.json - - 02_cat.json - - 02_lists/ - - 02_parse.json + - chapter_01.json + - chapter_02.json - sandbox.json - user/ - solutions/ @@ -52,46 +49,7 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble - blueprints - blueprint_0.json ``` -## formats -### level -`00_zeroes.json` -```json -{ - "id": "output", - "sortorder": 5, - "name": "Zeroes", - "description": "learn how to output data", - "init_board": "", - "stages": [{ - "input": [], - "output": [0, 0, 0, 0, 0, 0, 0, 0], - }] -} -``` -### solution -`00_zeroes/solution_0.json` -```json -{ - "level_id": "00_zeroes", - "solution_id": 0, - "name": "unnamed 1", - "board": "oo\nP*\n|-", - "score": { - "cycles": 8, - "tiles": 6, - "area": 6, - } -} -``` -### blueprint -`blueprints/blueprint_0.json` -```json -{ - "id": 0, - "name": "zero_printer", - "board": "o -B I\n> * < \n" -} -``` + ## levels ### intro, basic mechanics diff --git a/src/main.rs b/src/main.rs index a999f71..760c56d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,7 +40,7 @@ struct Game { #[derive(Debug)] enum LevelListEntry { Level(Level), - ChapterTitle(String), + Chapter(String, usize), } fn main() { @@ -128,43 +128,46 @@ impl Game { let mouse_pos = d.get_mouse_position(); let scroll_delta = d.get_mouse_wheel_move(); + const ENTRY_SPACING: i32 = 65; + let fit_on_screen = (d.get_screen_height() / ENTRY_SPACING) as usize; + let max_scroll = self.levels.len().saturating_sub(fit_on_screen); if mouse_pos.x < level_list_width as f32 { - if scroll_delta < 0. && self.level_scroll < self.levels.len().saturating_sub(5) { + if scroll_delta < 0. && self.level_scroll < max_scroll { self.level_scroll += 1; - } else if scroll_delta > 0. && self.level_scroll > 0 { + } + if scroll_delta > 0. && self.level_scroll > 0 { self.level_scroll -= 1; } } - for (i, level) in self.levels[self.level_scroll..].iter().enumerate() { - let level_entry_height = 65; - let index = i + self.level_scroll; - let y = 10 + i as i32 * level_entry_height; + for (row_index, level_index) in (self.level_scroll..self.levels.len()).enumerate() { + let level = &mut self.levels[level_index]; + let y = 10 + row_index as i32 * ENTRY_SPACING; let bounds = Rectangle { x: 5., y: y as f32 - 5., width: level_list_width as f32 - 10., - height: level_entry_height as f32 - 5., + height: ENTRY_SPACING as f32 - 5., }; + let clicked_this = clicked && bounds.check_collision_point_rec(mouse_pos); match level { - LevelListEntry::ChapterTitle(title) => { + LevelListEntry::Chapter(title, level_count) => { d.draw_rectangle_rec(bounds, BG_DARK); d.draw_text(title, 10, y, 30, FG_CHAPTER_TITLE); + let subtitle = format!("{level_count} levels"); + d.draw_text(&subtitle, 10, y + 30, 20, Color::WHITE); } LevelListEntry::Level(level) => { - if clicked - && bounds.check_collision_point_rec(mouse_pos) - && self.selected_level != index - { + if clicked_this && self.selected_level != level_index { self.editing_solution_name = false; - self.selected_level = index; + self.selected_level = level_index; self.selected_solution = 0; // select the last solution of the level, if there is one if let Some(solutions) = self.solutions.get(level.id()) { self.selected_solution = solutions.len().saturating_sub(1); } } - d.draw_rectangle_rec(bounds, widget_bg(self.selected_level == index)); + d.draw_rectangle_rec(bounds, widget_bg(self.selected_level == level_index)); let mut title_color = Color::WHITE; if let Some(solutions) = self.solutions.get(level.id()) { @@ -313,7 +316,7 @@ fn get_levels() -> Vec { let mut levels = Vec::new(); for c in chapters { - levels.push(LevelListEntry::ChapterTitle(c.title)); + levels.push(LevelListEntry::Chapter(c.title, c.levels.len())); levels.extend(c.levels.into_iter().map(LevelListEntry::Level)); } levels