Add widgets, proper resources.rs
This commit is contained in:
@@ -64,11 +64,3 @@ $ sol_cli --solve-board pb......br..p..p
|
||||
There are atleast 1 solutions to this puzzle
|
||||
|
||||
```
|
||||
|
||||
## Heuristics of current algorithm
|
||||
|
||||
1. About 6-7 pieces on the board.
|
||||
2. Select pieces to place based on its weight.
|
||||
3. Eg: Queen is too powerful, so it has lower weightage.
|
||||
4. Eg: Knights are confusing. More knights.
|
||||
|
||||
|
||||
+1
-1
@@ -72,7 +72,7 @@ fn generate_puzzle(num_pieces: Option<u32>, num_solutions: Option<u32>) -> Optio
|
||||
"Generating a puzzle with {} pieces with a maximum of {} solutions",
|
||||
num_pieces, num_solutions
|
||||
);
|
||||
let gen_result = generator::generate(num_pieces, num_solutions, &RandRngImpl);
|
||||
let gen_result = generator::generate_weighted_random(num_pieces, num_solutions, &RandRngImpl);
|
||||
gen_result.print_stats();
|
||||
|
||||
let Some(puzzle) = gen_result.puzzle() else {
|
||||
|
||||
+43
-40
@@ -1,55 +1,52 @@
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
fmt::{self, Display, Formatter},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
mod button;
|
||||
mod color;
|
||||
pub mod constants;
|
||||
mod draw;
|
||||
mod logic;
|
||||
mod shadow;
|
||||
pub mod sound;
|
||||
mod texture;
|
||||
|
||||
use button::Button;
|
||||
use crate::{
|
||||
resources::Resources,
|
||||
widgets::{button::Button, id_text_input::IdTextInput},
|
||||
};
|
||||
use macroquad::prelude::*;
|
||||
use sol_lib::{board::Board, generator::Puzzle};
|
||||
use sound::Sounds;
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Game {
|
||||
// The generated puzzle. We keep a copy of this to reset the game.
|
||||
resources: Resources,
|
||||
|
||||
volume: f32,
|
||||
|
||||
puzzle: Puzzle,
|
||||
|
||||
// What is shown to the user
|
||||
current_board: Board,
|
||||
|
||||
// Constants througout the game
|
||||
texture_res: Texture2D,
|
||||
sounds: Sounds,
|
||||
font: Rc<Font>,
|
||||
num_squares: usize,
|
||||
heading_text: String,
|
||||
|
||||
// Update below on handle input
|
||||
state: GameState,
|
||||
debug: bool,
|
||||
game_mode: GameMode,
|
||||
|
||||
// Update below on window resize
|
||||
// Used for drawing the state
|
||||
square_width: f32,
|
||||
window_height: f32,
|
||||
window_width: f32,
|
||||
|
||||
current_board: Board,
|
||||
square_width: f32,
|
||||
num_squares: usize, // this is a constant = 4 for now.
|
||||
board_rect: Rect,
|
||||
squares: Vec<GameSquare>,
|
||||
|
||||
heading_rect: Rect,
|
||||
heading_font_size: f32,
|
||||
gp_btns: HashMap<ButtonAction, Button>,
|
||||
mode_btns: HashMap<GameMode, Button>,
|
||||
rules: bool,
|
||||
rules_btn: Option<Button>,
|
||||
heading_text: String,
|
||||
|
||||
show_rules: bool,
|
||||
rules_text: String,
|
||||
rules_btn: Button,
|
||||
|
||||
id_text: IdTextInput,
|
||||
|
||||
reset_btn: Button,
|
||||
|
||||
next_btn: Button,
|
||||
|
||||
game_mode: GameMode,
|
||||
easy_btn: Button,
|
||||
medium_btn: Button,
|
||||
hard_btn: Button,
|
||||
}
|
||||
|
||||
struct GameSquare {
|
||||
@@ -62,12 +59,6 @@ struct GameSquare {
|
||||
j: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub enum ButtonAction {
|
||||
Reset,
|
||||
Next,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||
pub enum GameMode {
|
||||
Easy,
|
||||
@@ -75,6 +66,12 @@ pub enum GameMode {
|
||||
Hard,
|
||||
}
|
||||
|
||||
impl Default for GameMode {
|
||||
fn default() -> Self {
|
||||
GameMode::Medium
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum GameState {
|
||||
SelectSource(Option<(usize, usize)>),
|
||||
@@ -82,6 +79,12 @@ enum GameState {
|
||||
GameOver((usize, usize)),
|
||||
}
|
||||
|
||||
impl Default for GameState {
|
||||
fn default() -> Self {
|
||||
GameState::SelectSource(None)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for GameState {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
|
||||
@@ -31,8 +31,8 @@ pub const BOTTOM_RIGHT_ROW_OFFSET_MULTIPLIER: f32 = 0.3;
|
||||
pub const TOP_HEADING_OFFSET_MULTIPLIER: f32 = 0.5;
|
||||
|
||||
pub const HEADING_TEXT: &str = "Solitaire Chess";
|
||||
pub const RESET_BUTTON_TEXT: &str = "RESET";
|
||||
pub const NEXT_BUTTON_TEXT: &str = "NEXT";
|
||||
pub const RESET_BUTTON_TEXT: &str = "R";
|
||||
pub const NEXT_BUTTON_TEXT: &str = "N";
|
||||
pub const RULES_BUTTON_TEXT: &str = "RULES";
|
||||
pub const RULES_BUTTON_ALT_TEXT: &str = "CLOSE";
|
||||
pub const EASY_BUTTON_TEXT: &str = "EASY";
|
||||
|
||||
+131
-90
@@ -1,9 +1,7 @@
|
||||
use super::{
|
||||
ButtonAction, Game, GameMode, button::Button, color::UiColor, constants, shadow,
|
||||
texture::PieceTexture,
|
||||
};
|
||||
use super::{Game, GameMode, constants};
|
||||
use crate::widgets::{button::Button, id_text_input::IdTextInput, *};
|
||||
use macroquad::{math, prelude::*};
|
||||
use std::collections::HashMap;
|
||||
use sol_lib::board::piece::Piece;
|
||||
|
||||
impl Game {
|
||||
pub fn draw(&mut self) {
|
||||
@@ -11,6 +9,7 @@ impl Game {
|
||||
self.draw_heading();
|
||||
self.draw_board();
|
||||
self.draw_buttons();
|
||||
self.draw_id_text();
|
||||
self.draw_debug();
|
||||
}
|
||||
|
||||
@@ -45,7 +44,12 @@ impl Game {
|
||||
|
||||
self.heading_font_size = constants::HEADING_FONT_SIZE_MULTIPLIER * min_dimension;
|
||||
let f = self.heading_font_size.floor() as u16;
|
||||
let dims = measure_text(self.heading_text.as_str(), Some(&self.font), f, 1.0);
|
||||
let dims = measure_text(
|
||||
self.heading_text.as_str(),
|
||||
Some(&self.resources.font()),
|
||||
f,
|
||||
1.0,
|
||||
);
|
||||
self.heading_rect = Rect::new(
|
||||
board_x + (board_width - dims.width) / 2.0,
|
||||
board_y - dims.height - constants::TOP_HEADING_OFFSET_MULTIPLIER * self.square_width,
|
||||
@@ -83,114 +87,95 @@ impl Game {
|
||||
// Buttons
|
||||
let btn_h = constants::BUTTON_HEIGHT_MULTIPLIER * min_dimension;
|
||||
let btn_w = board_width * constants::BUTTON_WIDTH_MULTIPLIER;
|
||||
let btn_w_sq = btn_h;
|
||||
|
||||
// Bottom row
|
||||
let bottom_row_y = board_width
|
||||
+ board_y
|
||||
+ constants::BOTTOM_BUTTON_ROW_OFFSET_MULTIPLIER * self.square_width;
|
||||
let btn_reset_x_offset =
|
||||
(self.board_rect.w - self.square_width) + (self.square_width - btn_w) / 2.;
|
||||
let reset_btn = Button::new(
|
||||
constants::RESET_BUTTON_TEXT,
|
||||
Rect::new(board_x + btn_reset_x_offset, bottom_row_y, btn_w, btn_h),
|
||||
UiColor::Yellow,
|
||||
self.sounds.button.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
self.reset_btn = Button::new(Rect::new(
|
||||
board_x + (self.square_width - btn_w_sq) / 2.,
|
||||
bottom_row_y,
|
||||
btn_w_sq,
|
||||
btn_h,
|
||||
));
|
||||
let btn_next_x_offset =
|
||||
(self.board_rect.w - 2. * self.square_width) + (self.square_width - btn_w) / 2.;
|
||||
let mut next_btn = Button::new(
|
||||
constants::NEXT_BUTTON_TEXT,
|
||||
Rect::new(board_x + btn_next_x_offset, bottom_row_y, btn_w, btn_h),
|
||||
UiColor::Green,
|
||||
self.sounds.button.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
next_btn.is_active = false;
|
||||
(self.board_rect.w - self.square_width) + (self.square_width - btn_w_sq) / 2.;
|
||||
self.next_btn = Button::new(Rect::new(
|
||||
board_x + btn_next_x_offset,
|
||||
bottom_row_y,
|
||||
btn_w_sq,
|
||||
btn_h,
|
||||
));
|
||||
self.next_btn.is_active = false;
|
||||
|
||||
self.gp_btns = HashMap::new();
|
||||
self.gp_btns.insert(ButtonAction::Next, next_btn);
|
||||
self.gp_btns.insert(ButtonAction::Reset, reset_btn);
|
||||
let id_text_rect = Rect::new(
|
||||
board_x + 2. * self.square_width - btn_w,
|
||||
bottom_row_y,
|
||||
btn_w * 2.,
|
||||
btn_h,
|
||||
);
|
||||
self.id_text = IdTextInput::new(id_text_rect);
|
||||
|
||||
// Left column
|
||||
let left_column_x =
|
||||
board_x - constants::BOTTOM_RIGHT_ROW_OFFSET_MULTIPLIER * self.square_width - btn_w;
|
||||
let rules_button = Button::new(
|
||||
constants::RULES_BUTTON_TEXT,
|
||||
Rect::new(
|
||||
self.rules_btn = Button::new(Rect::new(
|
||||
left_column_x,
|
||||
board_y + self.board_rect.h - self.square_width + (self.square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
),
|
||||
UiColor::Brown,
|
||||
self.sounds.button.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
self.rules_btn = Some(rules_button);
|
||||
));
|
||||
|
||||
// Right column
|
||||
let right_column_x = board_x
|
||||
+ board_width
|
||||
+ constants::BOTTOM_RIGHT_ROW_OFFSET_MULTIPLIER * self.square_width;
|
||||
let easy_btn = Button::new(
|
||||
constants::EASY_BUTTON_TEXT,
|
||||
Rect::new(
|
||||
self.easy_btn = Button::new(Rect::new(
|
||||
right_column_x,
|
||||
board_y + self.square_width + (self.square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
),
|
||||
UiColor::Yellow,
|
||||
self.sounds.mode.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
));
|
||||
|
||||
let medium_btn = Button::new(
|
||||
constants::MEDIUM_BUTTON_TEXT,
|
||||
Rect::new(
|
||||
self.medium_btn = Button::new(Rect::new(
|
||||
right_column_x,
|
||||
board_y + 2. * self.square_width + (self.square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
),
|
||||
UiColor::Yellow,
|
||||
self.sounds.mode.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
));
|
||||
|
||||
let hard_button = Button::new(
|
||||
constants::HARD_BUTTON_TEXT,
|
||||
Rect::new(
|
||||
self.hard_btn = Button::new(Rect::new(
|
||||
right_column_x,
|
||||
board_y + 3. * self.square_width + (self.square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
),
|
||||
UiColor::Yellow,
|
||||
self.sounds.mode.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
));
|
||||
|
||||
self.mode_btns = HashMap::new();
|
||||
self.mode_btns.insert(GameMode::Easy, easy_btn);
|
||||
self.mode_btns.insert(GameMode::Medium, medium_btn);
|
||||
self.mode_btns.insert(GameMode::Hard, hard_button);
|
||||
|
||||
for btn in &mut self.mode_btns {
|
||||
btn.1.is_active = true;
|
||||
if self.game_mode == *btn.0 {
|
||||
btn.1.is_active = false;
|
||||
match self.game_mode {
|
||||
GameMode::Easy => {
|
||||
self.easy_btn.is_active = false;
|
||||
}
|
||||
GameMode::Medium => {
|
||||
self.medium_btn.is_active = false;
|
||||
}
|
||||
GameMode::Hard => {
|
||||
self.hard_btn.is_active = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_heading(&self) {
|
||||
let f = self.heading_font_size.floor() as u16;
|
||||
let dims = measure_text(self.heading_text.as_str(), Some(&self.font), f, 1.0);
|
||||
let dims = measure_text(
|
||||
self.heading_text.as_str(),
|
||||
Some(&self.resources.font()),
|
||||
f,
|
||||
1.0,
|
||||
);
|
||||
let draw_text_params = TextParams {
|
||||
font_size: f,
|
||||
font: Some(&self.font),
|
||||
font: Some(&self.resources.font()),
|
||||
color: BLACK,
|
||||
..Default::default()
|
||||
};
|
||||
@@ -204,9 +189,9 @@ impl Game {
|
||||
|
||||
fn draw_board(&self) {
|
||||
let board_shadow_width = constants::BOARD_SHADOW_MULTIPLIER * self.square_width;
|
||||
shadow::draw_shadow(self.board_rect, board_shadow_width);
|
||||
draw_shadow(&self.board_rect, board_shadow_width);
|
||||
|
||||
if self.rules {
|
||||
if self.show_rules {
|
||||
draw_rectangle(
|
||||
self.board_rect.x,
|
||||
self.board_rect.y,
|
||||
@@ -220,10 +205,11 @@ impl Game {
|
||||
Every move should be a \n\
|
||||
capture. Win when only \n\
|
||||
one piece is left.\n";
|
||||
let measurement = measure_text(rules, Some(&self.font), font_size as u16, 1.0);
|
||||
let measurement =
|
||||
measure_text(rules, Some(&self.resources.font()), font_size as u16, 1.0);
|
||||
let draw_text_params = TextParams {
|
||||
font_size: font_size as u16,
|
||||
font: Some(&self.font),
|
||||
font: Some(&self.resources.font()),
|
||||
color: UiColor::Brown.to_bg_color(),
|
||||
..Default::default()
|
||||
};
|
||||
@@ -259,14 +245,14 @@ impl Game {
|
||||
|
||||
if let Some(p) = &self.current_board.cells[square.i][square.j] {
|
||||
let offset = (square.rect.w - sprite_size) / 2.0;
|
||||
let dtp = PieceTexture::for_piece(*p, sprite_size);
|
||||
let texture_params = self.piece_draw_texture_params(&p, sprite_size);
|
||||
if !square.is_source {
|
||||
draw_texture_ex(
|
||||
&self.texture_res,
|
||||
texture_params.texture,
|
||||
square.rect.x + offset,
|
||||
square.rect.y + offset,
|
||||
WHITE,
|
||||
dtp,
|
||||
texture_params.draw_text_params,
|
||||
);
|
||||
} else {
|
||||
selected_square = Some(square);
|
||||
@@ -276,30 +262,80 @@ impl Game {
|
||||
|
||||
if let Some(selected_square) = selected_square {
|
||||
if let Some(p) = self.current_board.cells[selected_square.i][selected_square.j] {
|
||||
let dtp = PieceTexture::for_piece(p, sprite_size);
|
||||
let texture_params = self.piece_draw_texture_params(&p, sprite_size);
|
||||
draw_texture_ex(
|
||||
&self.texture_res,
|
||||
texture_params.texture,
|
||||
mouse_position().0 - sprite_size / 2.0,
|
||||
mouse_position().1 - sprite_size / 2.0,
|
||||
WHITE,
|
||||
dtp,
|
||||
texture_params.draw_text_params,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn piece_draw_texture_params<'a>(
|
||||
&'a self,
|
||||
piece: &Piece,
|
||||
sprite_size: f32,
|
||||
) -> PieceDrawTextureParams<'a> {
|
||||
let texture = self.resources.get_piece_texture(&piece);
|
||||
let dtp = DrawTextureParams {
|
||||
source: Some(Rect::new(
|
||||
texture.texture_rect.x,
|
||||
texture.texture_rect.y,
|
||||
texture.texture_rect.w,
|
||||
texture.texture_rect.h,
|
||||
)),
|
||||
dest_size: Some(Vec2::new(sprite_size, sprite_size)),
|
||||
..DrawTextureParams::default()
|
||||
};
|
||||
|
||||
PieceDrawTextureParams {
|
||||
texture: texture.texture,
|
||||
draw_text_params: dtp,
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_buttons(&mut self) {
|
||||
for btn in &self.gp_btns {
|
||||
btn.1.draw();
|
||||
self.reset_btn.draw(
|
||||
constants::RESET_BUTTON_TEXT,
|
||||
&UiColor::Yellow,
|
||||
&self.resources.font(),
|
||||
);
|
||||
self.next_btn.draw(
|
||||
constants::NEXT_BUTTON_TEXT,
|
||||
&UiColor::Green,
|
||||
&self.resources.font(),
|
||||
);
|
||||
self.easy_btn.draw(
|
||||
constants::EASY_BUTTON_TEXT,
|
||||
&UiColor::Yellow,
|
||||
&self.resources.font(),
|
||||
);
|
||||
self.medium_btn.draw(
|
||||
constants::MEDIUM_BUTTON_TEXT,
|
||||
&UiColor::Yellow,
|
||||
&self.resources.font(),
|
||||
);
|
||||
self.hard_btn.draw(
|
||||
constants::HARD_BUTTON_TEXT,
|
||||
&UiColor::Yellow,
|
||||
&self.resources.font(),
|
||||
);
|
||||
|
||||
let rules_btn_text = if self.show_rules {
|
||||
constants::RULES_BUTTON_ALT_TEXT
|
||||
} else {
|
||||
constants::RULES_BUTTON_TEXT
|
||||
};
|
||||
self.rules_btn
|
||||
.draw(rules_btn_text, &UiColor::Brown, &self.resources.font());
|
||||
}
|
||||
|
||||
for btn in &self.mode_btns {
|
||||
btn.1.draw();
|
||||
}
|
||||
|
||||
if let Some(btn) = &self.rules_btn {
|
||||
btn.draw();
|
||||
}
|
||||
fn draw_id_text(&self) {
|
||||
self.id_text
|
||||
.draw(&self.resources.font(), &self.puzzle.board.id);
|
||||
}
|
||||
|
||||
fn draw_debug(&self) {
|
||||
@@ -343,3 +379,8 @@ impl Game {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
struct PieceDrawTextureParams<'a> {
|
||||
texture: &'a Texture2D,
|
||||
draw_text_params: DrawTextureParams,
|
||||
}
|
||||
|
||||
+121
-130
@@ -1,10 +1,13 @@
|
||||
use super::{ButtonAction, Game, GameMode, GameSquare, GameState, constants, sound::Sounds};
|
||||
use super::{Game, GameMode, GameSquare, GameState, constants};
|
||||
use crate::{
|
||||
resources::{Resources, SoundKind},
|
||||
widgets::{button::Button, *},
|
||||
};
|
||||
use macroquad::prelude::*;
|
||||
use sol_lib::{
|
||||
board::BoardState,
|
||||
generator::{self, Puzzle, RandomRange},
|
||||
};
|
||||
use std::{collections::HashMap, rc::Rc};
|
||||
|
||||
impl Game {
|
||||
fn get(&mut self, i: usize, j: usize) -> &mut GameSquare {
|
||||
@@ -12,89 +15,17 @@ impl Game {
|
||||
}
|
||||
|
||||
pub fn handle_input(&mut self) {
|
||||
let mut gp_btn_clicked = None;
|
||||
for btn in &mut self.gp_btns {
|
||||
btn.1.handle_input();
|
||||
if btn.1.is_clicked() {
|
||||
gp_btn_clicked = Some(btn.0.clone());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(action) = gp_btn_clicked {
|
||||
match action {
|
||||
ButtonAction::Reset => self.reset(),
|
||||
ButtonAction::Next => self.next_puzzle(),
|
||||
}
|
||||
} else {
|
||||
let mut mode_btn_clicked = None;
|
||||
for btn in &mut self.mode_btns {
|
||||
btn.1.handle_input();
|
||||
if btn.1.is_clicked() {
|
||||
mode_btn_clicked = Some(btn);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(btn) = mode_btn_clicked {
|
||||
self.game_mode = *btn.0;
|
||||
self.next_puzzle();
|
||||
} else {
|
||||
let mut rules_btn_clicked = false;
|
||||
if let Some(btn) = &mut self.rules_btn {
|
||||
btn.handle_input();
|
||||
if btn.is_clicked() {
|
||||
rules_btn_clicked = true;
|
||||
}
|
||||
}
|
||||
|
||||
if rules_btn_clicked {
|
||||
self.rules = !self.rules;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for btn in &mut self.mode_btns {
|
||||
if self.game_mode == *btn.0 {
|
||||
btn.1.is_active = false;
|
||||
} else {
|
||||
btn.1.is_active = true;
|
||||
}
|
||||
}
|
||||
|
||||
if is_key_released(KeyCode::Escape) {
|
||||
if self.rules {
|
||||
Sounds::play(&self.sounds.button);
|
||||
}
|
||||
|
||||
self.rules = false;
|
||||
}
|
||||
|
||||
if let Some(rules_btn) = &mut self.rules_btn {
|
||||
if self.rules {
|
||||
rules_btn.text = constants::RULES_BUTTON_ALT_TEXT.to_string();
|
||||
} else {
|
||||
rules_btn.text = constants::RULES_BUTTON_TEXT.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
if is_key_released(KeyCode::D) {
|
||||
self.debug = !self.debug;
|
||||
return;
|
||||
}
|
||||
|
||||
if is_key_released(KeyCode::Q) {
|
||||
std::process::exit(0);
|
||||
}
|
||||
|
||||
// CORE GAMEPLAY
|
||||
let (mouse_x, mouse_y) = mouse_position();
|
||||
let mouse_pos = Circle::new(mouse_x, mouse_y, 0.0);
|
||||
if is_mouse_button_released(MouseButton::Left) {
|
||||
let current_state = self.state.clone();
|
||||
let new_state = match current_state {
|
||||
GameState::SelectSource(previous_target) => {
|
||||
self.handle_select_source(mouse_position(), previous_target)
|
||||
self.handle_select_source(mouse_pos, previous_target)
|
||||
}
|
||||
GameState::SelectTarget(source) => {
|
||||
let next = self.handle_select_target(mouse_position(), source);
|
||||
let next = self.handle_select_target(mouse_pos, source);
|
||||
if let GameState::SelectTarget(_) = next {
|
||||
self.reset_squares();
|
||||
GameState::SelectSource(None)
|
||||
@@ -105,15 +36,15 @@ impl Game {
|
||||
|
||||
GameState::GameOver(previous_target) => GameState::GameOver(previous_target),
|
||||
};
|
||||
|
||||
self.state = new_state;
|
||||
return;
|
||||
}
|
||||
|
||||
if is_mouse_button_pressed(MouseButton::Left) {
|
||||
let current_state = self.state.clone();
|
||||
let new_state = match current_state {
|
||||
GameState::SelectSource(previous_target) => {
|
||||
self.handle_select_source(mouse_position(), previous_target)
|
||||
self.handle_select_source(mouse_pos, previous_target)
|
||||
}
|
||||
GameState::SelectTarget(source) => GameState::SelectTarget(source),
|
||||
GameState::GameOver(previous_target) => GameState::GameOver(previous_target),
|
||||
@@ -121,19 +52,108 @@ impl Game {
|
||||
|
||||
self.state = new_state;
|
||||
}
|
||||
|
||||
if (self
|
||||
.reset_btn
|
||||
.handle_input(&self.resources.sound(&SoundKind::Button), self.volume))
|
||||
.is_clicked
|
||||
{
|
||||
self.reset();
|
||||
return;
|
||||
}
|
||||
|
||||
if (self
|
||||
.next_btn
|
||||
.handle_input(&self.resources.sound(&SoundKind::Button), self.volume))
|
||||
.is_clicked
|
||||
{
|
||||
self.next_puzzle();
|
||||
return;
|
||||
}
|
||||
|
||||
// PRESETS
|
||||
let mut clicked_game_mode_btn: Option<&Button> = None;
|
||||
if (self
|
||||
.easy_btn
|
||||
.handle_input(&self.resources.sound(&SoundKind::Mode), self.volume))
|
||||
.is_clicked
|
||||
{
|
||||
self.game_mode = GameMode::Easy;
|
||||
self.easy_btn.is_active = false;
|
||||
self.medium_btn.is_active = true;
|
||||
self.hard_btn.is_active = true;
|
||||
clicked_game_mode_btn = Some(&self.easy_btn);
|
||||
} else if (self
|
||||
.medium_btn
|
||||
.handle_input(&self.resources.sound(&SoundKind::Mode), self.volume))
|
||||
.is_clicked
|
||||
{
|
||||
self.game_mode = GameMode::Medium;
|
||||
self.easy_btn.is_active = true;
|
||||
self.medium_btn.is_active = false;
|
||||
self.hard_btn.is_active = true;
|
||||
clicked_game_mode_btn = Some(&self.medium_btn);
|
||||
} else if (self
|
||||
.hard_btn
|
||||
.handle_input(&self.resources.sound(&SoundKind::Mode), self.volume))
|
||||
.is_clicked
|
||||
{
|
||||
self.game_mode = GameMode::Hard;
|
||||
self.easy_btn.is_active = true;
|
||||
self.medium_btn.is_active = true;
|
||||
self.hard_btn.is_active = false;
|
||||
clicked_game_mode_btn = Some(&self.hard_btn);
|
||||
}
|
||||
|
||||
if let Some(_) = clicked_game_mode_btn {
|
||||
self.next_puzzle();
|
||||
return;
|
||||
}
|
||||
|
||||
if (self
|
||||
.rules_btn
|
||||
.handle_input(&self.resources.sound(&SoundKind::Button), self.volume))
|
||||
.is_clicked
|
||||
{
|
||||
self.show_rules = !self.show_rules;
|
||||
self.rules_text = match self.show_rules {
|
||||
true => constants::RULES_BUTTON_ALT_TEXT.to_string(),
|
||||
false => constants::RULES_BUTTON_TEXT.to_string(),
|
||||
};
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// KEY INPUTS
|
||||
if is_key_released(KeyCode::Escape) {
|
||||
if self.show_rules {
|
||||
play_sound_once(&self.resources.sound(&SoundKind::Button), self.volume);
|
||||
self.show_rules = false;
|
||||
self.rules_text = constants::RESET_BUTTON_TEXT.to_string();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if is_key_released(KeyCode::D) {
|
||||
self.debug = !self.debug;
|
||||
return;
|
||||
}
|
||||
|
||||
if is_key_released(KeyCode::Q) {
|
||||
std::process::exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_select_source(
|
||||
&mut self,
|
||||
mouse_position: (f32, f32),
|
||||
mouse_pos: Circle,
|
||||
previous_target: Option<(usize, usize)>,
|
||||
) -> GameState {
|
||||
self.reset_squares();
|
||||
let (x, y) = mouse_position;
|
||||
let mouse = Circle::new(x, y, 0.0);
|
||||
let mut selected = None;
|
||||
for square in &mut self.squares {
|
||||
if mouse.overlaps_rect(&square.rect) {
|
||||
if mouse_pos.overlaps_rect(&square.rect) {
|
||||
if let Some(_) = self.current_board.cells[square.i][square.j] {
|
||||
selected = Some((square.i, square.j));
|
||||
}
|
||||
@@ -163,17 +183,10 @@ impl Game {
|
||||
return GameState::SelectSource(None);
|
||||
}
|
||||
|
||||
fn handle_select_target(
|
||||
&mut self,
|
||||
mouse_position: (f32, f32),
|
||||
source: (usize, usize),
|
||||
) -> GameState {
|
||||
let (x, y) = mouse_position;
|
||||
let mouse = Circle::new(x, y, 0.0);
|
||||
|
||||
fn handle_select_target(&mut self, mouse_pos: Circle, source: (usize, usize)) -> GameState {
|
||||
let mut selected = None;
|
||||
for square in &mut self.squares {
|
||||
if mouse.overlaps_rect(&square.rect) {
|
||||
if mouse_pos.overlaps_rect(&square.rect) {
|
||||
if let Some(_) = self.current_board.cells[square.i][square.j] {
|
||||
selected = Some((square.i, square.j));
|
||||
}
|
||||
@@ -209,14 +222,10 @@ impl Game {
|
||||
{
|
||||
self.reset_squares();
|
||||
if self.current_board.game_state == BoardState::Won {
|
||||
let next_btn = self
|
||||
.gp_btns
|
||||
.get_mut(&ButtonAction::Next)
|
||||
.expect("Cannot find next button");
|
||||
next_btn.is_active = true;
|
||||
Sounds::play(&self.sounds.win);
|
||||
self.next_btn.is_active = true;
|
||||
play_sound_once(&self.resources.sound(&SoundKind::Win), self.volume);
|
||||
} else {
|
||||
Sounds::play(&self.sounds.loss);
|
||||
play_sound_once(&self.resources.sound(&SoundKind::Loss), self.volume);
|
||||
}
|
||||
|
||||
return GameState::GameOver((x, y));
|
||||
@@ -224,7 +233,7 @@ impl Game {
|
||||
|
||||
self.reset_squares();
|
||||
self.get(x, y).is_target = true;
|
||||
Sounds::play(&self.sounds.click);
|
||||
play_sound_once(&self.resources.sound(&SoundKind::Click), self.volume);
|
||||
return GameState::SelectSource(Some((x, y)));
|
||||
}
|
||||
|
||||
@@ -234,15 +243,9 @@ impl Game {
|
||||
|
||||
fn reset(&mut self) {
|
||||
self.current_board = self.puzzle.board.clone();
|
||||
self.reset_squares();
|
||||
|
||||
let next_button = self
|
||||
.gp_btns
|
||||
.get_mut(&ButtonAction::Next)
|
||||
.expect("Cannot find next button");
|
||||
next_button.is_active = false;
|
||||
|
||||
self.next_btn.is_active = false;
|
||||
self.state = GameState::SelectSource(None);
|
||||
self.reset_squares();
|
||||
}
|
||||
|
||||
fn next_puzzle(&mut self) {
|
||||
@@ -268,12 +271,13 @@ impl Game {
|
||||
GameMode::Hard => 7,
|
||||
};
|
||||
|
||||
let generated = generator::generate(piece_count, 100, &MacroquadRandAdapter);
|
||||
let generated =
|
||||
generator::generate_weighted_random(piece_count, 100, &MacroquadRandAdapter);
|
||||
let puzzle = generated.puzzle();
|
||||
puzzle.expect("No puzzle was generated")
|
||||
}
|
||||
|
||||
pub fn new_game(texture_res: Texture2D, sounds: Sounds, font: Font) -> Self {
|
||||
pub fn new_game(resources: Resources) -> Self {
|
||||
let game_mode = GameMode::Medium;
|
||||
let puzzle = Game::generate_puzzle(game_mode);
|
||||
let current_board = puzzle.board.clone();
|
||||
@@ -281,25 +285,12 @@ impl Game {
|
||||
Self {
|
||||
puzzle,
|
||||
current_board,
|
||||
board_rect: Rect::new(0., 0., 0., 0.),
|
||||
squares: Vec::new(),
|
||||
heading_rect: Rect::new(0., 0., 0., 0.),
|
||||
heading_text: constants::HEADING_TEXT.to_string(),
|
||||
heading_font_size: 0.,
|
||||
num_squares,
|
||||
texture_res,
|
||||
sounds,
|
||||
state: GameState::SelectSource(None),
|
||||
resources,
|
||||
game_mode,
|
||||
debug: false,
|
||||
gp_btns: HashMap::new(),
|
||||
mode_btns: HashMap::new(),
|
||||
rules: false,
|
||||
rules_btn: None,
|
||||
window_height: 0.,
|
||||
window_width: 0.,
|
||||
square_width: 0.,
|
||||
font: Rc::new(font),
|
||||
num_squares,
|
||||
heading_text: constants::HEADING_TEXT.to_string(),
|
||||
volume: constants::VOLUME,
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
use macroquad::prelude::*;
|
||||
|
||||
pub fn draw_shadow(rect: Rect, shadow_width: f32) {
|
||||
let shadow_color = Color::new(0., 0., 0., 0.8);
|
||||
draw_rectangle(
|
||||
rect.x + rect.w,
|
||||
rect.y + shadow_width,
|
||||
shadow_width,
|
||||
rect.h,
|
||||
shadow_color,
|
||||
);
|
||||
|
||||
draw_rectangle(
|
||||
rect.x + shadow_width,
|
||||
rect.y + rect.h,
|
||||
rect.w - shadow_width,
|
||||
shadow_width,
|
||||
shadow_color,
|
||||
);
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
use macroquad::audio::{self, Sound};
|
||||
use quad_snd::PlaySoundParams;
|
||||
|
||||
use crate::constants::VOLUME;
|
||||
|
||||
pub struct Sounds {
|
||||
pub click: Sound,
|
||||
pub win: Sound,
|
||||
pub loss: Sound,
|
||||
pub button: Sound,
|
||||
pub mode: Sound,
|
||||
}
|
||||
|
||||
impl Sounds {
|
||||
pub fn play(sound: &Sound) {
|
||||
audio::play_sound(
|
||||
sound,
|
||||
PlaySoundParams {
|
||||
looped: false,
|
||||
volume: VOLUME,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
use macroquad::prelude::*;
|
||||
use sol_lib::board::piece::Piece;
|
||||
|
||||
pub struct PieceTexture {
|
||||
x: f32,
|
||||
y: f32,
|
||||
w: f32,
|
||||
h: f32,
|
||||
}
|
||||
|
||||
impl PieceTexture {
|
||||
fn new(x: u32, y: u32) -> Self {
|
||||
Self {
|
||||
x: x as f32 * 128.0,
|
||||
y: y as f32 * 128.0,
|
||||
w: 128.0,
|
||||
h: 128.0,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn for_piece(piece: Piece, sprite_size: f32) -> DrawTextureParams {
|
||||
let index = match piece {
|
||||
Piece::Pawn => 0,
|
||||
Piece::Knight => 1,
|
||||
Piece::Bishop => 2,
|
||||
Piece::Rook => 3,
|
||||
Piece::Queen => 4,
|
||||
Piece::King => 5,
|
||||
};
|
||||
|
||||
let color = 0;
|
||||
let texture_rect = PieceTexture::new(index, color);
|
||||
|
||||
DrawTextureParams {
|
||||
source: Some(Rect::new(
|
||||
texture_rect.x,
|
||||
texture_rect.y,
|
||||
texture_rect.w,
|
||||
texture_rect.h,
|
||||
)),
|
||||
dest_size: Some(Vec2::new(sprite_size, sprite_size)),
|
||||
..DrawTextureParams::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-38
@@ -1,7 +1,9 @@
|
||||
mod game;
|
||||
mod resources;
|
||||
mod widgets;
|
||||
|
||||
use game::{Game, sound::Sounds};
|
||||
use macroquad::{audio, prelude::*};
|
||||
use game::Game;
|
||||
use macroquad::prelude::*;
|
||||
use miniquad::date;
|
||||
|
||||
use game::constants;
|
||||
@@ -26,7 +28,8 @@ fn window_conf() -> Conf {
|
||||
async fn main() {
|
||||
rand::srand(date::now() as u64);
|
||||
let background_color = Color::from_rgba(196, 195, 208, 255);
|
||||
let mut game = init().await;
|
||||
let resources = resources::init().await;
|
||||
let mut game = Game::new_game(resources);
|
||||
loop {
|
||||
clear_background(background_color);
|
||||
game.handle_input();
|
||||
@@ -34,38 +37,3 @@ async fn main() {
|
||||
next_frame().await
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! load_sound {
|
||||
($file_name:expr) => {
|
||||
audio::load_sound_from_bytes(include_bytes!($file_name))
|
||||
.await
|
||||
.unwrap()
|
||||
};
|
||||
}
|
||||
|
||||
async fn init() -> Game {
|
||||
let texture_bytes = include_bytes!("../assets/pieces.png");
|
||||
let texture_res = Texture2D::from_file_with_format(&texture_bytes[..], None);
|
||||
texture_res.set_filter(FilterMode::Nearest);
|
||||
build_textures_atlas();
|
||||
|
||||
let click = load_sound!("../assets/click.wav");
|
||||
let win = load_sound!("../assets/win.wav");
|
||||
let loss = load_sound!("../assets/loss.wav");
|
||||
let button = load_sound!("../assets/button.wav");
|
||||
let mode = load_sound!("../assets/mode.wav");
|
||||
let sounds = Sounds {
|
||||
click,
|
||||
win,
|
||||
loss,
|
||||
button,
|
||||
mode,
|
||||
};
|
||||
|
||||
let font_ttf = include_bytes!("../assets/Junction-regular.otf");
|
||||
let Ok(font) = load_ttf_font_from_bytes(font_ttf) else {
|
||||
panic!("Failed to load font");
|
||||
};
|
||||
|
||||
Game::new_game(texture_res, sounds, font)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
use macroquad::{
|
||||
audio::{self, Sound},
|
||||
prelude::*,
|
||||
};
|
||||
use sol_lib::board::piece::Piece;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Resources {
|
||||
texture_pieces: Option<Texture2D>,
|
||||
texture_rects_pieces: HashMap<Piece, Rect>,
|
||||
|
||||
sounds: HashMap<SoundKind, Sound>,
|
||||
|
||||
font: Option<Font>,
|
||||
}
|
||||
|
||||
pub struct Texture<'a> {
|
||||
pub texture: &'a Texture2D,
|
||||
pub texture_rect: Rect,
|
||||
}
|
||||
|
||||
macro_rules! load_sound {
|
||||
($file_name:expr) => {
|
||||
audio::load_sound_from_bytes(include_bytes!($file_name))
|
||||
.await
|
||||
.unwrap()
|
||||
};
|
||||
}
|
||||
|
||||
pub async fn init() -> Resources {
|
||||
// SOUNDS
|
||||
let click = load_sound!("../assets/click.wav");
|
||||
let win = load_sound!("../assets/win.wav");
|
||||
let loss = load_sound!("../assets/loss.wav");
|
||||
let button = load_sound!("../assets/button.wav");
|
||||
let mode = load_sound!("../assets/mode.wav");
|
||||
let mut sounds = HashMap::new();
|
||||
sounds.insert(SoundKind::Click, click);
|
||||
sounds.insert(SoundKind::Win, win);
|
||||
sounds.insert(SoundKind::Loss, loss);
|
||||
sounds.insert(SoundKind::Button, button);
|
||||
sounds.insert(SoundKind::Mode, mode);
|
||||
|
||||
// FONT
|
||||
let font_ttf = include_bytes!("../assets/Junction-regular.otf");
|
||||
let font = load_ttf_font_from_bytes(font_ttf).expect("Failed to load font");
|
||||
|
||||
// TEXTURES
|
||||
let texture_pieces_bytes = include_bytes!("../assets/pieces.png");
|
||||
let texture_pieces = Texture2D::from_file_with_format(&texture_pieces_bytes[..], None);
|
||||
texture_pieces.set_filter(FilterMode::Nearest);
|
||||
build_textures_atlas();
|
||||
|
||||
let mut texture_rects_pieces = HashMap::new();
|
||||
texture_rects_pieces.insert(Piece::Pawn, piece_texture_rect(Piece::Pawn));
|
||||
texture_rects_pieces.insert(Piece::Knight, piece_texture_rect(Piece::Knight));
|
||||
texture_rects_pieces.insert(Piece::Bishop, piece_texture_rect(Piece::Bishop));
|
||||
texture_rects_pieces.insert(Piece::Rook, piece_texture_rect(Piece::Rook));
|
||||
texture_rects_pieces.insert(Piece::Queen, piece_texture_rect(Piece::Queen));
|
||||
texture_rects_pieces.insert(Piece::King, piece_texture_rect(Piece::King));
|
||||
|
||||
Resources {
|
||||
texture_pieces: Some(texture_pieces),
|
||||
texture_rects_pieces,
|
||||
sounds,
|
||||
font: Some(font),
|
||||
}
|
||||
}
|
||||
|
||||
fn piece_texture_rect(piece: Piece) -> Rect {
|
||||
let index = match piece {
|
||||
Piece::Pawn => 0,
|
||||
Piece::Knight => 1,
|
||||
Piece::Bishop => 2,
|
||||
Piece::Rook => 3,
|
||||
Piece::Queen => 4,
|
||||
Piece::King => 5,
|
||||
};
|
||||
let color = 0;
|
||||
Rect::new(index as f32 * 128.0, color as f32 * 128.0, 128.0, 128.0)
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash)]
|
||||
pub enum SoundKind {
|
||||
Click,
|
||||
Win,
|
||||
Loss,
|
||||
Button,
|
||||
Mode,
|
||||
}
|
||||
|
||||
impl Resources {
|
||||
pub fn get_piece_texture<'a>(&'a self, piece: &Piece) -> Texture<'a> {
|
||||
let texture = self.texture_pieces.as_ref().unwrap();
|
||||
let texture_rect = self.texture_rects_pieces.get(piece).unwrap();
|
||||
Texture {
|
||||
texture: texture,
|
||||
texture_rect: *texture_rect,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn font(&self) -> &Font {
|
||||
self.font.as_ref().unwrap()
|
||||
}
|
||||
|
||||
pub fn sound(&self, kind: &SoundKind) -> &Sound {
|
||||
self.sounds.get(kind).unwrap()
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,11 @@
|
||||
use macroquad::prelude::*;
|
||||
pub mod button;
|
||||
pub mod id_text_input;
|
||||
|
||||
use macroquad::{
|
||||
audio::{self, Sound},
|
||||
prelude::*,
|
||||
};
|
||||
use quad_snd::PlaySoundParams;
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@@ -16,7 +23,7 @@ impl UiColor {
|
||||
match self {
|
||||
UiColor::Grey => Color::from_rgba(140, 140, 140, 200),
|
||||
UiColor::Green => Color::from_rgba(16, 60, 50, 200),
|
||||
UiColor::Pink => Color::from_rgba(234, 128, 71, 200),
|
||||
UiColor::Pink => Color::from_rgba(255, 60, 60, 200),
|
||||
UiColor::Brown => Color::from_rgba(123, 61, 35, 200),
|
||||
UiColor::Yellow => Color::from_rgba(242, 230, 190, 200),
|
||||
UiColor::Blue => Color::from_rgba(47, 85, 172, 200),
|
||||
@@ -44,3 +51,36 @@ impl UiColor {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw_rect(rect: &Rect, color: UiColor) {
|
||||
draw_rectangle(rect.x, rect.y, rect.w, rect.h, color.to_bg_color());
|
||||
}
|
||||
|
||||
pub fn draw_shadow(rect: &Rect, shadow_width: f32) {
|
||||
let shadow_color = Color::new(0., 0., 0., 0.8);
|
||||
draw_rectangle(
|
||||
rect.x + rect.w,
|
||||
rect.y + shadow_width,
|
||||
shadow_width,
|
||||
rect.h,
|
||||
shadow_color,
|
||||
);
|
||||
|
||||
draw_rectangle(
|
||||
rect.x + shadow_width,
|
||||
rect.y + rect.h,
|
||||
rect.w - shadow_width,
|
||||
shadow_width,
|
||||
shadow_color,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn play_sound_once(sound: &Sound, volume: f32) {
|
||||
audio::play_sound(
|
||||
sound,
|
||||
PlaySoundParams {
|
||||
looped: false,
|
||||
volume,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,52 +1,36 @@
|
||||
use super::{color::UiColor, shadow::draw_shadow, sound::Sounds};
|
||||
use macroquad::{audio::Sound, prelude::*};
|
||||
use std::rc::Rc;
|
||||
use crate::widgets::*;
|
||||
|
||||
pub struct Button {
|
||||
pub is_active: bool,
|
||||
pub text: String,
|
||||
is_down: bool,
|
||||
is_clicked: bool,
|
||||
rect: Rect,
|
||||
shadow_width: f32,
|
||||
pub color: UiColor,
|
||||
sound: Sound,
|
||||
font: Rc<Font>,
|
||||
}
|
||||
|
||||
pub struct ButtonInteraction {
|
||||
pub is_clicked: bool,
|
||||
}
|
||||
|
||||
impl Button {
|
||||
pub fn new(text: &str, rect: Rect, color: UiColor, sound: Sound, font: Rc<Font>) -> Self {
|
||||
pub fn new(rect: Rect) -> Self {
|
||||
Self {
|
||||
text: text.to_string(),
|
||||
is_down: false,
|
||||
is_clicked: false,
|
||||
is_active: true,
|
||||
rect,
|
||||
shadow_width: 5.0,
|
||||
color,
|
||||
sound,
|
||||
font,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_clicked(&mut self) -> bool {
|
||||
if self.is_clicked {
|
||||
self.is_clicked = false;
|
||||
return true;
|
||||
pub fn draw(&self, text: &str, color: &UiColor, font: &Font) {
|
||||
self.draw_button(color);
|
||||
self.draw_label(text, font, color);
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn draw(&self) {
|
||||
self.draw_button();
|
||||
self.draw_label();
|
||||
}
|
||||
|
||||
fn draw_button(&self) {
|
||||
fn draw_button(&self, color: &UiColor) {
|
||||
let bg_color = match self.is_active {
|
||||
true => self.color.to_bg_color(),
|
||||
false => self.color.to_shadow_color(),
|
||||
true => color.to_bg_color(),
|
||||
false => color.to_shadow_color(),
|
||||
};
|
||||
let button_draw_offset = self.get_button_draw_offset();
|
||||
draw_rectangle(
|
||||
@@ -69,27 +53,27 @@ impl Button {
|
||||
return;
|
||||
}
|
||||
|
||||
draw_shadow(self.rect, self.shadow_width);
|
||||
draw_shadow(&self.rect, self.shadow_width);
|
||||
}
|
||||
|
||||
fn draw_label(&self) {
|
||||
fn draw_label(&self, text: &str, font: &Font, color: &UiColor) {
|
||||
let font_color = match self.is_active {
|
||||
true => self.color.to_fg_color(),
|
||||
true => color.to_fg_color(),
|
||||
false => Color::from_rgba(100, 100, 100, 255),
|
||||
};
|
||||
|
||||
let font_size = (0.2 * self.rect.w) as u16;
|
||||
let dims = measure_text(&self.text, Some(&self.font), font_size, 1.0);
|
||||
let dims = measure_text(&text, Some(&font), font_size, 1.0);
|
||||
let button_draw_offset = self.get_button_draw_offset();
|
||||
|
||||
let text_params = TextParams {
|
||||
font_size: font_size as u16,
|
||||
color: font_color,
|
||||
font: Some(&self.font),
|
||||
font: Some(&font),
|
||||
..Default::default()
|
||||
};
|
||||
draw_text_ex(
|
||||
&self.text,
|
||||
&text,
|
||||
self.rect.x + (self.rect.w - dims.width) * 0.5 + button_draw_offset,
|
||||
self.rect.y + (self.rect.h - dims.height) * 0.5 + dims.offset_y + button_draw_offset,
|
||||
text_params,
|
||||
@@ -107,10 +91,10 @@ impl Button {
|
||||
button_pressed_correction
|
||||
}
|
||||
|
||||
pub fn handle_input(&mut self) {
|
||||
pub fn handle_input(&mut self, click_sound: &Sound, volume: f32) -> ButtonInteraction {
|
||||
if !self.is_active {
|
||||
self.is_down = false;
|
||||
return;
|
||||
return ButtonInteraction { is_clicked: false };
|
||||
}
|
||||
|
||||
let (mx, my) = mouse_position();
|
||||
@@ -119,19 +103,31 @@ impl Button {
|
||||
if is_mouse_button_pressed(MouseButton::Left) {
|
||||
if c.overlaps_rect(&self.rect) {
|
||||
self.is_down = true;
|
||||
return;
|
||||
return ButtonInteraction { is_clicked: false };
|
||||
}
|
||||
}
|
||||
|
||||
if is_mouse_button_released(MouseButton::Left) {
|
||||
if c.overlaps_rect(&self.rect) {
|
||||
self.is_clicked = true;
|
||||
Sounds::play(&self.sound);
|
||||
play_sound_once(&click_sound, volume);
|
||||
self.is_down = false;
|
||||
return;
|
||||
return ButtonInteraction { is_clicked: true };
|
||||
}
|
||||
|
||||
self.is_down = false;
|
||||
}
|
||||
|
||||
return ButtonInteraction { is_clicked: false };
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Button {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
is_active: Default::default(),
|
||||
is_down: Default::default(),
|
||||
rect: Default::default(),
|
||||
shadow_width: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
use crate::widgets::*;
|
||||
use macroquad::prelude::*;
|
||||
|
||||
pub struct IdTextInput {
|
||||
rect: Rect,
|
||||
text_rect: Rect,
|
||||
copy_box_rect: Rect,
|
||||
paste_box_rect: Rect,
|
||||
shadow_width: f32,
|
||||
color: UiColor,
|
||||
copy_color: UiColor,
|
||||
paste_color: UiColor,
|
||||
}
|
||||
|
||||
impl IdTextInput {
|
||||
pub fn new(rect: Rect) -> Self {
|
||||
let copy_button_w = 0.25 * rect.w;
|
||||
let paste_button_h = 0.5 * rect.h;
|
||||
Self {
|
||||
rect,
|
||||
text_rect: Rect::new(rect.x, rect.y, rect.w - copy_button_w, rect.h),
|
||||
copy_box_rect: Rect::new(
|
||||
rect.x + (rect.w - copy_button_w),
|
||||
rect.y,
|
||||
copy_button_w,
|
||||
paste_button_h,
|
||||
),
|
||||
paste_box_rect: Rect::new(
|
||||
rect.x + (rect.w - copy_button_w),
|
||||
rect.y + paste_button_h,
|
||||
copy_button_w,
|
||||
paste_button_h,
|
||||
),
|
||||
shadow_width: 3.0,
|
||||
color: UiColor::Yellow,
|
||||
copy_color: UiColor::Blue,
|
||||
paste_color: UiColor::Pink,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&self, font: &Font, text: &str) {
|
||||
draw_rect(&self.text_rect, self.color);
|
||||
draw_rect(&self.copy_box_rect, self.copy_color);
|
||||
draw_rect(&self.paste_box_rect, self.paste_color);
|
||||
draw_shadow(&self.rect, self.shadow_width);
|
||||
|
||||
let font_size = (0.15 * self.text_rect.w) as u16;
|
||||
let text_dims = measure_text(text, Some(font), font_size, 1.0);
|
||||
draw_text_ex(
|
||||
&text,
|
||||
self.rect.x + (self.text_rect.w - text_dims.width) / 2.,
|
||||
self.rect.y + (self.text_rect.h - text_dims.height) / 2. + text_dims.offset_y,
|
||||
TextParams {
|
||||
font: Some(font),
|
||||
font_size: font_size,
|
||||
color: self.color.to_fg_color(),
|
||||
..Default::default()
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for IdTextInput {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
rect: Default::default(),
|
||||
text_rect: Default::default(),
|
||||
copy_box_rect: Default::default(),
|
||||
paste_box_rect: Default::default(),
|
||||
shadow_width: Default::default(),
|
||||
color: UiColor::Grey,
|
||||
copy_color: UiColor::Grey,
|
||||
paste_color: UiColor::Grey,
|
||||
}
|
||||
}
|
||||
}
|
||||
+62
-1
@@ -19,7 +19,7 @@ use square::{Square, SquarePair};
|
||||
|
||||
use crate::generator::Puzzle;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Clone, Default)]
|
||||
pub struct Board {
|
||||
pub cells: [[Option<Piece>; BOARD_SIZE]; BOARD_SIZE],
|
||||
pub legal_moves: HashSet<CMove>,
|
||||
@@ -37,6 +37,12 @@ pub enum BoardState {
|
||||
Won,
|
||||
}
|
||||
|
||||
impl Default for BoardState {
|
||||
fn default() -> Self {
|
||||
BoardState::NotStarted
|
||||
}
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub fn new() -> Self {
|
||||
let cells = [[None; BOARD_SIZE]; BOARD_SIZE];
|
||||
@@ -714,4 +720,59 @@ mod tests {
|
||||
|
||||
validate_board!(board2, "..NB", "....", "RQ.K", "P...");
|
||||
}
|
||||
|
||||
macro_rules! sq {
|
||||
($sq:literal) => {
|
||||
Square::parse($sq)
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solver_smoke() {
|
||||
let mut board = Board::new();
|
||||
// . R . .
|
||||
// R . . P
|
||||
// B . B N
|
||||
// P . N .
|
||||
|
||||
board.set(sq!("Pa1"));
|
||||
board.set(sq!("Ba2"));
|
||||
board.set(sq!("Ra3"));
|
||||
board.set(sq!("Rb4"));
|
||||
board.set(sq!("Nc1"));
|
||||
board.set(sq!("Bc2"));
|
||||
board.set(sq!("Nd2"));
|
||||
board.set(sq!("Pd3"));
|
||||
|
||||
let solutions = board.solve().solutions;
|
||||
|
||||
for solution in solutions {
|
||||
let mut board = board.clone();
|
||||
solution
|
||||
.into_iter()
|
||||
.for_each(|m| assert!(board.make_move(m).is_some()));
|
||||
assert_eq!(BoardState::Won, board.game_state);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn solver_smoke_no_solution() {
|
||||
// . R . .
|
||||
// R . . .
|
||||
// B . B N
|
||||
// P . N .
|
||||
|
||||
let mut board = Board::new();
|
||||
board.set(sq!("Pa1"));
|
||||
board.set(sq!("Ba2"));
|
||||
board.set(sq!("Ra3"));
|
||||
board.set(sq!("Rb4"));
|
||||
board.set(sq!("Nc1"));
|
||||
board.set(sq!("Bc2"));
|
||||
board.set(sq!("Nd2"));
|
||||
|
||||
let solutions = board.solve().solutions;
|
||||
|
||||
assert_eq!(0, solutions.len());
|
||||
}
|
||||
}
|
||||
|
||||
+29
-18
@@ -1,12 +1,31 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use crate::board::{cmove::CMove, piece::Piece, Board};
|
||||
use crate::board::{Board, cmove::CMove, piece::Piece};
|
||||
|
||||
pub trait RandomRange {
|
||||
fn gen_range(&self, min: usize, max: usize) -> usize;
|
||||
}
|
||||
|
||||
pub fn generate(num_pieces: u32, num_solutions: u32, rand: &impl RandomRange) -> GenerateStats {
|
||||
#[derive(Default)]
|
||||
pub struct Puzzle {
|
||||
pub board: Board,
|
||||
pub solutions: Vec<Vec<CMove>>,
|
||||
pub solved: bool,
|
||||
}
|
||||
|
||||
pub struct GenerateStats {
|
||||
piece_total: u32,
|
||||
piece_success: u32,
|
||||
total: u32,
|
||||
board: Option<Board>,
|
||||
solutions: Vec<Vec<CMove>>,
|
||||
}
|
||||
|
||||
pub fn generate_weighted_random(
|
||||
num_pieces: u32,
|
||||
num_solutions: u32,
|
||||
rand: &impl RandomRange,
|
||||
) -> GenerateStats {
|
||||
let candidate_pieces = vec![
|
||||
Piece::Pawn,
|
||||
Piece::Pawn,
|
||||
@@ -47,20 +66,6 @@ pub fn generate(num_pieces: u32, num_solutions: u32, rand: &impl RandomRange) ->
|
||||
overall_stats
|
||||
}
|
||||
|
||||
pub struct Puzzle {
|
||||
pub board: Board,
|
||||
pub solutions: Vec<Vec<CMove>>,
|
||||
pub solved: bool,
|
||||
}
|
||||
|
||||
pub struct GenerateStats {
|
||||
piece_total: u32,
|
||||
piece_success: u32,
|
||||
total: u32,
|
||||
board: Option<Board>,
|
||||
solutions: Vec<Vec<CMove>>,
|
||||
}
|
||||
|
||||
impl GenerateStats {
|
||||
fn new(
|
||||
piece_total: u32,
|
||||
@@ -153,7 +158,13 @@ fn try_generate(
|
||||
if puzzle.solutions.len() > num_solutions as usize {
|
||||
GenerateStats::new(piece_total, piece_success, 1, None, vec![])
|
||||
} else {
|
||||
GenerateStats::new(piece_total, piece_success, 1, Some(puzzle.board), puzzle.solutions)
|
||||
GenerateStats::new(
|
||||
piece_total,
|
||||
piece_success,
|
||||
1,
|
||||
Some(puzzle.board),
|
||||
puzzle.solutions,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,7 +186,7 @@ mod tests {
|
||||
#[test]
|
||||
fn generator_smoke() {
|
||||
for _ in 0..10 {
|
||||
let gen_stats = generate(5, 5, &TestRandom);
|
||||
let gen_stats = generate_weighted_random(5, 5, &TestRandom);
|
||||
let board = gen_stats.board.expect("No puzzle was generated");
|
||||
assert_eq!(board.game_state, BoardState::InProgress);
|
||||
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
pub mod board;
|
||||
pub mod generator;
|
||||
pub mod solver;
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
// #[cfg(test)]
|
||||
// mod tests {
|
||||
// use super::*;
|
||||
// use crate::board::{square::Square, Board};
|
||||
//
|
||||
// macro_rules! sq {
|
||||
// ($sq:literal) => {
|
||||
// Square::parse($sq)
|
||||
// };
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn solver_smoke() {
|
||||
// let mut board = Board::new();
|
||||
// // . R . .
|
||||
// // R . . P
|
||||
// // B . B N
|
||||
// // P . N .
|
||||
//
|
||||
// board.set(sq!("Pa1"));
|
||||
// board.set(sq!("Ba2"));
|
||||
// board.set(sq!("Ra3"));
|
||||
// board.set(sq!("Rb4"));
|
||||
// board.set(sq!("Nc1"));
|
||||
// board.set(sq!("Bc2"));
|
||||
// board.set(sq!("Nd2"));
|
||||
// board.set(sq!("Pd3"));
|
||||
//
|
||||
// let solver = Solver::new(board.clone());
|
||||
// let solutions = solver.solve();
|
||||
//
|
||||
// for solution in solutions {
|
||||
// let mut board = board.clone();
|
||||
// solution
|
||||
// .into_iter()
|
||||
// .for_each(|m| assert!(board.make_move(m).is_some()));
|
||||
// assert_eq!(BoardState::Won, board.game_state);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// #[test]
|
||||
// fn solver_smoke_no_solution() {
|
||||
// // . R . .
|
||||
// // R . . .
|
||||
// // B . B N
|
||||
// // P . N .
|
||||
//
|
||||
// let mut board = Board::new();
|
||||
// board.set(sq!("Pa1"));
|
||||
// board.set(sq!("Ba2"));
|
||||
// board.set(sq!("Ra3"));
|
||||
// board.set(sq!("Rb4"));
|
||||
// board.set(sq!("Nc1"));
|
||||
// board.set(sq!("Bc2"));
|
||||
// board.set(sq!("Nd2"));
|
||||
//
|
||||
// let solver = Solver::new(board.clone());
|
||||
// let solutions = solver.solve();
|
||||
//
|
||||
// assert_eq!(0, solutions.len());
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user