move to workspace mode
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "sol_chess"
|
||||
version = "0.0.1"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
sol_lib = { path = "../lib" }
|
||||
macroquad = { workspace = true }
|
||||
quad-snd = { workspace = true }
|
||||
|
||||
[profile.release]
|
||||
opt-level = 's'
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
panic = 'abort'
|
||||
strip = true
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 251 KiB |
Binary file not shown.
@@ -0,0 +1,94 @@
|
||||
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 macroquad::prelude::*;
|
||||
use sol_lib::{board::Board, generator::Puzzle};
|
||||
use sound::Sounds;
|
||||
|
||||
pub struct Game {
|
||||
// The generated puzzle. We keep a copy of this to reset the game.
|
||||
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,
|
||||
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>,
|
||||
}
|
||||
|
||||
struct GameSquare {
|
||||
rect: Rect,
|
||||
color: Color,
|
||||
is_source: bool,
|
||||
is_target: bool,
|
||||
is_previous_target: bool,
|
||||
i: usize,
|
||||
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,
|
||||
Medium,
|
||||
Hard,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
enum GameState {
|
||||
SelectSource(Option<(usize, usize)>),
|
||||
SelectTarget((usize, usize)),
|
||||
GameOver((usize, usize)),
|
||||
}
|
||||
|
||||
impl Display for GameState {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
GameState::SelectSource(Some(x)) => write!(f, "Select Source [ {}, {} ]", x.0, x.1),
|
||||
GameState::SelectSource(None) => write!(f, "Select Source [ ]"),
|
||||
GameState::SelectTarget(x) => write!(f, "Select Target [ {}, {} ]", x.0, x.1),
|
||||
GameState::GameOver(x) => write!(f, "Game Over [ {}, {} ]", x.0, x.1),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
use super::{color::UiColor, shadow::draw_shadow, sound::Sounds};
|
||||
use macroquad::{audio::Sound, prelude::*};
|
||||
use std::rc::Rc;
|
||||
|
||||
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>,
|
||||
}
|
||||
|
||||
impl Button {
|
||||
pub fn new(text: &str, rect: Rect, color: UiColor, sound: Sound, font: Rc<Font>) -> 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;
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn draw(&self) {
|
||||
self.draw_button();
|
||||
self.draw_label();
|
||||
}
|
||||
|
||||
fn draw_button(&self) {
|
||||
let bg_color = match self.is_active {
|
||||
true => self.color.to_bg_color(),
|
||||
false => self.color.to_shadow_color(),
|
||||
};
|
||||
let button_draw_offset = self.get_button_draw_offset();
|
||||
draw_rectangle(
|
||||
self.rect.x + button_draw_offset,
|
||||
self.rect.y + button_draw_offset,
|
||||
self.rect.w,
|
||||
self.rect.h,
|
||||
bg_color,
|
||||
);
|
||||
|
||||
self.draw_shadow();
|
||||
}
|
||||
|
||||
fn draw_shadow(&self) {
|
||||
if !self.is_active {
|
||||
return;
|
||||
}
|
||||
|
||||
if self.is_down {
|
||||
return;
|
||||
}
|
||||
|
||||
draw_shadow(self.rect, self.shadow_width);
|
||||
}
|
||||
|
||||
fn draw_label(&self) {
|
||||
let font_color = match self.is_active {
|
||||
true => self.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 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),
|
||||
..Default::default()
|
||||
};
|
||||
draw_text_ex(
|
||||
&self.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,
|
||||
);
|
||||
}
|
||||
|
||||
fn get_button_draw_offset(&self) -> f32 {
|
||||
let button_pressed_correction = match self.is_down {
|
||||
true => self.shadow_width,
|
||||
false => match self.is_active {
|
||||
true => 0.0,
|
||||
false => self.shadow_width,
|
||||
},
|
||||
};
|
||||
button_pressed_correction
|
||||
}
|
||||
|
||||
pub fn handle_input(&mut self) {
|
||||
if !self.is_active {
|
||||
self.is_down = false;
|
||||
return;
|
||||
}
|
||||
|
||||
let (mx, my) = mouse_position();
|
||||
let c = Circle::new(mx, my, 0.0);
|
||||
|
||||
if is_mouse_button_pressed(MouseButton::Left) {
|
||||
if c.overlaps_rect(&self.rect) {
|
||||
self.is_down = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if is_mouse_button_released(MouseButton::Left) {
|
||||
if c.overlaps_rect(&self.rect) {
|
||||
self.is_clicked = true;
|
||||
Sounds::play(&self.sound);
|
||||
self.is_down = false;
|
||||
return;
|
||||
}
|
||||
|
||||
self.is_down = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use macroquad::prelude::*;
|
||||
|
||||
#[allow(unused)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum UiColor {
|
||||
Grey,
|
||||
Green,
|
||||
Pink,
|
||||
Brown,
|
||||
Yellow,
|
||||
Blue,
|
||||
}
|
||||
|
||||
impl UiColor {
|
||||
pub fn to_bg_color(&self) -> Color {
|
||||
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::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),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_fg_color(&self) -> Color {
|
||||
match self {
|
||||
UiColor::Grey => Color::from_rgba(255, 255, 255, 200),
|
||||
UiColor::Green => Color::from_rgba(255, 255, 255, 200),
|
||||
UiColor::Pink => Color::from_rgba(255, 255, 255, 200),
|
||||
UiColor::Brown => Color::from_rgba(255, 255, 255, 200),
|
||||
UiColor::Yellow => Color::from_rgba(0, 0, 0, 200),
|
||||
UiColor::Blue => Color::from_rgba(255, 255, 255, 200),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn to_shadow_color(&self) -> Color {
|
||||
let bg_color = self.to_bg_color();
|
||||
Color::from_rgba(
|
||||
(bg_color.r * 255.) as u8,
|
||||
(bg_color.g * 255.) as u8,
|
||||
(bg_color.b * 255.) as u8,
|
||||
100,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
pub const WINDOW_TITLE: &str = "Solitaire Chess";
|
||||
pub const HEADING_TEXT: &str = "Solitaire Chess";
|
||||
pub const VOLUME: f32 = 0.1; // Between 0 and 1
|
||||
pub const SCREEN_HEIGHT_MIN: f32 = 200.0;
|
||||
pub const SCREEN_HEIGHT_MAX: f32 = 10000.0;
|
||||
pub const SCREEN_WIDTH_MIN: f32 = 200.0;
|
||||
pub const SCREEN_WIDTH_MAX: f32 = 10000.0;
|
||||
|
||||
// How big the square should be relative to the screen size, between 0 and 1
|
||||
pub const BOARD_SQUARE_WIDTH_MULTIPLIER: f32 = 0.15;
|
||||
|
||||
pub const BOARD_SHADOW_MULTIPLIER: f32 = 0.1;
|
||||
|
||||
pub const BOARD_PIECE_WIDTH_MULTIPLIER: f32 = 0.8;
|
||||
|
||||
// How big the heading text font size should be relative to the screen size, between 0 and 1
|
||||
pub const HEADING_FONT_SIZE_MULTIPLIER: f32 = 0.07;
|
||||
|
||||
// Height of the button relative to the screen size.
|
||||
pub const BUTTON_HEIGHT_MULTIPLIER: f32 = 0.08;
|
||||
|
||||
// Width of the button relative to the 'board size'
|
||||
pub const BUTTON_WIDTH_MULTIPLIER: f32 = 0.20;
|
||||
|
||||
// Gap between the bottom of the row and the first row of buttons
|
||||
pub const BOTTOM_BUTTON_ROW_OFFSET_MULTIPLIER: f32 = 0.3;
|
||||
|
||||
pub const RESET_BUTTON_TEXT: &str = "RESET";
|
||||
pub const NEXT_BUTTON_TEXT: &str = "NEXT";
|
||||
pub const RULES_BUTTON_TEXT: &str = "RULES";
|
||||
pub const RULES_BUTTON_ALT_TEXT: &str = "CLOSE";
|
||||
pub const EASY_BUTTON_TEXT: &str = "EASY";
|
||||
pub const MEDIUM_BUTTON_TEXT: &str = "MEDIUM";
|
||||
pub const HARD_BUTTON_TEXT: &str = "HARD";
|
||||
@@ -0,0 +1,336 @@
|
||||
use super::{
|
||||
ButtonAction, Game, GameMode, button::Button, color::UiColor, constants, shadow,
|
||||
texture::PieceTexture,
|
||||
};
|
||||
use macroquad::{math, prelude::*};
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Game {
|
||||
pub fn draw(&mut self) {
|
||||
self.update_window_size();
|
||||
self.draw_heading();
|
||||
self.draw_board();
|
||||
self.draw_buttons();
|
||||
self.draw_debug();
|
||||
}
|
||||
|
||||
fn update_window_size(&mut self) {
|
||||
let new_height = math::clamp(
|
||||
screen_height(),
|
||||
constants::SCREEN_HEIGHT_MIN,
|
||||
constants::SCREEN_HEIGHT_MAX,
|
||||
);
|
||||
let new_width = math::clamp(
|
||||
screen_width(),
|
||||
constants::SCREEN_WIDTH_MIN,
|
||||
constants::SCREEN_WIDTH_MAX,
|
||||
);
|
||||
|
||||
if new_height == self.window_height && new_width == self.window_width {
|
||||
return;
|
||||
}
|
||||
|
||||
self.window_height = new_height;
|
||||
self.window_width = new_width;
|
||||
self.initialize_drawables();
|
||||
}
|
||||
|
||||
fn initialize_drawables(&mut self) {
|
||||
let min_dimension = f32::min(self.window_height, self.window_width);
|
||||
self.square_width = constants::BOARD_SQUARE_WIDTH_MULTIPLIER * min_dimension;
|
||||
let board_width = self.square_width * self.num_squares as f32;
|
||||
let board_x = (self.window_width - board_width) / 2.0;
|
||||
let board_y = (self.window_height - board_width) / 2.0;
|
||||
self.board_rect = Rect::new(board_x, board_y, board_width, board_width);
|
||||
|
||||
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);
|
||||
self.heading_rect = Rect::new(
|
||||
board_x + (board_width - dims.width) / 2.0,
|
||||
(board_y - dims.height) / 2.0,
|
||||
dims.width,
|
||||
dims.height,
|
||||
);
|
||||
|
||||
let dark = UiColor::Brown.to_bg_color();
|
||||
let light = UiColor::Yellow.to_bg_color();
|
||||
let mut rects = Vec::new();
|
||||
for i in 0..self.num_squares {
|
||||
for j in 0..self.num_squares {
|
||||
let x_eff = board_x + (i as f32 * self.square_width);
|
||||
let y_eff = board_y + (j as f32 * self.square_width);
|
||||
let rect = Rect::new(x_eff, y_eff, self.square_width, self.square_width);
|
||||
let color = match (i + j) % 2 {
|
||||
1 => dark,
|
||||
_ => light,
|
||||
};
|
||||
|
||||
rects.push(super::GameSquare {
|
||||
rect,
|
||||
color,
|
||||
i,
|
||||
j,
|
||||
is_source: false,
|
||||
is_target: false,
|
||||
is_previous_target: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
self.squares = rects;
|
||||
|
||||
let btn_h = constants::BUTTON_HEIGHT_MULTIPLIER * min_dimension;
|
||||
let btn_w = board_width * constants::BUTTON_WIDTH_MULTIPLIER;
|
||||
let btn_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, btn_y, btn_w, btn_h),
|
||||
UiColor::Yellow,
|
||||
self.sounds.button.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
|
||||
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, btn_y, btn_w, btn_h),
|
||||
UiColor::Green,
|
||||
self.sounds.button.clone(),
|
||||
self.font.clone(),
|
||||
);
|
||||
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 rules_button = Button::new(
|
||||
constants::RULES_BUTTON_TEXT,
|
||||
Rect::new(
|
||||
(board_x - btn_w) / 2.,
|
||||
board_y + (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);
|
||||
|
||||
let easy_btn = Button::new(
|
||||
constants::EASY_BUTTON_TEXT,
|
||||
Rect::new(
|
||||
(board_x - btn_w) / 2.,
|
||||
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(
|
||||
(board_x - btn_w) / 2.,
|
||||
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(
|
||||
(board_x - btn_w) / 2.,
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 draw_text_params = TextParams {
|
||||
font_size: f,
|
||||
font: Some(&self.font),
|
||||
color: BLACK,
|
||||
..Default::default()
|
||||
};
|
||||
draw_text_ex(
|
||||
self.heading_text.as_str(),
|
||||
self.heading_rect.x,
|
||||
self.heading_rect.y + dims.offset_y,
|
||||
draw_text_params,
|
||||
);
|
||||
}
|
||||
|
||||
fn draw_board(&self) {
|
||||
let board_shadow_width = constants::BOARD_SHADOW_MULTIPLIER * self.square_width;
|
||||
shadow::draw_shadow(self.board_rect, board_shadow_width);
|
||||
|
||||
if self.rules {
|
||||
draw_rectangle(
|
||||
self.board_rect.x,
|
||||
self.board_rect.y,
|
||||
self.board_rect.w,
|
||||
self.board_rect.h,
|
||||
UiColor::Yellow.to_bg_color(),
|
||||
);
|
||||
|
||||
let font_size = self.heading_font_size * 0.6;
|
||||
let rules = "\
|
||||
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 draw_text_params = TextParams {
|
||||
font_size: font_size as u16,
|
||||
font: Some(&self.font),
|
||||
color: UiColor::Brown.to_bg_color(),
|
||||
..Default::default()
|
||||
};
|
||||
draw_multiline_text_ex(
|
||||
rules,
|
||||
self.board_rect.x + 0.05 * self.square_width,
|
||||
self.board_rect.y + 0.5 * (self.board_rect.h - measurement.height)
|
||||
- 2. * measurement.offset_y,
|
||||
Some(2.),
|
||||
draw_text_params,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let sprite_size = constants::BOARD_PIECE_WIDTH_MULTIPLIER * self.square_width;
|
||||
let mut selected_square = None;
|
||||
self.squares.iter().for_each(|square| {
|
||||
let color = match square.is_source {
|
||||
true => square.color,
|
||||
false => match square.is_target {
|
||||
true => UiColor::Pink.to_shadow_color(),
|
||||
false => square.color,
|
||||
},
|
||||
};
|
||||
|
||||
draw_rectangle(
|
||||
square.rect.x,
|
||||
square.rect.y,
|
||||
square.rect.w,
|
||||
square.rect.h,
|
||||
color,
|
||||
);
|
||||
|
||||
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);
|
||||
if !square.is_source {
|
||||
draw_texture_ex(
|
||||
&self.texture_res,
|
||||
square.rect.x + offset,
|
||||
square.rect.y + offset,
|
||||
WHITE,
|
||||
dtp,
|
||||
);
|
||||
} else {
|
||||
selected_square = Some(square);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
draw_texture_ex(
|
||||
&self.texture_res,
|
||||
mouse_position().0 - sprite_size / 2.0,
|
||||
mouse_position().1 - sprite_size / 2.0,
|
||||
WHITE,
|
||||
dtp,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_buttons(&mut self) {
|
||||
for btn in &self.gp_btns {
|
||||
btn.1.draw();
|
||||
}
|
||||
|
||||
for btn in &self.mode_btns {
|
||||
btn.1.draw();
|
||||
}
|
||||
|
||||
if let Some(btn) = &self.rules_btn {
|
||||
btn.draw();
|
||||
}
|
||||
}
|
||||
|
||||
fn draw_debug(&self) {
|
||||
if self.debug {
|
||||
let mut debug_lines = vec![];
|
||||
let (mx, my) = mouse_position();
|
||||
let hover_square = self.squares.iter().find(|s| {
|
||||
let c = Circle::new(mx, my, 0.0);
|
||||
if c.overlaps_rect(&s.rect) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
debug_lines.push(format!("Game State: {}", self.state));
|
||||
debug_lines.push(format!("Board State: {}", self.current_board.game_state));
|
||||
if let Some(hover_square) = hover_square {
|
||||
debug_lines.push(format!("Hover: [ {}, {} ]", hover_square.i, hover_square.j));
|
||||
}
|
||||
self.add_debug_info(debug_lines);
|
||||
|
||||
self.show_fps();
|
||||
}
|
||||
}
|
||||
|
||||
fn add_debug_info(&self, lines: Vec<String>) {
|
||||
let mut y = 20.0;
|
||||
for line in lines {
|
||||
draw_text(&line, 10.0, y, 20.0, BLACK);
|
||||
y += 25.0;
|
||||
}
|
||||
}
|
||||
|
||||
fn show_fps(&self) {
|
||||
let fps = get_fps();
|
||||
draw_text(
|
||||
&format!("FPS: {}", fps),
|
||||
10.0,
|
||||
screen_height() - 20.0,
|
||||
20.0,
|
||||
BLACK,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
use super::{ButtonAction, Game, GameMode, GameSquare, GameState, constants, sound::Sounds};
|
||||
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 {
|
||||
&mut self.squares[i * self.num_squares + j]
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
GameState::SelectTarget(source) => {
|
||||
let next = self.handle_select_target(mouse_position(), source);
|
||||
if let GameState::SelectTarget(_) = next {
|
||||
self.reset_squares();
|
||||
GameState::SelectSource(None)
|
||||
} else {
|
||||
next
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
GameState::SelectTarget(source) => GameState::SelectTarget(source),
|
||||
GameState::GameOver(previous_target) => GameState::GameOver(previous_target),
|
||||
};
|
||||
|
||||
self.state = new_state;
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_select_source(
|
||||
&mut self,
|
||||
mouse_position: (f32, f32),
|
||||
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 let Some(_) = self.current_board.cells[square.i][square.j] {
|
||||
selected = Some((square.i, square.j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((i, j)) = selected {
|
||||
self.get(i, j).is_source = true;
|
||||
let mut target_squares = vec![];
|
||||
for m in self.current_board.legal_moves.iter() {
|
||||
if m.from.file == i && m.from.rank == j {
|
||||
target_squares.push((m.to.file, m.to.rank));
|
||||
}
|
||||
}
|
||||
|
||||
for (i, j) in target_squares {
|
||||
self.get(i, j).is_target = true;
|
||||
}
|
||||
|
||||
return GameState::SelectTarget(selected.unwrap());
|
||||
}
|
||||
|
||||
if let Some((i, j)) = previous_target {
|
||||
self.get(i, j).is_previous_target = true;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
let mut selected = None;
|
||||
for square in &mut self.squares {
|
||||
if mouse.overlaps_rect(&square.rect) {
|
||||
if let Some(_) = self.current_board.cells[square.i][square.j] {
|
||||
selected = Some((square.i, square.j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let (s_x, s_y) = source;
|
||||
let Some((x, y)) = selected else {
|
||||
self.get(s_x, s_y).is_source = true;
|
||||
return GameState::SelectTarget(source);
|
||||
};
|
||||
|
||||
if x == s_x && y == s_y {
|
||||
self.get(s_x, s_y).is_source = true;
|
||||
return GameState::SelectTarget(source);
|
||||
}
|
||||
|
||||
let mut is_legal = false;
|
||||
if self.get(x, y).is_target {
|
||||
is_legal = true;
|
||||
}
|
||||
|
||||
if is_legal {
|
||||
let m = self.current_board.legal_moves.iter().find(|m| {
|
||||
m.from.file == s_x && m.from.rank == s_y && m.to.file == x && m.to.rank == y
|
||||
});
|
||||
|
||||
let m = m.expect("legal move should be found");
|
||||
self.current_board.make_move(m.clone());
|
||||
|
||||
if self.current_board.game_state == BoardState::Won
|
||||
|| self.current_board.game_state == BoardState::Lost
|
||||
{
|
||||
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);
|
||||
} else {
|
||||
Sounds::play(&self.sounds.loss);
|
||||
}
|
||||
|
||||
return GameState::GameOver((x, y));
|
||||
}
|
||||
|
||||
self.reset_squares();
|
||||
self.get(x, y).is_target = true;
|
||||
Sounds::play(&self.sounds.click);
|
||||
return GameState::SelectSource(Some((x, y)));
|
||||
}
|
||||
|
||||
self.reset_squares();
|
||||
return GameState::SelectSource(None);
|
||||
}
|
||||
|
||||
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.state = GameState::SelectSource(None);
|
||||
}
|
||||
|
||||
fn next_puzzle(&mut self) {
|
||||
self.reset();
|
||||
let puzzle = Game::generate_puzzle(self.game_mode);
|
||||
self.current_board = puzzle.board.clone();
|
||||
self.puzzle = puzzle;
|
||||
}
|
||||
|
||||
fn reset_squares(&mut self) {
|
||||
for i in 0..self.num_squares {
|
||||
for j in 0..self.num_squares {
|
||||
self.get(i, j).is_source = false;
|
||||
self.get(i, j).is_target = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_puzzle(mode: GameMode) -> Puzzle {
|
||||
let piece_count = match mode {
|
||||
GameMode::Easy => 3,
|
||||
GameMode::Medium => 5,
|
||||
GameMode::Hard => 7,
|
||||
};
|
||||
|
||||
let generated = generator::generate(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 {
|
||||
let game_mode = GameMode::Medium;
|
||||
let puzzle = Game::generate_puzzle(game_mode);
|
||||
let current_board = puzzle.board.clone();
|
||||
let num_squares: usize = current_board.size;
|
||||
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),
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MacroquadRandAdapter;
|
||||
impl RandomRange for MacroquadRandAdapter {
|
||||
fn gen_range(&self, min: usize, max: usize) -> usize {
|
||||
rand::gen_range(min, max)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
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,
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
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,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
mod game;
|
||||
|
||||
use game::{Game, sound::Sounds};
|
||||
use macroquad::{audio, prelude::*};
|
||||
use miniquad::date;
|
||||
|
||||
use game::constants;
|
||||
|
||||
fn window_conf() -> Conf {
|
||||
let window_title = {
|
||||
if cfg!(debug_assertions) {
|
||||
"Debug: Puzzle Game"
|
||||
} else {
|
||||
constants::WINDOW_TITLE
|
||||
}
|
||||
};
|
||||
|
||||
Conf {
|
||||
window_title: String::from(window_title),
|
||||
fullscreen: false,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[macroquad::main(window_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;
|
||||
loop {
|
||||
clear_background(background_color);
|
||||
game.handle_input();
|
||||
game.draw();
|
||||
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/caskaydia.ttf");
|
||||
let Ok(font) = load_ttf_font_from_bytes(font_ttf) else {
|
||||
panic!("Failed to load font");
|
||||
};
|
||||
|
||||
Game::new_game(texture_res, sounds, font)
|
||||
}
|
||||
Reference in New Issue
Block a user