From 4f821b44bcdafc33b6c015c092dffcff8e488ea1 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Wed, 10 May 2023 22:59:43 +0200 Subject: [PATCH] When a piece is placed at a position, check if this space is placable and place it there. --- app/src/main/java/school_project/GameUI.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/school_project/GameUI.java b/app/src/main/java/school_project/GameUI.java index 139109a..9b7491f 100644 --- a/app/src/main/java/school_project/GameUI.java +++ b/app/src/main/java/school_project/GameUI.java @@ -53,9 +53,20 @@ public class GameUI extends Group{ _piece.setLayoutX(event.getSceneX() - piece_pos_click.x); _piece.setLayoutY(event.getSceneY() - piece_pos_click.y); }); + _piece.setOnMouseReleased(event -> { + if(event.getSceneX() > grid.getLayoutX() && event.getSceneX() < grid.getLayoutX() + grid.boundary_size.x + && event.getSceneY() > grid.getLayoutY() && event.getSceneY() < grid.getLayoutY() + grid.boundary_size.y ) + { + // Inverted because screen is x →; y ↓ and matrix is x ↓; y → + Vec2 piece_position_in_grid = new Vec2( + (int) (_piece.getLayoutY() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutY())/(SEGMENT_SIZE+SPACE_SIZE), + (int) (_piece.getLayoutX() + (SEGMENT_SIZE+SPACE_SIZE)/2 - grid.getLayoutX())/(SEGMENT_SIZE+SPACE_SIZE) + ); + System.out.println(level.placePiece(p, piece_position_in_grid) + piece_position_in_grid.toString()); + } + }); getChildren().add(_piece); } - } -} +} \ No newline at end of file