add CommanderSender (Fixes #9)
All checks were successful
Linux arm64 / Build (push) Successful in 36s

This commit is contained in:
2025-05-17 16:48:04 +02:00
parent b33e333276
commit 90daf662ea
6 changed files with 179 additions and 164 deletions

View File

@@ -11,9 +11,9 @@ import imgui.type.ImBoolean;
import org.joml.Vector2f;
import org.joml.Vector3f;
import chess.controller.Command;
import chess.controller.Command.CommandResult;
import chess.controller.CommandExecutor;
import chess.controller.CommandSender;
import chess.controller.event.GameAdapter;
import chess.model.Color;
import chess.model.Coordinate;
@@ -23,10 +23,12 @@ import chess.view.DDDrender.world.BoardEntity;
import chess.view.DDDrender.world.PieceEntity;
import chess.view.DDDrender.world.World;
public class DDDView extends GameAdapter {
public class DDDView extends GameAdapter implements CommandSender {
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 RED = new Vector3f(1.0f, 0.0f, 0.0f);
private static final Vector3f YELLOW = new Vector3f(1.0f, 1.0f, 0.0f);
private final CommandExecutor commandExecutor;
private final Window window;
@@ -50,6 +52,11 @@ public class DDDView extends GameAdapter {
this.window = new Window(new Renderer(), this.world, this.camera);
}
@Override
public CommandExecutor getCommandExecutor() {
return this.commandExecutor;
}
private void cancelClick() {
this.click = null;
}
@@ -58,19 +65,10 @@ public class DDDView extends GameAdapter {
this.click = coordinate;
}
private CommandResult sendCommand(Command command) {
return this.commandExecutor.executeCommand(command);
}
// Invoked when a cell is clicked
private void onCellClick(Coordinate coordinate) {
if (this.click == null) { // case: first click
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) { // case: invalid piece to move
System.out.println("Nothing to do here.");
return;
}
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty()) { // case: no movement possible for piece
System.out.println("This piece cannot be moved at the moment.");
return;
@@ -98,7 +96,7 @@ public class DDDView extends GameAdapter {
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)));
sendMove(new Move(click, coordinate));
cancelClick();
return;
}
@@ -114,21 +112,13 @@ public class DDDView extends GameAdapter {
}
private void previewMoves(Coordinate coordinate) {
Piece p = pieceAt(coordinate);
if (p == null) {
return;
}
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed) {
return;
}
this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0));
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0));
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
this.boardEntity.setCellColor(coordinate, RED);
this.world.getPiece(coordinate).setColor(RED);
for (Coordinate destCoord : allowedMoves) {
this.boardEntity.setCellColor(destCoord, new Vector3f(1, 1, 0));
this.boardEntity.setCellColor(destCoord, YELLOW);
}
}
@@ -136,19 +126,16 @@ public class DDDView extends GameAdapter {
private void onCellEnter(Coordinate coordinate) {
if (this.click == null) {
// small test turning a cell red when hovered
this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0));
this.boardEntity.setCellColor(coordinate, RED);
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<Coordinate> allowedMoves = movesCommand.getDestinations();
this.world.getPiece(coordinate).setColor(RED);
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
for (Coordinate destCoord : allowedMoves) {
this.boardEntity.setCellColor(destCoord, new Vector3f(1, 0, 0));
this.boardEntity.setCellColor(destCoord, RED);
}
}
}
@@ -165,10 +152,7 @@ public class DDDView extends GameAdapter {
return;
pEntity.setColor(p.getColor() == Color.White ? WHITE : BLACK);
GetAllowedMovesPieceCommand movesCommand = new GetAllowedMovesPieceCommand(coordinate);
if (sendCommand(movesCommand) == CommandResult.NotAllowed)
return;
List<Coordinate> allowedMoves = movesCommand.getDestinations();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
for (Coordinate destCoord : allowedMoves) {
@@ -183,10 +167,7 @@ public class DDDView extends GameAdapter {
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();
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
for (Coordinate destCoord : allowedMoves) {
@@ -223,15 +204,15 @@ public class DDDView extends GameAdapter {
private void onFooterRender() {
if (ImGui.button("Roque")) {
sendCommand(new CastlingCommand(false));
sendCastling();
}
ImGui.sameLine();
if (ImGui.button("Grand Roque")) {
sendCommand(new CastlingCommand(true));
sendBigCastling();
}
ImGui.sameLine();
if (ImGui.button("Annuler le coup précédent")) {
sendCommand(new UndoCommand());
sendUndo();
}
openPopup();
renderPopups();
@@ -279,7 +260,7 @@ public class DDDView extends GameAdapter {
private Function<Float, Float> lagrangeInterpolation(Vector3f begin, Vector3f middle, Vector3f end) {
return (t) -> {
return t+1.0f;
return t + 1.0f;
};
}
@@ -288,7 +269,8 @@ public class DDDView extends GameAdapter {
Vector2f pieceStartBoard = DDDPlacement.coordinatesToVector(move.getStart());
Vector2f pieceDestinationBoard = DDDPlacement.coordinatesToVector(move.getFinish());
Vector3f start = new Vector3f(pieceStartBoard.x(), 0, pieceStartBoard.y());
Vector3f top = new Vector3f(pieceDestinationBoard.x() - pieceStartBoard.x(), height, pieceDestinationBoard.y() - pieceStartBoard.y());
Vector3f top = new Vector3f(pieceDestinationBoard.x() - pieceStartBoard.x(), height,
pieceDestinationBoard.y() - pieceStartBoard.y());
Vector3f end = new Vector3f(pieceDestinationBoard.x(), 0, pieceDestinationBoard.y());
piece.setPosition(bezierCurve(start, top, end, progress));
@@ -362,14 +344,14 @@ public class DDDView extends GameAdapter {
}
private void renderPopup(String title, String text) {
if(ImGui.beginPopupModal(title, popupOpened)){
if (ImGui.beginPopupModal(title, popupOpened)) {
ImGui.text(text);
ImGui.endPopup();
}
}
private void renderPopups() {
renderPopup("Check","Your king is in check");
renderPopup("Check", "Your king is in check");
renderPopup("Checkmate", "Checkmate, it's a win!");
renderPopup("Promotion", "Please promote your pawn.");
renderPopup("Pat", "It's a pat!");
@@ -387,7 +369,7 @@ public class DDDView extends GameAdapter {
}
@Override
public void onKingInCheck(){
public void onKingInCheck() {
openPopup("Check");
}
@@ -413,7 +395,7 @@ public class DDDView extends GameAdapter {
@Override
public void onSurrender(Color color) {
openPopup(color == Color.White ? "White surrender": "Black surrender");
openPopup(color == Color.White ? "White surrender" : "Black surrender");
openPopup(color == Color.White ? "Black victory" : "White victory");
}
}