functional move in 3D view

This commit is contained in:
Janet-Doe
2025-05-14 15:57:01 +02:00
parent ce977c3b48
commit 264391ba81

View File

@@ -3,6 +3,8 @@ package chess.view.DDDrender;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import chess.controller.commands.MoveCommand;
import chess.controller.event.GameListener;
import org.joml.Vector2f; import org.joml.Vector2f;
import org.joml.Vector3f; 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.PieceEntity;
import chess.view.DDDrender.world.World; 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 BLACK = new Vector3f(0.3f, 0.3f, 0.3f);
private static final Vector3f WHITE = new Vector3f(1.0f, 1.0f, 1.0f); 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(){ private void cancelClick(){
this.click=null; this.click=null;
} }
private void setClick(Coordinate coordinate) {this.click=coordinate;}
private CommandResult sendCommand(Command command) { private CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command); return this.commandExecutor.executeCommand(command);
} }
@@ -50,58 +52,58 @@ public class DDDView extends GameAdaptator {
// Invoked when a cell is clicked // Invoked when a cell is clicked
private void onCellClick(Coordinate coordinate) { private void onCellClick(Coordinate coordinate) {
if (this.click == null){ // case: first click if (this.click == null){ // case: first click
System.out.println("First click on " + coordinate); setClick(coordinate);
previewMoves(coordinate); previewMoves(coordinate);
this.click = coordinate; System.out.println("First click on " + coordinate);
return; return;
} }
// case: second click // case: second click
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(this.click); GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(this.click);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) { // case: invalid piece to move
System.out.println("Nothing to do here.");
cancelClick();
return;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations(); List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.contains(coordinate)){ // case: valid attempt at move if (allowedMoves.isEmpty()) { // case: no movement possible for piece
System.out.println("This piece cannot be moved at the moment.");
cancelClick();
return;
}
if (allowedMoves.contains(coordinate)){ // case: valid attempt to move
System.out.println("Move on " + coordinate); System.out.println("Move on " + coordinate);
cancelPreview(click); cancelPreview(this.click);
//onMove(new Move(click, coordinate)); Command.CommandResult result = sendCommand(new MoveCommand(new Move(click, coordinate)));
cancelClick(); cancelClick();
return; return;
} }
if (!(coordinate == click)) { // case: cancelling previous click if (!(coordinate == this.click)) {
System.out.println("New click on " + coordinate); // cases: invalid move, selecting another piece System.out.println("New click on " + coordinate); // cases: invalid move, selecting another piece
cancelPreview(click); cancelPreview(this.click);
previewMoves(coordinate); previewMoves(coordinate);
this.click = coordinate; setClick(coordinate);
return; return;
} }
System.out.println("Cancelling click."); System.out.println("Cancelling click."); // case: cancelling previous click
cancelPreview(click); cancelPreview(this.click);
cancelClick(); cancelClick();
} }
// cas 1 : hover sans click => click = null private void previewMoves(Coordinate coordinate){
// 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)); this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0));
Piece p = pieceAt(coordinate); Piece p = pieceAt(coordinate);
if (p == null) if (p == null)
return false; return;
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0));
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate); GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return false; return;
List<Coordinate> allowedMoves = movesCommand.getDestinations(); List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty()) if (allowedMoves.isEmpty())
return false; return;
for (Coordinate destCoord : allowedMoves) { for (Coordinate destCoord : allowedMoves) {
this.boardEntity.setCellColor(destCoord, new Vector3f(1, 1, 0)); this.boardEntity.setCellColor(destCoord, new Vector3f(1, 1, 0));
} }
return true;
} }
// Invoked when a cell is hovered // Invoked when a cell is hovered
@@ -207,7 +209,12 @@ public class DDDView extends GameAdaptator {
@Override @Override
public void onMove(Move move) { 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); this.world.movePiece(this.world.getPiece(move.getStart()), move);
} }