show level count for each chapter

This commit is contained in:
Crispy 2024-12-22 18:03:52 +01:00
parent 42dfe4fac7
commit 8b62aec760
2 changed files with 22 additions and 61 deletions

View file

@ -35,11 +35,8 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble
``` ```
- assets/ - assets/
- levels/ - levels/
- 01_intro/ - chapter_01.json
- 01_output.json - chapter_02.json
- 02_cat.json
- 02_lists/
- 02_parse.json
- sandbox.json - sandbox.json
- user/ - user/
- solutions/ - solutions/
@ -52,46 +49,7 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble
- blueprints - blueprints
- blueprint_0.json - blueprint_0.json
``` ```
## formats
### level
`00_zeroes.json`
```json
{
"id": "output",
"sortorder": 5,
"name": "Zeroes",
"description": "learn how to output data",
"init_board": "<optional, serialized 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 ## levels
### intro, basic mechanics ### intro, basic mechanics

View file

@ -40,7 +40,7 @@ struct Game {
#[derive(Debug)] #[derive(Debug)]
enum LevelListEntry { enum LevelListEntry {
Level(Level), Level(Level),
ChapterTitle(String), Chapter(String, usize),
} }
fn main() { fn main() {
@ -128,43 +128,46 @@ impl Game {
let mouse_pos = d.get_mouse_position(); let mouse_pos = d.get_mouse_position();
let scroll_delta = d.get_mouse_wheel_move(); 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 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; 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; self.level_scroll -= 1;
} }
} }
for (i, level) in self.levels[self.level_scroll..].iter().enumerate() { for (row_index, level_index) in (self.level_scroll..self.levels.len()).enumerate() {
let level_entry_height = 65; let level = &mut self.levels[level_index];
let index = i + self.level_scroll; let y = 10 + row_index as i32 * ENTRY_SPACING;
let y = 10 + i as i32 * level_entry_height;
let bounds = Rectangle { let bounds = Rectangle {
x: 5., x: 5.,
y: y as f32 - 5., y: y as f32 - 5.,
width: level_list_width as f32 - 10., 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 { match level {
LevelListEntry::ChapterTitle(title) => { LevelListEntry::Chapter(title, level_count) => {
d.draw_rectangle_rec(bounds, BG_DARK); d.draw_rectangle_rec(bounds, BG_DARK);
d.draw_text(title, 10, y, 30, FG_CHAPTER_TITLE); 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) => { LevelListEntry::Level(level) => {
if clicked if clicked_this && self.selected_level != level_index {
&& bounds.check_collision_point_rec(mouse_pos)
&& self.selected_level != index
{
self.editing_solution_name = false; self.editing_solution_name = false;
self.selected_level = index; self.selected_level = level_index;
self.selected_solution = 0; self.selected_solution = 0;
// select the last solution of the level, if there is one // select the last solution of the level, if there is one
if let Some(solutions) = self.solutions.get(level.id()) { if let Some(solutions) = self.solutions.get(level.id()) {
self.selected_solution = solutions.len().saturating_sub(1); 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; let mut title_color = Color::WHITE;
if let Some(solutions) = self.solutions.get(level.id()) { if let Some(solutions) = self.solutions.get(level.id()) {
@ -313,7 +316,7 @@ fn get_levels() -> Vec<LevelListEntry> {
let mut levels = Vec::new(); let mut levels = Vec::new();
for c in chapters { 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.extend(c.levels.into_iter().map(LevelListEntry::Level));
} }
levels levels