From 264391ba81a285d0ff687c0266ee720382ccd953 Mon Sep 17 00:00:00 2001 From: Janet-Doe Date: Wed, 14 May 2025 15:57:01 +0200 Subject: [PATCH] functional move in 3D view --- .../java/chess/view/DDDrender/DDDView.java | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/chess/view/DDDrender/DDDView.java b/app/src/main/java/chess/view/DDDrender/DDDView.java index a4cd4f5..3588f62 100644 --- a/app/src/main/java/chess/view/DDDrender/DDDView.java +++ b/app/src/main/java/chess/view/DDDrender/DDDView.java @@ -3,6 +3,8 @@ package chess.view.DDDrender; import java.io.IOException; import java.util.List; +import chess.controller.commands.MoveCommand; +import chess.controller.event.GameListener; import org.joml.Vector2f; import org.joml.Vector3f; @@ -20,7 +22,7 @@ import chess.view.DDDrender.world.BoardEntity; import chess.view.DDDrender.world.PieceEntity; import chess.view.DDDrender.world.World; -public class DDDView extends GameAdaptator { +public class DDDView extends GameAdaptator implements GameListener { private static final Vector3f BLACK = new Vector3f(0.3f, 0.3f, 0.3f); private static final Vector3f WHITE = new Vector3f(1.0f, 1.0f, 1.0f); @@ -42,7 +44,7 @@ public class DDDView extends GameAdaptator { private void cancelClick(){ this.click=null; } - + private void setClick(Coordinate coordinate) {this.click=coordinate;} private CommandResult sendCommand(Command command) { return this.commandExecutor.executeCommand(command); } @@ -50,58 +52,58 @@ public class DDDView extends GameAdaptator { // Invoked when a cell is clicked private void onCellClick(Coordinate coordinate) { if (this.click == null){ // case: first click - System.out.println("First click on " + coordinate); + setClick(coordinate); previewMoves(coordinate); - this.click = coordinate; + System.out.println("First click on " + 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)); + if (sendCommand(movesCommand) == CommandResult.NotAllowed) { // case: invalid piece to move + System.out.println("Nothing to do here."); 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; + List allowedMoves = movesCommand.getDestinations(); + if (allowedMoves.isEmpty()) { // case: no movement possible for piece + System.out.println("This piece cannot be moved at the moment."); + cancelClick(); return; } - System.out.println("Cancelling click."); - cancelPreview(click); + if (allowedMoves.contains(coordinate)){ // case: valid attempt to move + System.out.println("Move on " + coordinate); + cancelPreview(this.click); + Command.CommandResult result = sendCommand(new MoveCommand(new Move(click, coordinate))); + cancelClick(); + return; + } + if (!(coordinate == this.click)) { + System.out.println("New click on " + coordinate); // cases: invalid move, selecting another piece + cancelPreview(this.click); + previewMoves(coordinate); + setClick(coordinate); + return; + } + System.out.println("Cancelling click."); // case: cancelling previous click + cancelPreview(this.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){ + private void previewMoves(Coordinate coordinate){ this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0)); Piece p = pieceAt(coordinate); if (p == null) - return false; + return; this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); if (sendCommand(movesCommand) == CommandResult.NotAllowed) - return false; + return; List allowedMoves = movesCommand.getDestinations(); if (allowedMoves.isEmpty()) - return false; + return; for (Coordinate destCoord : allowedMoves) { this.boardEntity.setCellColor(destCoord, new Vector3f(1, 1, 0)); } - return true; } // Invoked when a cell is hovered @@ -207,7 +209,12 @@ public class DDDView extends GameAdaptator { @Override public void onMove(Move move) { - // update world internal positions + if(move.getDeadPieceCoords() != null) { + this.world.ejectPiece(move.getDeadPieceCoords()); + } + Vector2f pieceBoardPos = DDDPlacement.coordinatesToVector(move.getFinish()); + Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y()); + this.world.getPiece(move.getStart()).setPosition(pieceWorldPos); this.world.movePiece(this.world.getPiece(move.getStart()), move); }