diff --git a/app/src/main/java/chess/view/DDDrender/DDDView.java b/app/src/main/java/chess/view/DDDrender/DDDView.java index 297b7ae..a4cd4f5 100644 --- a/app/src/main/java/chess/view/DDDrender/DDDView.java +++ b/app/src/main/java/chess/view/DDDrender/DDDView.java @@ -1,11 +1,15 @@ package chess.view.DDDrender; import java.io.IOException; +import java.util.List; import org.joml.Vector2f; import org.joml.Vector3f; +import chess.controller.Command; +import chess.controller.Command.CommandResult; import chess.controller.CommandExecutor; +import chess.controller.commands.GetAllowedMovesPieceCommand; import chess.controller.commands.GetPieceAtCommand; import chess.controller.event.GameAdaptator; import chess.model.Color; @@ -26,6 +30,7 @@ public class DDDView extends GameAdaptator { private final World world; private BoardEntity boardEntity; private final Camera camera; + private Coordinate click = null; public DDDView(CommandExecutor commandExecutor) { this.commandExecutor = commandExecutor; @@ -34,28 +39,127 @@ public class DDDView extends GameAdaptator { this.window = new Window(new Renderer(), this.world, this.camera); } + private void cancelClick(){ + this.click=null; + } + + private CommandResult sendCommand(Command command) { + return this.commandExecutor.executeCommand(command); + } + // Invoked when a cell is clicked private void onCellClick(Coordinate coordinate) { - System.out.println("Cell clicked : " + coordinate); + if (this.click == null){ // case: first click + System.out.println("First click on " + coordinate); + previewMoves(coordinate); + this.click = coordinate; + return; + } + // case: second click + GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(this.click); + List allowedMoves = movesCommand.getDestinations(); + if (allowedMoves.contains(coordinate)){ // case: valid attempt at move + System.out.println("Move on " + coordinate); + cancelPreview(click); + //onMove(new Move(click, coordinate)); + cancelClick(); + return; + } + if (!(coordinate == click)) { // case: cancelling previous click + System.out.println("New click on " + coordinate); // cases: invalid move, selecting another piece + cancelPreview(click); + previewMoves(coordinate); + this.click = coordinate; + return; + } + System.out.println("Cancelling click."); + cancelPreview(click); + cancelClick(); + } + + // cas 1 : hover sans click => click = null + // cas 2 : click, attente => click = coo + // cas 3 : click, click sur le même => cancelClick + // cas 4 : click, click sur un autre pion à vérifier => preview nouveau pion, click = new_coo + // cas 5 : click, click sur une position éventuelle valide => move, cancelClick + // cas 6 : click, click sur une case non valide => move invalide, cancelClick + + + + private boolean previewMoves(Coordinate coordinate){ + this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0)); + Piece p = pieceAt(coordinate); + if (p == null) + return false; + this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); + GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); + if (sendCommand(movesCommand) == CommandResult.NotAllowed) + return false; + List allowedMoves = movesCommand.getDestinations(); + if (allowedMoves.isEmpty()) + return false; + for (Coordinate destCoord : allowedMoves) { + this.boardEntity.setCellColor(destCoord, new Vector3f(1, 1, 0)); + } + return true; } // Invoked when a cell is hovered private void onCellEnter(Coordinate coordinate) { - // small test turning a cell red when hovered - this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0)); - Piece p = pieceAt(coordinate); - if (p == null) - return; - this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); + if (this.click == null){ + // small test turning a cell red when hovered + this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0)); + Piece p = pieceAt(coordinate); + if (p == null) + return; + this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); + GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); + if (sendCommand(movesCommand) == CommandResult.NotAllowed) + return ; + List allowedMoves = movesCommand.getDestinations(); + if (allowedMoves.isEmpty()) + return ; + for (Coordinate destCoord : allowedMoves) { + this.boardEntity.setCellColor(destCoord, new Vector3f(1, 0, 0)); + } + } } // Invoked when a cell is not hovered anymore private void onCellExit(Coordinate coordinate) { + if (this.click == null){ + this.boardEntity.resetCellColor(coordinate); + Piece p = pieceAt(coordinate); + if (p == null) + return; + this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK); + GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); + if (sendCommand(movesCommand) == CommandResult.NotAllowed) + return ; + List allowedMoves = movesCommand.getDestinations(); + if (allowedMoves.isEmpty()) + return ; + for (Coordinate destCoord : allowedMoves) { + this.boardEntity.resetCellColor(destCoord); + } + } + } + + private void cancelPreview(Coordinate coordinate){ this.boardEntity.resetCellColor(coordinate); Piece p = pieceAt(coordinate); if (p == null) return; this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK); + GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); + if (sendCommand(movesCommand) == CommandResult.NotAllowed) + return ; + List allowedMoves = movesCommand.getDestinations(); + if (allowedMoves.isEmpty()) + return ; + for (Coordinate destCoord : allowedMoves) { + this.boardEntity.resetCellColor(destCoord); + } } private Piece pieceAt(Coordinate pos) { @@ -108,10 +212,12 @@ public class DDDView extends GameAdaptator { } public void run() { + /* this.window.addRegularTask((delta) -> { - final float angle = 1f; + final float angle = 1f; this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta); }); + */ this.window.run(); // free OpenGL resources