Major refactor
This commit is contained in:
+54
-123
@@ -1,9 +1,12 @@
|
||||
use super::{Game, constants};
|
||||
use crate::{game::GameMode, widgets::*};
|
||||
use crate::{
|
||||
game::{self, GameMode, w_button, w_counter, w_label},
|
||||
widgets::*,
|
||||
};
|
||||
use macroquad::{math, prelude::*};
|
||||
|
||||
impl Game {
|
||||
fn initialize_drawables(&mut self, new_width: f32, new_height: f32) {
|
||||
fn initialize_scene(&mut self, new_width: f32, new_height: f32) {
|
||||
self.window_height = new_height;
|
||||
self.window_width = new_width;
|
||||
|
||||
@@ -19,7 +22,7 @@ impl Game {
|
||||
let heading_font_size = constants::HEADING_FONT_SIZE_MULTIPLIER * min_dimension;
|
||||
let f = heading_font_size.floor() as u16;
|
||||
let heading_text_dims = measure_text(
|
||||
self.heading_text.as_str(),
|
||||
w_label!(self, label_heading).text.as_str(),
|
||||
Some(&self.resources.font()),
|
||||
f,
|
||||
1.0,
|
||||
@@ -32,8 +35,11 @@ impl Game {
|
||||
heading_text_dims.width,
|
||||
heading_text_dims.height,
|
||||
);
|
||||
self.heading
|
||||
.initialize_drawables(heading_rect, heading_font_size, &self.resources);
|
||||
w_label!(self, label_heading).initialize_drawables(
|
||||
heading_rect,
|
||||
heading_font_size,
|
||||
&self.resources,
|
||||
);
|
||||
|
||||
// Buttons
|
||||
let btn_h = constants::BUTTON_HEIGHT_MULTIPLIER * min_dimension;
|
||||
@@ -42,14 +48,14 @@ impl Game {
|
||||
// Bottom row
|
||||
let bottom_row_y =
|
||||
board_width + board_y + constants::BOTTOM_ROW_OFFSET_MULTIPLIER * square_width;
|
||||
self.rules_btn.initialize_drawables(Rect::new(
|
||||
w_button!(self, btn_rules).place(Rect::new(
|
||||
board_x + (square_width - btn_w) / 2.,
|
||||
bottom_row_y,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
|
||||
self.reset_btn.initialize_drawables(Rect::new(
|
||||
w_button!(self, btn_reset).place(Rect::new(
|
||||
board_x + square_width + (square_width - btn_w) / 2.,
|
||||
bottom_row_y,
|
||||
btn_w,
|
||||
@@ -57,52 +63,42 @@ impl Game {
|
||||
));
|
||||
|
||||
let btn_next_x_offset = (board_width - square_width) + (square_width - btn_w) / 2.;
|
||||
self.next_btn.initialize_drawables(Rect::new(
|
||||
w_button!(self, btn_reset).place(Rect::new(
|
||||
board_x + btn_next_x_offset,
|
||||
bottom_row_y,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
|
||||
let id_text_rect = Rect::new(
|
||||
board_x + 2. * square_width - btn_w,
|
||||
bottom_row_y,
|
||||
btn_w * 2.,
|
||||
btn_h,
|
||||
);
|
||||
self.id_text_btn.initialize_drawables(id_text_rect);
|
||||
|
||||
self.rules_font_size = heading_font_size;
|
||||
|
||||
// Right column
|
||||
let right_column_x =
|
||||
board_x + board_width + constants::RIGHT_COLUMN_OFFSET_MULTIPLIER * square_width;
|
||||
self.get_game_mode_button(&GameMode::Easy)
|
||||
.initialize_drawables(Rect::new(
|
||||
right_column_x,
|
||||
board_y + (square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
self.get_game_mode_button(&GameMode::Easy).place(Rect::new(
|
||||
right_column_x,
|
||||
board_y + (square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
|
||||
self.get_game_mode_button(&GameMode::Medium)
|
||||
.initialize_drawables(Rect::new(
|
||||
.place(Rect::new(
|
||||
right_column_x,
|
||||
board_y + square_width + (square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
|
||||
self.get_game_mode_button(&GameMode::Hard)
|
||||
.initialize_drawables(Rect::new(
|
||||
right_column_x,
|
||||
board_y + 2. * square_width + (square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
self.get_game_mode_button(&GameMode::Hard).place(Rect::new(
|
||||
right_column_x,
|
||||
board_y + 2. * square_width + (square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
btn_h,
|
||||
));
|
||||
|
||||
self.get_game_mode_button(&GameMode::Custom)
|
||||
.initialize_drawables(Rect::new(
|
||||
.place(Rect::new(
|
||||
right_column_x,
|
||||
board_y + 3. * square_width + (square_width - btn_h) / 2.,
|
||||
btn_w,
|
||||
@@ -112,14 +108,14 @@ impl Game {
|
||||
// Right column 2
|
||||
let right_column_2_x =
|
||||
right_column_x + btn_w + constants::RIGHT_COLUMN_OFFSET_MULTIPLIER * square_width;
|
||||
self.generate_btn.initialize_drawables(Rect::new(
|
||||
self.generate_btn.place(Rect::new(
|
||||
right_column_2_x,
|
||||
board_y + board_width - square_width + (square_width - btn_h) / 2.,
|
||||
2. * btn_w,
|
||||
btn_h,
|
||||
));
|
||||
|
||||
self.pieces_label.initialize_drawables(
|
||||
w_label!(self, label_pieces).initialize_drawables(
|
||||
Rect::new(
|
||||
right_column_2_x,
|
||||
board_y + (square_width + btn_h) / 2.,
|
||||
@@ -129,7 +125,8 @@ impl Game {
|
||||
0.30 * btn_h,
|
||||
&self.resources,
|
||||
);
|
||||
self.pieces_counter.initialize_drawables(
|
||||
|
||||
w_counter!(self, counter_pieces).initialize_drawables(
|
||||
Rect::new(
|
||||
right_column_2_x,
|
||||
board_y + square_width + (2. * square_width - 2. * btn_h) / 2.,
|
||||
@@ -142,7 +139,7 @@ impl Game {
|
||||
|
||||
// Right column 3
|
||||
let right_column_3_x = right_column_2_x + btn_w;
|
||||
self.max_age_label.initialize_drawables(
|
||||
w_label!(self, label_max_age).initialize_drawables(
|
||||
Rect::new(
|
||||
right_column_2_x + btn_w,
|
||||
board_y + (square_width + btn_h) / 2.,
|
||||
@@ -152,7 +149,7 @@ impl Game {
|
||||
0.30 * btn_h,
|
||||
&self.resources,
|
||||
);
|
||||
self.max_age_counter.initialize_drawables(
|
||||
w_counter!(self, counter_moves).initialize_drawables(
|
||||
Rect::new(
|
||||
right_column_3_x,
|
||||
board_y + square_width + (2. * square_width - 2. * btn_h) / 2.,
|
||||
@@ -167,16 +164,18 @@ impl Game {
|
||||
pub fn draw(&mut self) {
|
||||
self.update_window_size();
|
||||
|
||||
self.draw_heading();
|
||||
self.draw_board();
|
||||
self.draw_id_text_button();
|
||||
self.draw_buttons();
|
||||
self.draw_setting_controls();
|
||||
self.draw_debug();
|
||||
}
|
||||
|
||||
fn draw_heading(&self) {
|
||||
self.heading.draw(&self.resources);
|
||||
self.generate_btn.draw(&self.resources);
|
||||
for (_, btn) in &self.game_mode_btns {
|
||||
btn.draw(&self.resources);
|
||||
}
|
||||
|
||||
for (_, widget) in &mut self.widgets {
|
||||
widget.draw(&self.resources);
|
||||
}
|
||||
|
||||
self.draw_fps();
|
||||
}
|
||||
|
||||
fn draw_board(&mut self) {
|
||||
@@ -192,87 +191,19 @@ impl Game {
|
||||
self.game_mode_btns.get_mut(mode).unwrap()
|
||||
}
|
||||
|
||||
fn draw_game_mode_button(&mut self, mode: &GameMode, color: &UiColor, text: &str) {
|
||||
let btn = self.game_mode_btns.get_mut(mode).unwrap();
|
||||
btn.draw(text, color, &self.resources);
|
||||
}
|
||||
|
||||
fn draw_buttons(&mut self) {
|
||||
self.reset_btn.draw(
|
||||
constants::RESET_BUTTON_TEXT,
|
||||
&UiColor::Yellow,
|
||||
&self.resources,
|
||||
);
|
||||
|
||||
self.next_btn.draw(
|
||||
constants::NEXT_BUTTON_TEXT,
|
||||
&UiColor::Green,
|
||||
&self.resources,
|
||||
);
|
||||
|
||||
self.draw_game_mode_button(
|
||||
&GameMode::Easy,
|
||||
&UiColor::Yellow,
|
||||
constants::EASY_BUTTON_TEXT,
|
||||
);
|
||||
|
||||
self.draw_game_mode_button(
|
||||
&GameMode::Medium,
|
||||
&UiColor::Yellow,
|
||||
constants::MEDIUM_BUTTON_TEXT,
|
||||
);
|
||||
|
||||
self.draw_game_mode_button(
|
||||
&GameMode::Hard,
|
||||
&UiColor::Yellow,
|
||||
constants::HARD_BUTTON_TEXT,
|
||||
);
|
||||
|
||||
self.draw_game_mode_button(
|
||||
&GameMode::Custom,
|
||||
&UiColor::Blue,
|
||||
constants::CUSTOM_BUTTON_TEXT,
|
||||
);
|
||||
|
||||
self.generate_btn.draw(
|
||||
constants::GENERATE_BUTTON_TEXT,
|
||||
&UiColor::Pink,
|
||||
&self.resources,
|
||||
);
|
||||
|
||||
self.rules_btn
|
||||
.draw(&self.rules_btn_text, &UiColor::Brown, &self.resources);
|
||||
}
|
||||
|
||||
fn draw_setting_controls(&mut self) {
|
||||
self.pieces_label.draw(&self.resources);
|
||||
self.pieces_counter.draw(&self.resources);
|
||||
self.max_age_label.draw(&self.resources);
|
||||
self.max_age_counter.draw(&self.resources);
|
||||
}
|
||||
|
||||
fn draw_id_text_button(&self) {
|
||||
// self.id_text_btn
|
||||
// .draw(&self.puzzle.board.id, &UiColor::Yellow, &self.resources);
|
||||
}
|
||||
|
||||
fn draw_debug(&self) {
|
||||
fn draw_fps(&self) {
|
||||
if self.settings.debug {
|
||||
self.show_fps();
|
||||
let fps = get_fps();
|
||||
draw_text(
|
||||
&format!("FPS: {}", fps),
|
||||
10.0,
|
||||
screen_height() - 20.0,
|
||||
20.0,
|
||||
BLACK,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn show_fps(&self) {
|
||||
let fps = get_fps();
|
||||
draw_text(
|
||||
&format!("FPS: {}", fps),
|
||||
10.0,
|
||||
screen_height() - 20.0,
|
||||
20.0,
|
||||
BLACK,
|
||||
);
|
||||
}
|
||||
|
||||
fn update_window_size(&mut self) {
|
||||
let new_height = math::clamp(
|
||||
screen_height(),
|
||||
@@ -289,6 +220,6 @@ impl Game {
|
||||
return;
|
||||
}
|
||||
|
||||
self.initialize_drawables(new_width, new_height);
|
||||
self.initialize_scene(new_width, new_height);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user