From af04741a15fb2475d283d50dc1310d0f1b8a9f23 Mon Sep 17 00:00:00 2001 From: szymon Date: Sun, 23 Aug 2026 11:33:18 +0200 Subject: [PATCH] feat: add drawing board --- src/logic/board/board_view.rs | 18 +++++++++++ src/ui/macroquad/game/board.rs | 56 ++++++++++++++++++++++++++++++++++ src/ui/macroquad/game/game.rs | 44 ++++++++++++++++++++------ src/ui/macroquad/game/mod.rs | 1 + src/ui/state.rs | 12 +++++++- 5 files changed, 121 insertions(+), 10 deletions(-) create mode 100644 src/ui/macroquad/game/board.rs diff --git a/src/logic/board/board_view.rs b/src/logic/board/board_view.rs index 4850edb..8976784 100644 --- a/src/logic/board/board_view.rs +++ b/src/logic/board/board_view.rs @@ -31,6 +31,24 @@ impl Index for BoardView { } } +impl IntoIterator for BoardView { + type Item = Field; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.board.into_iter() + } +} + +impl<'a> IntoIterator for &'a BoardView { + type Item = &'a Field; + type IntoIter = std::slice::Iter<'a, Field>; + + fn into_iter(self) -> Self::IntoIter { + self.board.iter() + } +} + impl BoardView { pub fn to_string(&self, owner: &Uuid) -> String { (0..self.size) diff --git a/src/ui/macroquad/game/board.rs b/src/ui/macroquad/game/board.rs new file mode 100644 index 0000000..17318d7 --- /dev/null +++ b/src/ui/macroquad/game/board.rs @@ -0,0 +1,56 @@ +use macroquad::{ + color::{BLACK, RED, WHITE}, + math::vec2, + shapes::{draw_circle, draw_rectangle}, +}; + +use crate::logic::{board::board_view::BoardView, math::position::Position}; + +pub struct Board { + board_view: BoardView, +} + +impl From for Board { + fn from(value: BoardView) -> Self { + Self { board_view: value } + } +} + +impl Default for Board { + fn default() -> Self { + Self::from(BoardView::default()) + } +} + +impl Board { + pub fn draw(&self) { + let field_size = 32.0; + + for field in &self.board_view { + let pos: Position = field.position; + + let is_row_even: bool = pos.row % 2 == 0; + let is_column_even: bool = pos.column % 2 == 0; + let is_field_black: bool = is_row_even ^ is_column_even; + + let abs_x = pos.column as f32 * field_size; + let abs_y = pos.row as f32 * field_size; + + if is_field_black { + draw_rectangle(abs_x, abs_y, field_size, field_size, BLACK); + } else { + draw_rectangle(abs_x, abs_y, field_size, field_size, WHITE); + } + + if field.pawn.is_some() { + // draw something for now + draw_circle( + abs_x + field_size * 0.5, + abs_y + field_size * 0.5, + 16.0, + RED, + ); + } + } + } +} diff --git a/src/ui/macroquad/game/game.rs b/src/ui/macroquad/game/game.rs index 83a3877..6b74d63 100644 --- a/src/ui/macroquad/game/game.rs +++ b/src/ui/macroquad/game/game.rs @@ -1,3 +1,5 @@ +use std::convert::identity; + use tokio::sync::mpsc::{self, Sender}; use log::{debug, error, info}; @@ -17,7 +19,10 @@ use crate::{ client::Client, message::ServerMessage, network_identity::NetworkIdentity, server::ServerStage, }, - ui::{macroquad::game::chat::Chat, state::GameContext}, + ui::{ + macroquad::game::{board::Board, chat::Chat}, + state::GameContext, + }, }; pub struct GameClient { @@ -26,7 +31,8 @@ pub struct GameClient { identity: Option, - board: BoardView, + board: Board, + is_my_turn: bool, player_name: String, enemy_name: String, // TODO! Server should give this info to us (it doesn't rn) @@ -95,7 +101,7 @@ impl GameClient { }); }); - let board = BoardView::default(); + let board = Board::default(); let game_stage = ServerStage::default(); let mut lobby = Lobby::default(); @@ -119,6 +125,8 @@ impl GameClient { board, lobby, + is_my_turn: false, + player_name: "Morbius".to_string(), enemy_name: "Milo".to_string(), @@ -180,7 +188,7 @@ impl GameClient { } fn draw_game(&self) { - todo!(); + self.board.draw(); } fn draw_summary_screen(&self) { @@ -195,7 +203,8 @@ impl GameClient { match self.game_state { ServerStage::Lobby => self.update_lobby(), - ServerStage::Game => todo!(), + ServerStage::Game => {} + ServerStage::End => todo!(), } @@ -219,18 +228,35 @@ impl GameClient { ServerMessage::GameStart { identity } => { info!("We go identity!"); self.identity = Some(identity); + self.game_state = ServerStage::Game; } ServerMessage::AvailableCaptures { captures } => todo!(), - ServerMessage::AvailableMoves { moves } => todo!(), - ServerMessage::BroadcastBoardState { board } => todo!(), - ServerMessage::BroadcastCurrentTurn { active_player } => todo!(), + ServerMessage::AvailableMoves { moves } => { + // TODO! Implement this (no todo to test drawing board!!) + } + + ServerMessage::BroadcastBoardState { board } => { + self.board = Board::from(board); + } + + ServerMessage::BroadcastCurrentTurn { active_player } => { + if let Some(identity) = &self.identity + && identity.id == active_player + { + info!("It's out turn!"); + self.is_my_turn = true; + } + } + ServerMessage::BroadCastTextMessage { sender, content } => { debug!("Got message from: {} with content: {}", sender, content); self.chat.push_message(sender, content); } - ServerMessage::GameEnd { result } => todo!(), + ServerMessage::GameEnd { result } => { + self.game_state = ServerStage::End; + } _ => {} // ignore things that network client handled by itself }, diff --git a/src/ui/macroquad/game/mod.rs b/src/ui/macroquad/game/mod.rs index 34a8b29..661b9ac 100644 --- a/src/ui/macroquad/game/mod.rs +++ b/src/ui/macroquad/game/mod.rs @@ -1,2 +1,3 @@ mod chat; +mod board; pub mod game; diff --git a/src/ui/state.rs b/src/ui/state.rs index 9dade60..7a5d9c1 100644 --- a/src/ui/state.rs +++ b/src/ui/state.rs @@ -29,13 +29,23 @@ pub enum GameMode { }, } -#[derive(Default, Clone)] +#[derive(Clone)] pub struct GameContext { pub difficulty: BotDificulty, pub gamemode: GameMode, pub server_url_buffer: String, } +impl Default for GameContext { + fn default() -> Self { + Self { + difficulty: BotDificulty::default(), + gamemode: GameMode::default(), + server_url_buffer: "127.0.0.1".to_string(), + } + } +} + // TODO! Move this pub fn connect_to_server() -> Receiver> { let (tx, rc) = mpsc::channel::>();