diff --git a/MavenChess/MavenChess/src/main/java/controller/Main.java b/MavenChess/MavenChess/src/main/java/controller/Main.java index 008b9952c903a684fcda5efa37a47eed3fee5a0f..9328f91194a1e86914b9c3feb04a53fffb1e2313 100644 --- a/MavenChess/MavenChess/src/main/java/controller/Main.java +++ b/MavenChess/MavenChess/src/main/java/controller/Main.java @@ -94,25 +94,38 @@ public class Main extends Application { } public void clickPiece(PieceView clickedPiece) { - // clicking a piece will ask her available moves - Position position = clickedPiece.getPosition(); - board.setSelected(position); - boardView.resetColor(material); - boardView.setColor(material, position, CustomMaterial.SELECTED_TILE); - ArrayList<Position> moves = board.getPiece(position).getAvailableMoves(board); - - // shows all available moves on the tiles, with colors, each tile-color having a meaning - for (Position move : moves) { - if(board.isFree(move)) { - boardView.setColor(material, move, CustomMaterial.MOVABLE_TILE); - } else if(board.isEnemy(move, position)) { - boardView.setColor(material, move, CustomMaterial.ATTACK_TILE); - } else { - boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); // any special move - } - // PROMOTION (attacking or not) - if(board.isPawn(position) && (move.getY() == 8 || move.getY() == 1)) { - boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); + + // graveyard pieces should not do anything + if(clickedPiece.getPosition().getX() < 0) { + return; + } + // if piece is an "attack" or "special" target, we consider it an action to apply (tile click) + if(clickedPiece.isTargeted()) { + clickTile(boardView.getTile(clickedPiece.getPosition().getID())); + } + else { + // clicking a piece will ask her available moves + Position position = clickedPiece.getPosition(); + board.setSelected(position); + boardView.resetColor(material); + boardView.setColor(material, position, CustomMaterial.SELECTED_TILE); + ArrayList<Position> moves = board.getPiece(position).getAvailableMoves(board); + + // shows all available moves on the tiles, with colors, each tile-color having a meaning + for (Position move : moves) { + if (board.isFree(move)) { + boardView.setColor(material, move, CustomMaterial.MOVABLE_TILE); + } else if (board.isEnemy(move, position)) { + boardView.setColor(material, move, CustomMaterial.ATTACK_TILE); + boardView.getPiece(move.getID()).setTargeted(material, CustomMaterial.ATTACK_TILE); + } else { + boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); // any special move + boardView.getPiece(move.getID()).setTargeted(material, CustomMaterial.SPECIAL_TILE); + } + // PROMOTION (attacking or not) + if (board.isPawn(position) && (move.getY() == 8 || move.getY() == 1)) { + boardView.setColor(material, move, CustomMaterial.SPECIAL_TILE); + } } } } @@ -129,6 +142,7 @@ public class Main extends Application { if(arrival.getY() == 8 || arrival.getY() == 1) { // --- ATTACK (while doing promotion) --- if (!board.isFree(arrival)) { + boardView.getPiece(arrival.getID()).resetTargeted(material); // reset color boardView.killPiece(arrival); // place piece on graveyard (visual) } // Promotion to Queen (by default, Queen for now) @@ -179,6 +193,7 @@ public class Main extends Application { else { // --- ATTACK --- if(state == TileView.ATTACK) { + boardView.getPiece(arrival.getID()).resetTargeted(material); // reset color boardView.killPiece(arrival); // place piece on graveyard (visual) } // --- MOVE --- diff --git a/MavenChess/MavenChess/src/main/java/view/BoardView.java b/MavenChess/MavenChess/src/main/java/view/BoardView.java index dc006f208c1d134bc0eef33287dbe04e4f95b4ad..1235ffdc8880d93ffc597c05c88301948e680ba1 100644 --- a/MavenChess/MavenChess/src/main/java/view/BoardView.java +++ b/MavenChess/MavenChess/src/main/java/view/BoardView.java @@ -21,7 +21,6 @@ public class BoardView { createTile(new Position(i, j)); } } - resetColor(material); // init/reset the board squares with black and white // creates the 3d pieces (from the board list) pieces = new PieceView[64]; @@ -41,6 +40,8 @@ public class BoardView { // init graveyards as empty black_death_count = 0; white_death_count = 0; + + resetColor(material); // init/reset the board squares with black and white } public TileView getTile(int id) { @@ -131,6 +132,9 @@ public class BoardView { getTileObj(n).setMaterial(material.get(CustomMaterial.WHITE_TILE)); // (visually) } getTile(n).setState(TileView.NORMAL); // (on 'state' attribute) + if(getPiece(n) != null) { + getPiece(n).resetTargeted(material); // reset the 3d pieces color too (visual) + } } } } diff --git a/MavenChess/MavenChess/src/main/java/view/PieceView.java b/MavenChess/MavenChess/src/main/java/view/PieceView.java index 3e6cb142a55b6566408e2ff9a4a2244295e211bc..a67f77f6cb4d65786eca3543bf57dac335e0b51c 100644 --- a/MavenChess/MavenChess/src/main/java/view/PieceView.java +++ b/MavenChess/MavenChess/src/main/java/view/PieceView.java @@ -1,17 +1,20 @@ package view; import javafx.scene.shape.MeshView; +import model.Piece; import model.Position; public class PieceView { private MeshView obj; private Position position; private int color; + private boolean isTargeted; public PieceView(MeshView obj, Position position, int color) { this.obj = obj; this.position = position; this.color = color; + isTargeted = false; } public MeshView getObj() { @@ -37,4 +40,21 @@ public class PieceView { public void setColor(int color) { this.color = color; } + + public boolean isTargeted() { + return isTargeted; + } + + public void setTargeted(CustomMaterial material, int tileColor) { + isTargeted = true; + obj.setMaterial(material.get(tileColor)); + } + + public void resetTargeted(CustomMaterial material) { + isTargeted = false; + if(color == Piece.WHITE) + obj.setMaterial(material.get(CustomMaterial.WHITE)); + else + obj.setMaterial(material.get(CustomMaterial.BLACK)); + } } diff --git a/MavenChess/MavenChess/target/classes/controller/Main.class b/MavenChess/MavenChess/target/classes/controller/Main.class index a58fe7ddbb1a5d7404aac4d5172a66aeafdf4666..5e69f5ffa85aa5268903966b3a16a5f30e180f58 100644 Binary files a/MavenChess/MavenChess/target/classes/controller/Main.class and b/MavenChess/MavenChess/target/classes/controller/Main.class differ diff --git a/MavenChess/MavenChess/target/classes/view/BoardView.class b/MavenChess/MavenChess/target/classes/view/BoardView.class index 13aa03119faf5d4aef19c4100a993ea3c4332aaf..899a1f0571f780e57241ebcaad5bc04e9324a0c2 100644 Binary files a/MavenChess/MavenChess/target/classes/view/BoardView.class and b/MavenChess/MavenChess/target/classes/view/BoardView.class differ diff --git a/MavenChess/MavenChess/target/classes/view/PieceView.class b/MavenChess/MavenChess/target/classes/view/PieceView.class index 861779ea1eb44cd71c28a9393b6864abf3cf04a9..2cacf877973b8926ea0dae0b465634a7c1287266 100644 Binary files a/MavenChess/MavenChess/target/classes/view/PieceView.class and b/MavenChess/MavenChess/target/classes/view/PieceView.class differ