Skip to content

Commit

Permalink
fix:game
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Mauran <[email protected]>
  • Loading branch information
thomas-mauran committed Nov 23, 2024
1 parent 7adc4eb commit e9bc75e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 29 deletions.
36 changes: 18 additions & 18 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
// crossterm on Windows sends Release and Repeat events as well, which we ignore.
return Ok(());
}
if app.board.mouse_used {
app.board.mouse_used = false;
if app.board.selected_coordinates != Coord::undefined() {
app.board.cursor_coordinates = app.board.selected_coordinates;
app.board.selected_coordinates = Coord::undefined();
if app.game.board.mouse_used {
app.game.board.mouse_used = false;
if app.game.board.selected_coordinates != Coord::undefined() {
app.game.board.cursor_coordinates = app.game.board.selected_coordinates;
app.game.board.selected_coordinates = Coord::undefined();
} else {
app.board.cursor_coordinates.col = 4;
app.board.cursor_coordinates.row = 4;
app.game.board.cursor_coordinates.col = 4;
app.game.board.cursor_coordinates.row = 4;
}
}

Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn handle_mouse_events(mouse_event: MouseEvent, app: &mut App) -> AppResult<
return Ok(());
}
if mouse_event.kind == MouseEventKind::Down(MouseButton::Left) {
if app.board.is_checkmate || app.board.is_draw {
if app.game.board.is_checkmate || app.game.board.is_draw {
return Ok(());
}

Expand All @@ -118,26 +118,26 @@ pub fn handle_mouse_events(mouse_event: MouseEvent, app: &mut App) -> AppResult<

// If there is a promotion to be done the top_x, top_y, width and height
// values are updated accordingly
if app.board.is_promotion {
let x = (mouse_event.column - app.board.top_x) / app.board.width;
let y = (mouse_event.row - app.board.top_y) / app.board.height;
if app.game.board.is_promotion {
let x = (mouse_event.column - app.game.board.top_x) / app.game.board.width;
let y = (mouse_event.row - app.game.board.top_y) / app.game.board.height;
if x > 3 || y > 0 {
return Ok(());
}
app.board.promotion_cursor = x as i8;
app.board.promote_piece();
app.game.board.promotion_cursor = x as i8;
app.game.board.promote_piece();
}
if mouse_event.column < app.board.top_x || mouse_event.row < app.board.top_y {
if mouse_event.column < app.game.board.top_x || mouse_event.row < app.game.board.top_y {
return Ok(());
}
let x = (mouse_event.column - app.board.top_x) / app.board.width;
let y = (mouse_event.row - app.board.top_y) / app.board.height;
let x = (mouse_event.column - app.game.board.top_x) / app.game.board.width;
let y = (mouse_event.row - app.game.board.top_y) / app.game.board.height;
if x > 7 || y > 7 {
return Ok(());
}
app.board.mouse_used = true;
app.game.board.mouse_used = true;
let coords: Coord = Coord::new(y as u8, x as u8);
app.board.move_selected_piece_cursor_mouse(coords);
app.game.board.move_selected_piece_cursor_mouse(coords);
}
Ok(())
}
14 changes: 7 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate chess_tui;
use chess_tui::app::{App, AppResult};
use chess_tui::constants::{home_dir, DisplayMode};
use chess_tui::event::{Event, EventHandler};
use chess_tui::handler::handle_key_events;
use chess_tui::handler::{handle_key_events, handle_mouse_events};
use chess_tui::ratatui::tui::Tui;
use clap::Parser;
use std::fs::{self, File};
Expand Down Expand Up @@ -77,13 +77,13 @@ fn main() -> AppResult<()> {
Event::Mouse(mouse_event) => handle_mouse_events(mouse_event, &mut app)?,
Event::Resize(_, _) => {}
}
if app.board.bot_will_move {
app.board.bot_move();
app.board.switch_player_turn();
app.board.bot_will_move = false;
if app.game.board.bot_will_move {
app.game.board.bot_move();
app.game.board.switch_player_turn();
app.game.board.bot_will_move = false;
// need to be centralised
app.board.is_checkmate = app.board.is_checkmate();
app.board.is_draw = app.board.is_draw();
app.game.board.is_checkmate = app.game.board.is_checkmate();
app.game.board.is_draw = app.game.board.is_draw();
tui.draw(&mut app)?;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ui/popups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ pub fn render_promotion_popup(frame: &mut Frame, app: &mut App) {
.split(inner_popup_layout_vertical[1]);

// When a promotion is happening, the mouse should be able to know where the icons are
app.board.top_x = inner_popup_layout_horizontal[0].x;
app.board.top_y = inner_popup_layout_horizontal[0].y;
app.board.width = inner_popup_layout_horizontal[0].width;
app.board.height = inner_popup_layout_horizontal[0].height;
app.game.board.top_x = inner_popup_layout_horizontal[0].x;
app.game.board.top_y = inner_popup_layout_horizontal[0].y;
app.game.board.width = inner_popup_layout_horizontal[0].width;
app.game.board.height = inner_popup_layout_horizontal[0].height;

let display_mode = &app.game.board.display_mode;

Expand Down

0 comments on commit e9bc75e

Please sign in to comment.