diff --git a/src/ui/macroquad/game/board.rs b/src/ui/macroquad/game/board.rs index c790ec8..94bb279 100644 --- a/src/ui/macroquad/game/board.rs +++ b/src/ui/macroquad/game/board.rs @@ -113,7 +113,10 @@ pub struct Board { is_my_turn: bool, available_captures: Option>, + choosen_capture: Option, + available_moves: Option>, + choosen_move: Option, field_size: f32, } @@ -138,7 +141,10 @@ impl Board { is_my_turn: false, available_captures: None, + choosen_capture: None, + available_moves: None, + choosen_move: None, field_size: 32.0, } @@ -175,6 +181,20 @@ impl Board { } self.highlight_move_paths(from); + + if let Some(field) = &self.get_clicked_field() { + if let Some(available_moves) = &self.available_moves { + if available_moves.iter().any(|available_move| { + available_move.from == from + && available_move.available_steps.contains(&field.pos) + }) { + self.choosen_move = Some(MovePath { + from, + available_steps: vec![field.pos], + }); + } + } + } } BoardState::ChooseCapture(from) => { @@ -201,6 +221,14 @@ impl Board { } } + pub fn get_choosen_capture(&mut self) -> Option { + self.choosen_capture.take() + } + + pub fn get_choosen_move(&mut self) -> Option { + self.choosen_move.take() + } + pub fn update_board_view(&mut self, board_view: BoardView) { self.board_view = board_view; } diff --git a/src/ui/macroquad/game/game.rs b/src/ui/macroquad/game/game.rs index 8d6efe2..c001490 100644 --- a/src/ui/macroquad/game/game.rs +++ b/src/ui/macroquad/game/game.rs @@ -14,7 +14,10 @@ use macroquad::{ use tokio::sync::mpsc::{Receiver, error::TryRecvError::Disconnected}; use crate::{ - logic::board::board_view::BoardView, + logic::board::{ + board_view::BoardView, + pawn::{CapturePath, MovePath}, + }, network::{ client::Client, message::ServerMessage, network_identity::NetworkIdentity, server::ServerStage, @@ -60,8 +63,8 @@ pub enum GuiCommands { Ready, Unready, - Capture, - Move, + Capture(CapturePath), + Move(MovePath), } impl GameClient { @@ -90,8 +93,13 @@ impl GameClient { GuiCommands::Unready => { client.revoke_readiness().await; } - GuiCommands::Capture => todo!(), - GuiCommands::Move => todo!(), + GuiCommands::Capture(capture_path) => todo!(), + + GuiCommands::Move(move_path) => { + client + .send_move(move_path.from, move_path.available_steps[0]) + .await; // ugly + } }, None => { @@ -229,6 +237,14 @@ impl GameClient { _ => {} } } + + if let Some(capture_path) = self.board.get_choosen_capture() { + let _ = self.cmd_sender.try_send(GuiCommands::Capture(capture_path)); + } + + if let Some(move_path) = self.board.get_choosen_move() { + let _ = self.cmd_sender.try_send(GuiCommands::Move(move_path)); + } } fn update_network(&mut self) {