marble-machinations/src/main.rs

193 lines
4.7 KiB
Rust
Raw Normal View History

2024-10-06 12:39:36 +02:00
use std::{
collections::HashMap,
fs::{read_dir, read_to_string},
};
2024-10-03 22:59:49 +02:00
use raylib::prelude::*;
2024-10-06 00:24:11 +02:00
mod editor;
2024-10-06 12:39:36 +02:00
mod level;
2024-10-03 22:59:49 +02:00
mod marble_engine;
2024-10-06 12:39:36 +02:00
mod solution;
2024-10-04 21:20:53 +02:00
mod util;
2024-10-06 12:39:36 +02:00
use editor::Editor;
use level::Level;
use marble_engine::board::Board;
use solution::Solution;
2024-10-04 21:20:53 +02:00
use util::*;
2024-10-06 12:39:36 +02:00
struct Game {
levels: Vec<Level>,
solutions: HashMap<String, Vec<Solution>>,
open_editor: Option<Editor>,
textures: Textures,
selected_level: usize,
selected_solution: usize,
}
2024-10-03 22:59:49 +02:00
fn main() {
2024-10-04 01:21:52 +02:00
let (mut rl, thread) = raylib::init()
.resizable()
.title("good window title")
.build();
2024-10-03 22:59:49 +02:00
rl.set_target_fps(60);
rl.set_window_min_size(640, 480);
rl.set_mouse_cursor(MouseCursor::MOUSE_CURSOR_CROSSHAIR);
rl.set_exit_key(None);
rl.set_trace_log(TraceLogLevel::LOG_WARNING);
2024-10-03 22:59:49 +02:00
2024-10-06 12:39:36 +02:00
let mut game = Game::new(&mut rl, &thread);
game.run(&mut rl, &thread);
// let board = Board::parse(&read_to_string("boards/adder.mbl").unwrap());
// game.load_board(board);
}
impl Game {
fn new(rl: &mut RaylibHandle, thread: &RaylibThread) -> Self {
let mut textures = Textures::default();
textures.load_dir("assets", rl, &thread);
textures.load_dir("assets/tiles", rl, &thread);
Self {
levels: get_levels(),
solutions: HashMap::new(),
open_editor: None,
textures,
selected_level: 0,
selected_solution: 0,
}
}
2024-10-05 15:19:27 +02:00
2024-10-06 12:39:36 +02:00
fn run(&mut self, rl: &mut RaylibHandle, thread: &RaylibThread) {
while !rl.window_should_close() {
let mut d = rl.begin_drawing(&thread);
if let Some(editor) = &mut self.open_editor {
editor.input(&d);
editor.draw(&mut d, &self.textures);
} else {
self.draw(&mut d);
}
}
}
2024-10-04 01:21:52 +02:00
2024-10-06 12:39:36 +02:00
fn draw(&mut self, d: &mut RaylibDrawHandle) {
d.clear_background(Color::new(64, 64, 64, 255));
2024-10-06 12:39:36 +02:00
let level_list_width = 320;
let screen_height = d.get_screen_height();
d.draw_rectangle(0, 0, level_list_width, screen_height, Color::GRAY);
// let (a, b, c) = d.gui_scroll_panel(
// Rectangle {
// x: 10.,
// y: 10.,
// width: level_list_width as f32 - 20.,
// height: screen_height as f32 - 40.,
// },
// Some(rstr!("text")),
// Rectangle{},
// scroll,
// view,
// );
let clicked = d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT);
let mouse_pos = d.get_mouse_position();
for (i, level) in self.levels.iter().enumerate() {
let level_entry_height = 48;
let y = 10 + i as i32 * level_entry_height;
let bounds = Rectangle {
x: 5.,
y: y as f32 - 5.,
width: level_list_width as f32 - 10.,
height: level_entry_height as f32 - 5.,
};
if clicked && bounds.check_collision_point_rec(mouse_pos) && self.selected_level != i {
self.selected_solution = 0;
self.selected_level = i;
}
if self.selected_level == i {
d.draw_rectangle_rec(bounds, Color::DARKCYAN);
}
d.draw_text(level.name(), 10, y, 20, Color::WHITE);
let solution_count = self
.solutions
.get(level.id())
.map(Vec::len)
.unwrap_or_default();
let subtext = format!("solutions: {solution_count}");
let subtext_color = if solution_count > 0 {
Color::GOLD
} else {
Color::LIGHTGRAY
};
d.draw_text(&subtext, 10, y + 20, 10, subtext_color);
}
if let Some(level) = self.levels.get(self.selected_level) {
d.draw_text(level.name(), level_list_width + 10, 10, 30, Color::CYAN);
d.draw_text(level.id(), level_list_width + 10, 40, 10, Color::GRAY);
let mut y = 60;
if let Some(solutions) = self.solutions.get_mut(level.id()) {
let solution_entry_height = 40;
for (solution_index, solution) in solutions.iter().enumerate() {
simple_option_button(
d,
level_list_width + 10,
y,
200,
solution_entry_height,
solution_index,
&mut self.selected_solution,
);
let name_color = if solution.score.is_some() {
Color::LIME
} else {
Color::ORANGE
};
d.draw_text(&solution.name, level_list_width + 15, y + 5, 20, name_color);
d.draw_text(
&solution.score_text(),
level_list_width + 15,
y + 25,
10,
Color::WHITE,
);
y += solution_entry_height + 10;
}
// d.gui_button(bounds, text)
if simple_button(d, level_list_width + 10, y, 200, 30) {
let n = solutions.len();
solutions.push(Solution::new(level.id().to_owned(), n));
}
d.draw_text(
"new solution",
level_list_width + 15,
y + 5,
20,
Color::WHITE,
);
} else {
self.solutions.insert(level.id().to_owned(), Vec::new());
}
}
}
}
fn get_levels() -> Vec<Level> {
let mut levels = Vec::<Level>::new();
for d in read_dir("levels").unwrap().flatten() {
let l = read_to_string(d.path())
.ok()
.as_deref()
.map(|s| serde_json::from_str(s).ok())
.flatten();
if let Some(level) = l {
levels.push(level);
}
2024-10-05 20:22:18 +02:00
}
2024-10-06 12:39:36 +02:00
levels.sort_by(|a, b| a.id().cmp(b.id()));
levels
2024-10-05 20:22:18 +02:00
}