feat: add drawing board

This commit is contained in:
2026-08-23 11:33:18 +02:00
parent a8de0f550d
commit af04741a15
5 changed files with 121 additions and 10 deletions

View File

@@ -31,6 +31,24 @@ impl Index<usize> for BoardView {
}
}
impl IntoIterator for BoardView {
type Item = Field;
type IntoIter = std::vec::IntoIter<Field>;
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)

View File

@@ -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<BoardView> 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,
);
}
}
}
}

View File

@@ -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<NetworkIdentity>,
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
},

View File

@@ -1,2 +1,3 @@
mod chat;
mod board;
pub mod game;

View File

@@ -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<Option<Client>> {
let (tx, rc) = mpsc::channel::<Option<Client>>();