onclick functional

This commit is contained in:
Janet-Doe
2025-05-14 10:10:14 +02:00
parent 83d5737789
commit 046f680937

View File

@@ -1,11 +1,15 @@
package chess.view.DDDrender; package chess.view.DDDrender;
import java.io.IOException; import java.io.IOException;
import java.util.List;
import org.joml.Vector2f; import org.joml.Vector2f;
import org.joml.Vector3f; import org.joml.Vector3f;
import chess.controller.Command;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor; import chess.controller.CommandExecutor;
import chess.controller.commands.GetAllowedMovesPieceCommand;
import chess.controller.commands.GetPieceAtCommand; import chess.controller.commands.GetPieceAtCommand;
import chess.controller.event.GameAdaptator; import chess.controller.event.GameAdaptator;
import chess.model.Color; import chess.model.Color;
@@ -26,6 +30,7 @@ public class DDDView extends GameAdaptator {
private final World world; private final World world;
private BoardEntity boardEntity; private BoardEntity boardEntity;
private final Camera camera; private final Camera camera;
private Coordinate click = null;
public DDDView(CommandExecutor commandExecutor) { public DDDView(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
@@ -34,28 +39,127 @@ public class DDDView extends GameAdaptator {
this.window = new Window(new Renderer(), this.world, this.camera); 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 // Invoked when a cell is clicked
private void onCellClick(Coordinate coordinate) { 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<Coordinate> 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<Coordinate> 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 // Invoked when a cell is hovered
private void onCellEnter(Coordinate coordinate) { private void onCellEnter(Coordinate coordinate) {
// small test turning a cell red when hovered if (this.click == null){
this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0)); // small test turning a cell red when hovered
Piece p = pieceAt(coordinate); this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0));
if (p == null) Piece p = pieceAt(coordinate);
return; if (p == null)
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0)); return;
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0));
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return ;
List<Coordinate> 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 // Invoked when a cell is not hovered anymore
private void onCellExit(Coordinate coordinate) { 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<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty())
return ;
for (Coordinate destCoord : allowedMoves) {
this.boardEntity.resetCellColor(destCoord);
}
}
}
private void cancelPreview(Coordinate coordinate){
this.boardEntity.resetCellColor(coordinate); this.boardEntity.resetCellColor(coordinate);
Piece p = pieceAt(coordinate); Piece p = pieceAt(coordinate);
if (p == null) if (p == null)
return; return;
this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK); this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return ;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
if (allowedMoves.isEmpty())
return ;
for (Coordinate destCoord : allowedMoves) {
this.boardEntity.resetCellColor(destCoord);
}
} }
private Piece pieceAt(Coordinate pos) { private Piece pieceAt(Coordinate pos) {
@@ -108,10 +212,12 @@ public class DDDView extends GameAdaptator {
} }
public void run() { public void run() {
/*
this.window.addRegularTask((delta) -> { this.window.addRegularTask((delta) -> {
final float angle = 1f; final float angle = 1f;
this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta); this.camera.setRotateAngle(this.camera.getRotateAngle() + angle * delta);
}); });
*/
this.window.run(); this.window.run();
// free OpenGL resources // free OpenGL resources