mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
refactor: split update_dimensions into repopulate and update_dimensions
This commit is contained in:
@@ -1,4 +1,7 @@
|
|||||||
use std::ops::{Index, IndexMut};
|
use std::{
|
||||||
|
ops::{Index, IndexMut},
|
||||||
|
slice::IterMut,
|
||||||
|
};
|
||||||
|
|
||||||
use log::info;
|
use log::info;
|
||||||
use macroquad::{
|
use macroquad::{
|
||||||
@@ -29,6 +32,7 @@ struct GuiField {
|
|||||||
pos: Position,
|
pos: Position,
|
||||||
is_clickable: bool,
|
is_clickable: bool,
|
||||||
color: Color,
|
color: Color,
|
||||||
|
is_blac: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -59,6 +63,15 @@ impl<'a> IntoIterator for &'a GuiFieldVec {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoIterator for &'a mut GuiFieldVec {
|
||||||
|
type Item = &'a mut GuiField;
|
||||||
|
type IntoIter = IterMut<'a, GuiField>;
|
||||||
|
|
||||||
|
fn into_iter(self) -> Self::IntoIter {
|
||||||
|
self.gui_fields.iter_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Index<Position> for GuiFieldVec {
|
impl Index<Position> for GuiFieldVec {
|
||||||
type Output = GuiField;
|
type Output = GuiField;
|
||||||
|
|
||||||
@@ -85,6 +98,7 @@ enum BoardState {
|
|||||||
View,
|
View,
|
||||||
ChoosePawn,
|
ChoosePawn,
|
||||||
ChooseMove(Position),
|
ChooseMove(Position),
|
||||||
|
ChooseCapture(Position),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
@@ -99,6 +113,8 @@ pub struct Board {
|
|||||||
|
|
||||||
available_captures: Option<Vec<CapturePath>>,
|
available_captures: Option<Vec<CapturePath>>,
|
||||||
available_moves: Option<Vec<MovePath>>,
|
available_moves: Option<Vec<MovePath>>,
|
||||||
|
|
||||||
|
field_size: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Board {
|
impl Default for Board {
|
||||||
@@ -121,10 +137,15 @@ impl Board {
|
|||||||
|
|
||||||
available_captures: None,
|
available_captures: None,
|
||||||
available_moves: None,
|
available_moves: None,
|
||||||
|
|
||||||
|
field_size: 32.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) {
|
pub fn update(&mut self) {
|
||||||
|
self.update_dimensions();
|
||||||
|
self.reset_color();
|
||||||
|
|
||||||
let clicked_field = self.get_clicked_field();
|
let clicked_field = self.get_clicked_field();
|
||||||
|
|
||||||
match self.state {
|
match self.state {
|
||||||
@@ -140,6 +161,8 @@ impl Board {
|
|||||||
BoardState::ChooseMove(from) => {
|
BoardState::ChooseMove(from) => {
|
||||||
self.highlight_move_paths(from);
|
self.highlight_move_paths(from);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BoardState::ChooseCapture(from) => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,10 +191,11 @@ impl Board {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_dimensions(&mut self) {
|
/// Recreate fields vec with new dimensions. Should be used when new board_view arrives
|
||||||
|
pub fn repopulate(&mut self) {
|
||||||
self.fields.clear();
|
self.fields.clear();
|
||||||
|
|
||||||
let field_size = self.rect.w / self.board_view.size as f32;
|
self.field_size = self.rect.w / self.board_view.size as f32;
|
||||||
|
|
||||||
for field in &self.board_view {
|
for field in &self.board_view {
|
||||||
let pos: Position = field.position;
|
let pos: Position = field.position;
|
||||||
@@ -180,14 +204,14 @@ impl Board {
|
|||||||
let is_column_even: bool = pos.column % 2 == 0;
|
let is_column_even: bool = pos.column % 2 == 0;
|
||||||
let is_field_black: bool = is_row_even ^ is_column_even;
|
let is_field_black: bool = is_row_even ^ is_column_even;
|
||||||
|
|
||||||
let abs_x = self.rect.x + pos.column as f32 * field_size;
|
let abs_x = self.rect.x + pos.column as f32 * self.field_size;
|
||||||
let abs_y = self.rect.y + pos.row as f32 * field_size;
|
let abs_y = self.rect.y + pos.row as f32 * self.field_size;
|
||||||
|
|
||||||
let rect = Rect {
|
let rect = Rect {
|
||||||
x: abs_x,
|
x: abs_x,
|
||||||
y: abs_y,
|
y: abs_y,
|
||||||
w: field_size,
|
w: self.field_size,
|
||||||
h: field_size,
|
h: self.field_size,
|
||||||
};
|
};
|
||||||
|
|
||||||
self.fields.push(GuiField {
|
self.fields.push(GuiField {
|
||||||
@@ -196,10 +220,36 @@ impl Board {
|
|||||||
is_clickable: false,
|
is_clickable: false,
|
||||||
pos,
|
pos,
|
||||||
color: if is_field_black { BLACK } else { WHITE },
|
color: if is_field_black { BLACK } else { WHITE },
|
||||||
|
is_blac: is_field_black,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Updates dimensions of graphical squares
|
||||||
|
pub fn update_dimensions(&mut self) {
|
||||||
|
for field in &mut self.fields {
|
||||||
|
let pos: Position = field.pos;
|
||||||
|
|
||||||
|
let abs_x = self.rect.x + pos.column as f32 * self.field_size;
|
||||||
|
let abs_y = self.rect.y + pos.row as f32 * self.field_size;
|
||||||
|
|
||||||
|
let new_rect = Rect {
|
||||||
|
x: abs_x,
|
||||||
|
y: abs_y,
|
||||||
|
w: self.field_size,
|
||||||
|
h: self.field_size,
|
||||||
|
};
|
||||||
|
|
||||||
|
field.rect = new_rect;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reset_color(&mut self) {
|
||||||
|
for field in &mut self.fields {
|
||||||
|
field.color = if field.is_blac { BLACK } else { WHITE };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn draw(&self) {
|
pub fn draw(&self) {
|
||||||
for field in &self.fields {
|
for field in &self.fields {
|
||||||
draw_rectangle(
|
draw_rectangle(
|
||||||
|
|||||||
@@ -252,7 +252,7 @@ impl GameClient {
|
|||||||
|
|
||||||
ServerMessage::BroadcastBoardState { board } => {
|
ServerMessage::BroadcastBoardState { board } => {
|
||||||
self.board.update_board_view(board);
|
self.board.update_board_view(board);
|
||||||
self.board.update_dimensions();
|
self.board.repopulate();
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::BroadcastCurrentTurn { active_player } => {
|
ServerMessage::BroadcastCurrentTurn { active_player } => {
|
||||||
|
|||||||
Reference in New Issue
Block a user