mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-28 11:10:44 +01:00
feat: center board
This commit is contained in:
@@ -19,16 +19,18 @@ use crate::logic::{
|
|||||||
struct GuiField {
|
struct GuiField {
|
||||||
rect: Rect,
|
rect: Rect,
|
||||||
field: Field,
|
field: Field,
|
||||||
|
pos: Position,
|
||||||
is_clickable: bool,
|
is_clickable: bool,
|
||||||
color: Color,
|
color: Color,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
|
rect: Rect,
|
||||||
|
|
||||||
board_view: BoardView,
|
board_view: BoardView,
|
||||||
fields: Vec<GuiField>,
|
fields: Vec<GuiField>,
|
||||||
|
|
||||||
my_id: Uuid,
|
my_id: Uuid,
|
||||||
current_highlight: Option<Position>,
|
|
||||||
|
|
||||||
available_captures: Option<Vec<CapturePath>>,
|
available_captures: Option<Vec<CapturePath>>,
|
||||||
available_moves: Option<Vec<MovePath>>,
|
available_moves: Option<Vec<MovePath>>,
|
||||||
@@ -36,18 +38,19 @@ pub struct Board {
|
|||||||
|
|
||||||
impl Default for Board {
|
impl Default for Board {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new(Uuid::default())
|
Self::new(Rect::default(), Uuid::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn new(player_id: Uuid) -> Self {
|
pub fn new(rect: Rect, player_id: Uuid) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
rect,
|
||||||
|
|
||||||
board_view: BoardView::default(),
|
board_view: BoardView::default(),
|
||||||
fields: vec![],
|
fields: vec![],
|
||||||
|
|
||||||
my_id: player_id,
|
my_id: player_id,
|
||||||
current_highlight: None,
|
|
||||||
|
|
||||||
available_captures: None,
|
available_captures: None,
|
||||||
available_moves: None,
|
available_moves: None,
|
||||||
@@ -68,10 +71,19 @@ impl Board {
|
|||||||
self.available_captures = None;
|
self.available_captures = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn update_board_rect(&mut self, rect: Rect) -> bool {
|
||||||
|
if self.rect != rect {
|
||||||
|
self.rect = rect;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
pub fn update_state(&mut self) {
|
pub fn update_state(&mut self) {
|
||||||
self.fields.clear();
|
self.fields.clear();
|
||||||
|
|
||||||
let field_size = 32.0;
|
let 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;
|
||||||
@@ -80,8 +92,8 @@ 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 = pos.column as f32 * field_size;
|
let abs_x = self.rect.x + pos.column as f32 * field_size;
|
||||||
let abs_y = pos.row as f32 * field_size;
|
let abs_y = self.rect.y + pos.row as f32 * field_size;
|
||||||
|
|
||||||
let rect = Rect {
|
let rect = Rect {
|
||||||
x: abs_x,
|
x: abs_x,
|
||||||
@@ -94,6 +106,7 @@ impl Board {
|
|||||||
rect,
|
rect,
|
||||||
field: field.clone(),
|
field: field.clone(),
|
||||||
is_clickable: false,
|
is_clickable: false,
|
||||||
|
pos,
|
||||||
color: if is_field_black { BLACK } else { WHITE },
|
color: if is_field_black { BLACK } else { WHITE },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,8 @@ pub struct GameClient {
|
|||||||
game_state: ServerStage,
|
game_state: ServerStage,
|
||||||
|
|
||||||
game_area: Rect,
|
game_area: Rect,
|
||||||
|
board_area: Rect,
|
||||||
|
|
||||||
game_padding: f32,
|
game_padding: f32,
|
||||||
|
|
||||||
lobby: Lobby,
|
lobby: Lobby,
|
||||||
@@ -115,6 +117,8 @@ impl GameClient {
|
|||||||
};
|
};
|
||||||
let game_padding = 25.0;
|
let game_padding = 25.0;
|
||||||
|
|
||||||
|
let board_area = Rect::default();
|
||||||
|
|
||||||
let chat = Chat::default();
|
let chat = Chat::default();
|
||||||
|
|
||||||
Some(Self {
|
Some(Self {
|
||||||
@@ -133,6 +137,8 @@ impl GameClient {
|
|||||||
game_state: game_stage,
|
game_state: game_stage,
|
||||||
|
|
||||||
game_area,
|
game_area,
|
||||||
|
board_area,
|
||||||
|
|
||||||
game_padding,
|
game_padding,
|
||||||
|
|
||||||
chat,
|
chat,
|
||||||
@@ -228,7 +234,7 @@ impl GameClient {
|
|||||||
ServerMessage::GameStart { identity } => {
|
ServerMessage::GameStart { identity } => {
|
||||||
info!("We go identity!");
|
info!("We go identity!");
|
||||||
self.identity = Some(identity.clone());
|
self.identity = Some(identity.clone());
|
||||||
self.board = Board::new(identity.id);
|
self.board = Board::new(self.board_area, identity.id);
|
||||||
self.game_state = ServerStage::Game;
|
self.game_state = ServerStage::Game;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,5 +297,20 @@ impl GameClient {
|
|||||||
};
|
};
|
||||||
|
|
||||||
self.chat.update(&self.game_area);
|
self.chat.update(&self.game_area);
|
||||||
|
|
||||||
|
let w = self.game_area.w * 0.75;
|
||||||
|
let h = self.game_area.h * 0.75;
|
||||||
|
let side = w.min(h);
|
||||||
|
|
||||||
|
self.board_area = Rect {
|
||||||
|
x: self.game_area.w / 2.0 - (side / 2.0),
|
||||||
|
y: self.game_area.h / 2.0 - (side / 2.0),
|
||||||
|
w: side,
|
||||||
|
h: side,
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.board.update_board_rect(self.board_area) {
|
||||||
|
self.board.update_state();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user