diff --git a/src/logic/board/field.rs b/src/logic/board/field.rs index 7ee3a18..f95e00b 100644 --- a/src/logic/board/field.rs +++ b/src/logic/board/field.rs @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize}; use crate::logic::{board::pawn::Pawn, math::position::Position}; -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, Default)] pub struct Field { pub position: Position, pub pawn: Option, diff --git a/src/logic/math/position.rs b/src/logic/math/position.rs index dbb7199..a5a904a 100644 --- a/src/logic/math/position.rs +++ b/src/logic/math/position.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use crate::logic::math::vector::Vector2D; -#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize)] +#[derive(Clone, Copy, PartialEq, Eq, Debug, Serialize, Deserialize, Default)] /// Position on the board. Consists of row and column. Should be treated like a point in space on /// unsigned grid. /// diff --git a/src/ui/macroquad/game/board.rs b/src/ui/macroquad/game/board.rs index 69864e6..7e1cfb1 100644 --- a/src/ui/macroquad/game/board.rs +++ b/src/ui/macroquad/game/board.rs @@ -1,5 +1,7 @@ +use std::ops::{Index, IndexMut}; + use macroquad::{ - color::{BLACK, Color, GREEN, RED, WHITE}, + color::{BLACK, Color, GREEN, ORANGE, RED, WHITE}, input::{is_mouse_button_pressed, mouse_position}, math::{Circle, Rect, vec2}, miniquad::window::high_dpi, @@ -16,6 +18,10 @@ use crate::logic::{ math::position::Position, }; +const MOVE_HIGHLITGHT_COLOR: Color = GREEN; +const CAPTURE_HIGHLITGHT_COLOR: Color = ORANGE; + +#[derive(Default)] struct GuiField { rect: Rect, field: Field, @@ -24,11 +30,59 @@ struct GuiField { color: Color, } +#[derive(Default)] +struct GuiFieldVec { + gui_fields: Vec, +} + +impl GuiFieldVec { + pub fn push(&mut self, input: GuiField) { + self.gui_fields.push(input); + } + + pub fn clear(&mut self) { + self.gui_fields.clear(); + } + + pub fn size(&self) -> usize { + self.gui_fields.len() + } +} + +impl<'a> IntoIterator for &'a GuiFieldVec { + type Item = &'a GuiField; + type IntoIter = std::slice::Iter<'a, GuiField>; + + fn into_iter(self) -> Self::IntoIter { + self.gui_fields.iter() + } +} + +impl Index for GuiFieldVec { + type Output = GuiField; + + fn index(&self, index: Position) -> &Self::Output { + self.gui_fields + .iter() + .find(|field| field.pos == index) + .unwrap() + } +} + +impl IndexMut for GuiFieldVec { + fn index_mut(&mut self, index: Position) -> &mut Self::Output { + self.gui_fields + .iter_mut() + .find(|field| field.pos == index) + .unwrap() + } +} + pub struct Board { rect: Rect, board_view: BoardView, - fields: Vec, + fields: GuiFieldVec, my_id: Uuid, @@ -48,7 +102,7 @@ impl Board { rect, board_view: BoardView::default(), - fields: vec![], + fields: GuiFieldVec::default(), my_id: player_id, @@ -69,6 +123,7 @@ impl Board { pub fn update_available_moves(&mut self, moves: Vec) { self.available_moves = Some(moves); self.available_captures = None; + self.highlight_current_moves(); } pub fn update_board_rect(&mut self, rect: Rect) -> bool { @@ -147,13 +202,19 @@ impl Board { } } - fn highligt_current_moves(&mut self) {} + fn highlight_current_moves(&mut self) { + let mut from: Vec = vec![]; - /* - 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; + let Some(available_moves) = &self.available_moves else { + return; + }; + + for available_move in available_moves { + from.push(available_move.from); + } + + for pos in from { + self.fields[pos].color = MOVE_HIGHLITGHT_COLOR; + } } - */ }