refactor: add GuiField; split drawing and updating

This commit is contained in:
2026-08-23 15:15:41 +02:00
parent 0663a25131
commit 337eec8e09
3 changed files with 66 additions and 34 deletions

View File

@@ -1,4 +1,4 @@
pub mod board; pub mod board;
pub mod board_view; pub mod board_view;
mod field; pub mod field;
pub mod pawn; pub mod pawn;

View File

@@ -1,22 +1,31 @@
use macroquad::{ use macroquad::{
color::{BLACK, GREEN, RED, WHITE}, color::{BLACK, Color, GREEN, RED, WHITE},
input::{is_mouse_button_pressed, mouse_position}, input::{is_mouse_button_pressed, mouse_position},
math::{Rect, vec2}, math::{Circle, Rect, vec2},
miniquad::window::high_dpi, miniquad::window::high_dpi,
shapes::{draw_circle, draw_rectangle, draw_rectangle_ex}, shapes::{draw_circle, draw_circle_lines, draw_rectangle, draw_rectangle_ex},
}; };
use uuid::Uuid; use uuid::Uuid;
use crate::logic::{ use crate::logic::{
board::{ board::{
board_view::BoardView, board_view::BoardView,
field::Field,
pawn::{CapturePath, MovePath}, pawn::{CapturePath, MovePath},
}, },
math::position::Position, math::position::Position,
}; };
struct GuiField {
rect: Rect,
field: Field,
is_clickable: bool,
color: Color,
}
pub struct Board { pub struct Board {
board_view: BoardView, board_view: BoardView,
fields: Vec<GuiField>,
my_id: Uuid, my_id: Uuid,
current_highlight: Option<Position>, current_highlight: Option<Position>,
@@ -35,6 +44,8 @@ impl Board {
pub fn new(player_id: Uuid) -> Self { pub fn new(player_id: Uuid) -> Self {
Self { Self {
board_view: BoardView::default(), board_view: BoardView::default(),
fields: vec![],
my_id: player_id, my_id: player_id,
current_highlight: None, current_highlight: None,
@@ -57,7 +68,9 @@ impl Board {
self.available_captures = None; self.available_captures = None;
} }
pub fn draw(&mut self) { pub fn update_state(&mut self) {
self.fields.clear();
let field_size = 32.0; let field_size = 32.0;
for field in &self.board_view { for field in &self.board_view {
@@ -70,42 +83,59 @@ 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(); let rect = Rect {
x: abs_x,
y: abs_y,
w: field_size,
h: field_size,
};
if abs_x < mouse_pos_x self.fields.push(GuiField {
&& mouse_pos_x <= abs_x + field_size rect,
&& abs_y < mouse_pos_y field: field.clone(),
&& mouse_pos_y <= abs_y + field_size is_clickable: false,
&& is_mouse_button_pressed(macroquad::input::MouseButton::Left) color: if is_field_black { BLACK } else { WHITE },
{ });
self.current_highlight = Some(pos); }
} }
if is_field_black { pub fn draw(&self) {
draw_rectangle(abs_x, abs_y, field_size, field_size, BLACK); for field in &self.fields {
} else { draw_rectangle(
draw_rectangle(abs_x, abs_y, field_size, field_size, WHITE); field.rect.x,
} field.rect.y,
field.rect.w,
field.rect.h,
field.color,
);
if let Some(hihglith) = self.current_highlight { if field.is_clickable {
let x = hihglith.column as f32 * field_size; draw_rectangle(
let y = hihglith.row as f32 * field_size; field.rect.x,
field.rect.y,
draw_rectangle(x, y, field_size, field_size, GREEN); field.rect.w,
} field.rect.h,
GREEN,
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,
); );
} }
if let Some(pawn) = &field.field.pawn {
let color = if pawn.owner == self.my_id { WHITE } else { RED }; // TODO! This should
// be customizable
let center = field.rect.center();
let r = field.rect.w * 0.5;
let thickness = 0.5;
draw_circle(center.x, center.y, r, color);
draw_circle_lines(center.x, center.y, r, thickness, BLACK);
}
} }
} }
fn highligt_current_moves(&mut self) {}
/* /*
pub fn update_current_highlight(&mut self) { pub fn update_current_highlight(&mut self) {
let (mut mouse_pos_x, mut mouse_pos_y) = mouse_position(); let (mut mouse_pos_x, mut mouse_pos_y) = mouse_position();

View File

@@ -227,7 +227,8 @@ impl GameClient {
Ok(msg) => match msg { Ok(msg) => match msg {
ServerMessage::GameStart { identity } => { ServerMessage::GameStart { identity } => {
info!("We go identity!"); info!("We go identity!");
self.identity = Some(identity); self.identity = Some(identity.clone());
self.board = Board::new(identity.id);
self.game_state = ServerStage::Game; self.game_state = ServerStage::Game;
} }
@@ -242,6 +243,7 @@ impl GameClient {
ServerMessage::BroadcastBoardState { board } => { ServerMessage::BroadcastBoardState { board } => {
self.board.update_board_view(board); self.board.update_board_view(board);
self.board.update_state();
} }
ServerMessage::BroadcastCurrentTurn { active_player } => { ServerMessage::BroadcastCurrentTurn { active_player } => {