mirror of
https://github.com/szymon-jozef/checkers.git
synced 2026-10-27 19:20:44 +01:00
fix: not highligthing pawns on capture
This commit is contained in:
@@ -3,13 +3,12 @@ use std::{
|
|||||||
slice::IterMut,
|
slice::IterMut,
|
||||||
};
|
};
|
||||||
|
|
||||||
use log::{debug, info};
|
use log::debug;
|
||||||
use macroquad::{
|
use macroquad::{
|
||||||
color::{BLACK, Color, GREEN, ORANGE, RED, WHITE},
|
color::{BLACK, Color, GREEN, RED, WHITE},
|
||||||
input::{is_mouse_button_pressed, mouse_position},
|
input::{is_mouse_button_pressed, mouse_position},
|
||||||
math::{Circle, Rect, vec2},
|
math::{Rect, vec2},
|
||||||
miniquad::window::high_dpi,
|
shapes::{draw_circle, draw_circle_lines, draw_rectangle},
|
||||||
shapes::{draw_circle, draw_circle_lines, draw_rectangle, draw_rectangle_ex},
|
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@@ -23,7 +22,7 @@ use crate::logic::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
const MOVE_HIGHLITGHT_COLOR: Color = GREEN;
|
const MOVE_HIGHLITGHT_COLOR: Color = GREEN;
|
||||||
const CAPTURE_HIGHLITGHT_COLOR: Color = ORANGE;
|
const CAPTURE_HIGHLITGHT_COLOR: Color = RED;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct GuiField {
|
struct GuiField {
|
||||||
@@ -92,7 +91,7 @@ impl IndexMut<Position> for GuiFieldVec {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default, Debug)]
|
||||||
enum BoardState {
|
enum BoardState {
|
||||||
#[default]
|
#[default]
|
||||||
View,
|
View,
|
||||||
@@ -160,6 +159,8 @@ impl Board {
|
|||||||
|
|
||||||
let clicked_field = self.get_clicked_field();
|
let clicked_field = self.get_clicked_field();
|
||||||
|
|
||||||
|
debug!("Current state: {:?}", self.state);
|
||||||
|
|
||||||
match self.state {
|
match self.state {
|
||||||
BoardState::View => {
|
BoardState::View => {
|
||||||
if self.is_my_turn {
|
if self.is_my_turn {
|
||||||
@@ -196,6 +197,7 @@ impl Board {
|
|||||||
from,
|
from,
|
||||||
available_steps: vec![field.pos],
|
available_steps: vec![field.pos],
|
||||||
});
|
});
|
||||||
|
self.state = BoardState::View;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -203,10 +205,7 @@ impl Board {
|
|||||||
|
|
||||||
BoardState::ChooseCapture(from) => {
|
BoardState::ChooseCapture(from) => {
|
||||||
if self.is_back_move_wanted_by_player() {
|
if self.is_back_move_wanted_by_player() {
|
||||||
debug!("Going back");
|
self.reset_capture();
|
||||||
self.current_capture_path.clear();
|
|
||||||
self.current_deepness = 0;
|
|
||||||
self.state = BoardState::ChoosePawn;
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -255,9 +254,7 @@ impl Board {
|
|||||||
})
|
})
|
||||||
.cloned();
|
.cloned();
|
||||||
|
|
||||||
self.current_capture_path.clear();
|
self.reset_capture();
|
||||||
self.current_deepness = 0;
|
|
||||||
self.state = BoardState::View;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.highlight_capture_path(current_highlight);
|
self.highlight_capture_path(current_highlight);
|
||||||
@@ -265,6 +262,12 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn reset_capture(&mut self) {
|
||||||
|
self.current_capture_path.clear();
|
||||||
|
self.current_deepness = 0;
|
||||||
|
self.state = BoardState::View;
|
||||||
|
}
|
||||||
|
|
||||||
fn delegate_move(&mut self, from: Position) {
|
fn delegate_move(&mut self, from: Position) {
|
||||||
if let Some(capture_path) = &self.available_captures
|
if let Some(capture_path) = &self.available_captures
|
||||||
&& capture_path.iter().any(|path| path.from == from)
|
&& capture_path.iter().any(|path| path.from == from)
|
||||||
@@ -432,26 +435,22 @@ impl Board {
|
|||||||
self.is_my_turn = is_my_turn;
|
self.is_my_turn = is_my_turn;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_capture_path(&mut self, fields: Vec<Position>) {
|
fn highlight_current_moves(&mut self) {
|
||||||
for field in fields {
|
if let Some(available_captures) = &self.available_captures {
|
||||||
self.fields[field].color = CAPTURE_HIGHLITGHT_COLOR;
|
for capture_path in available_captures {
|
||||||
}
|
self.fields[capture_path.from].color = MOVE_HIGHLITGHT_COLOR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(available_moves) = &self.available_moves {
|
||||||
|
for available_move in available_moves {
|
||||||
|
self.fields[available_move.from].color = MOVE_HIGHLITGHT_COLOR;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_current_moves(&mut self) {
|
|
||||||
let Some(available_moves) = &self.available_moves else {
|
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut from: Vec<Position> = vec![];
|
|
||||||
|
|
||||||
for available_move in available_moves {
|
|
||||||
from.push(available_move.from);
|
|
||||||
}
|
|
||||||
|
|
||||||
for pos in from {
|
|
||||||
self.fields[pos].color = MOVE_HIGHLITGHT_COLOR;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn highlight_move_paths(&mut self, from: Position) {
|
fn highlight_move_paths(&mut self, from: Position) {
|
||||||
@@ -467,4 +466,10 @@ impl Board {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn highlight_capture_path(&mut self, fields: Vec<Position>) {
|
||||||
|
for field in fields {
|
||||||
|
self.fields[field].color = CAPTURE_HIGHLITGHT_COLOR;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user