mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
feat: add highlithing fields
This commit is contained in:
@@ -1,29 +1,63 @@
|
|||||||
use macroquad::{
|
use macroquad::{
|
||||||
color::{BLACK, RED, WHITE},
|
color::{BLACK, GREEN, RED, WHITE},
|
||||||
math::vec2,
|
input::{is_mouse_button_pressed, mouse_position},
|
||||||
shapes::{draw_circle, draw_rectangle},
|
math::{Rect, vec2},
|
||||||
|
miniquad::window::high_dpi,
|
||||||
|
shapes::{draw_circle, draw_rectangle, draw_rectangle_ex},
|
||||||
};
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::logic::{board::board_view::BoardView, math::position::Position};
|
use crate::logic::{
|
||||||
|
board::{
|
||||||
|
board_view::BoardView,
|
||||||
|
pawn::{CapturePath, MovePath},
|
||||||
|
},
|
||||||
|
math::position::Position,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Board {
|
pub struct Board {
|
||||||
board_view: BoardView,
|
board_view: BoardView,
|
||||||
}
|
|
||||||
|
|
||||||
impl From<BoardView> for Board {
|
my_id: Uuid,
|
||||||
fn from(value: BoardView) -> Self {
|
current_highlight: Option<Position>,
|
||||||
Self { board_view: value }
|
|
||||||
}
|
available_captures: Option<Vec<CapturePath>>,
|
||||||
|
available_moves: Option<Vec<MovePath>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Board {
|
impl Default for Board {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::from(BoardView::default())
|
Self::new(Uuid::default())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn draw(&self) {
|
pub fn new(player_id: Uuid) -> Self {
|
||||||
|
Self {
|
||||||
|
board_view: BoardView::default(),
|
||||||
|
my_id: player_id,
|
||||||
|
current_highlight: None,
|
||||||
|
|
||||||
|
available_captures: None,
|
||||||
|
available_moves: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_board_view(&mut self, board_view: BoardView) {
|
||||||
|
self.board_view = board_view;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_available_captures(&mut self, captures: Vec<CapturePath>) {
|
||||||
|
self.available_captures = Some(captures);
|
||||||
|
self.available_moves = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_available_moves(&mut self, moves: Vec<MovePath>) {
|
||||||
|
self.available_moves = Some(moves);
|
||||||
|
self.available_captures = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn draw(&mut self) {
|
||||||
let field_size = 32.0;
|
let field_size = 32.0;
|
||||||
|
|
||||||
for field in &self.board_view {
|
for field in &self.board_view {
|
||||||
@@ -36,12 +70,30 @@ impl Board {
|
|||||||
let abs_x = pos.column as f32 * field_size;
|
let abs_x = pos.column as f32 * field_size;
|
||||||
let abs_y = pos.row as f32 * field_size;
|
let abs_y = pos.row as f32 * field_size;
|
||||||
|
|
||||||
|
let (mouse_pos_x, mouse_pos_y) = mouse_position();
|
||||||
|
|
||||||
|
if abs_x < mouse_pos_x
|
||||||
|
&& mouse_pos_x <= abs_x + field_size
|
||||||
|
&& abs_y < mouse_pos_y
|
||||||
|
&& mouse_pos_y <= abs_y + field_size
|
||||||
|
&& is_mouse_button_pressed(macroquad::input::MouseButton::Left)
|
||||||
|
{
|
||||||
|
self.current_highlight = Some(pos);
|
||||||
|
}
|
||||||
|
|
||||||
if is_field_black {
|
if is_field_black {
|
||||||
draw_rectangle(abs_x, abs_y, field_size, field_size, BLACK);
|
draw_rectangle(abs_x, abs_y, field_size, field_size, BLACK);
|
||||||
} else {
|
} else {
|
||||||
draw_rectangle(abs_x, abs_y, field_size, field_size, WHITE);
|
draw_rectangle(abs_x, abs_y, field_size, field_size, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(hihglith) = self.current_highlight {
|
||||||
|
let x = hihglith.column as f32 * field_size;
|
||||||
|
let y = hihglith.row as f32 * field_size;
|
||||||
|
|
||||||
|
draw_rectangle(x, y, field_size, field_size, GREEN);
|
||||||
|
}
|
||||||
|
|
||||||
if field.pawn.is_some() {
|
if field.pawn.is_some() {
|
||||||
// draw something for now
|
// draw something for now
|
||||||
draw_circle(
|
draw_circle(
|
||||||
@@ -53,4 +105,12 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
pub fn update_current_highlight(&mut self) {
|
||||||
|
let (mut mouse_pos_x, mut mouse_pos_y) = mouse_position();
|
||||||
|
mouse_pos_x /= self.board_view.size as f32;
|
||||||
|
mouse_pos_y /= self.board_view.size as f32;
|
||||||
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ impl GameClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_game(&self) {
|
fn draw_game(&mut self) {
|
||||||
self.board.draw();
|
self.board.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -231,13 +231,17 @@ impl GameClient {
|
|||||||
self.game_state = ServerStage::Game;
|
self.game_state = ServerStage::Game;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::AvailableCaptures { captures } => todo!(),
|
ServerMessage::AvailableCaptures { captures } => {
|
||||||
|
self.board.update_available_captures(captures);
|
||||||
|
}
|
||||||
|
|
||||||
ServerMessage::AvailableMoves { moves } => {
|
ServerMessage::AvailableMoves { moves } => {
|
||||||
// TODO! Implement this (no todo to test drawing board!!)
|
// TODO! Implement this (no todo to test drawing board!!)
|
||||||
|
self.board.update_available_moves(moves);
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::BroadcastBoardState { board } => {
|
ServerMessage::BroadcastBoardState { board } => {
|
||||||
self.board = Board::from(board);
|
self.board.update_board_view(board);
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerMessage::BroadcastCurrentTurn { active_player } => {
|
ServerMessage::BroadcastCurrentTurn { active_player } => {
|
||||||
|
|||||||
Reference in New Issue
Block a user