feat: working castling
All checks were successful
Linux arm64 / Build (push) Successful in 47s

This commit is contained in:
2025-05-17 18:51:23 +02:00
parent 01f0caf672
commit 646eb6492e
11 changed files with 63 additions and 44 deletions

View File

@@ -11,7 +11,7 @@ public class OpenGLMain {
Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(game);
PgnFileSimulator fileSimulator = new PgnFileSimulator(commandExecutor, "games/FoolCheckmate.pgn");
PgnFileSimulator fileSimulator = new PgnFileSimulator(commandExecutor, "games/CastlingTest.pgn");
DDDView ddd = new DDDView(commandExecutor);

View File

@@ -31,7 +31,7 @@ public class GameSimulation extends GameAdapter implements CommandSender {
}
@Override
public void onCastling(boolean bigCastling) {
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {
if (bigCastling)
sendBigCastling();
else

View File

@@ -44,6 +44,16 @@ public interface CommandSender {
return cmd.getCastlingResult();
}
default boolean canDoCastling() {
CastlingResult castlings = getAllowedCastlings();
return castlings == CastlingResult.Both || castlings == CastlingResult.Small;
}
default boolean canDoBigCastling() {
CastlingResult castlings = getAllowedCastlings();
return castlings == CastlingResult.Both || castlings == CastlingResult.Big;
}
default Piece getPieceAt(int x, int y) {
return getPieceAt(new Coordinate(x, y));
}
@@ -77,7 +87,7 @@ public interface CommandSender {
}
default CommandResult sendBigCastling() {
return sendCommand(new CastlingCommand(false));
return sendCommand(new CastlingCommand(true));
}
default CommandResult sendMove(Move move) {

View File

@@ -51,7 +51,7 @@ public class CastlingCommand extends PlayerCommand {
board.setLastMove(this.kingMove);
outputSystem.onCastling(this.bigCastling);
outputSystem.onCastling(this.bigCastling, kingMove, rookMove);
return CommandResult.Moved;
}

View File

@@ -96,8 +96,8 @@ public class AsyncGameDispatcher extends GameDispatcher {
}
@Override
public void onCastling(boolean bigCastling) {
asyncForEachCall((l) -> l.onCastling(bigCastling));
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {
asyncForEachCall((l) -> l.onCastling(bigCastling, kingMove, rookMove));
}
@Override

View File

@@ -60,7 +60,7 @@ public class EmptyGameDispatcher extends GameDispatcher {
}
@Override
public void onCastling(boolean bigCastling) {
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {
}
@Override

View File

@@ -47,7 +47,7 @@ public abstract class GameAdapter implements GameListener {
public void onDraw() {}
@Override
public void onCastling(boolean bigCastling) {}
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {}
@Override
public void onPawnPromoted(PromoteType promotion) {}

View File

@@ -90,8 +90,10 @@ public interface GameListener {
* Invoked when a castling is done
*
* @param bigCastling if it's queen side castling
* @param kingMove the king's move
* @param rookMove the rook's move
*/
void onCastling(boolean bigCastling);
void onCastling(boolean bigCastling, Move kingMove, Move rookMove);
/**
* Invoked when a pawn is promoted

View File

@@ -1,12 +1,12 @@
package chess.view.DDDrender;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import chess.controller.commands.*;
import chess.controller.commands.GetAllowedCastlingsCommand.CastlingResult;
import imgui.ImGui;
import imgui.type.ImBoolean;
import org.joml.Vector2f;
@@ -48,8 +48,6 @@ public class DDDView extends GameAdapter implements CommandSender {
private String waitingPopup = null;
private final ImBoolean popupOpened = new ImBoolean(false);
public final Signal0 OnReady = new Signal0();
public DDDView(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
this.world = new World();
@@ -225,14 +223,13 @@ public class DDDView extends GameAdapter implements CommandSender {
}
private void onFooterRender() {
CastlingResult allowedCastlings = getAllowedCastlings();
ImGui.beginDisabled(allowedCastlings == CastlingResult.None || allowedCastlings == CastlingResult.Big);
ImGui.beginDisabled(!canDoCastling());
if (ImGui.button("Roque")) {
sendCastling();
}
ImGui.endDisabled();
ImGui.sameLine();
ImGui.beginDisabled(allowedCastlings == CastlingResult.None || allowedCastlings == CastlingResult.Small);
ImGui.beginDisabled(!canDoBigCastling());
if (ImGui.button("Grand Roque")) {
sendBigCastling();
}
@@ -303,39 +300,52 @@ public class DDDView extends GameAdapter implements CommandSender {
piece.setPosition(bezierCurve(start, top, end, progress));
}
private void move3DPiece(Move move) {
Vector2f pieceDestBoardPos = DDDPlacement.coordinatesToVector(move.getFinish());
Vector3f pieceDestWorldPos = new Vector3f(pieceDestBoardPos.x(), 0, pieceDestBoardPos.y());
final PieceEntity pEntity = this.world.getPiece(move.getStart());
final Move pMove = move;
private void move3DPieces(List<Move> moves) {
final List<PieceEntity> pEntities = new ArrayList<>(moves.size());
final List<Consumer<Float>> consumers = new ArrayList<>(moves.size());
this.moveProgress = 0.0f;
Consumer<Float> moveConsumer = (delta) -> {
this.moveProgress += delta / animationTime;
pieceTick(this.moveProgress, pEntity, pMove);
for (Move move : moves) {
final PieceEntity pEntity = this.world.getPiece(move.getStart());
final Consumer<Float> moveConsumer = (delta) -> {
this.moveProgress += delta / animationTime / (float) moves.size();
pieceTick(this.moveProgress, pEntity, move);
};
pEntities.add(pEntity);
consumers.add(moveConsumer);
this.window.addRegularTask(moveConsumer);
}
try {
Thread.sleep((long) (animationTime * 1000.0f));
} catch (InterruptedException e) {
}
for (int i = 0; i < moves.size(); i++) {
final Move move = moves.get(i);
final Consumer<Float> moveConsumer = consumers.get(i);
final PieceEntity pEntity = pEntities.get(i);
this.window.removeRegularTask(moveConsumer);
Vector2f pieceDestBoardPos = DDDPlacement.coordinatesToVector(move.getFinish());
Vector3f pieceDestWorldPos = new Vector3f(pieceDestBoardPos.x(), 0, pieceDestBoardPos.y());
if (move.getDeadPieceCoords() != null) {
this.world.ejectPiece(move.getDeadPieceCoords());
}
pEntity.setPosition(pieceDestWorldPos);
this.world.movePiece(pEntity, move);
}
}
@Override
public void onMove(Move move, boolean captured) {
move3DPiece(move);
move3DPieces(List.of(move));
}
private void cameraTick(float delta) {
@@ -349,7 +359,7 @@ public class DDDView extends GameAdapter implements CommandSender {
Consumer<Float> rotationConsumer = this::cameraTick;
this.window.addRegularTask(rotationConsumer);
try {
Thread.sleep((long) (animationTime * 1000));
Thread.sleep((long) (animationTime * 1000.0f));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
@@ -431,7 +441,7 @@ public class DDDView extends GameAdapter implements CommandSender {
}
@Override
public void onCastling(boolean bigCastling) {
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {
move3DPieces(List.of(kingMove, rookMove));
}
}

View File

@@ -56,7 +56,7 @@ public class GameAudio extends GameAdapter {
}
@Override
public void onCastling(boolean bigCastling) {
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {
playSound("castle");
}

View File

@@ -178,11 +178,8 @@ public class Window extends JFrame implements GameListener, CommandSender {
}
private void updateButtons() {
CastlingResult castlings = getAllowedCastlings();
this.castlingButton.setEnabled(
castlings == CastlingResult.Small || castlings == CastlingResult.Both);
this.bigCastlingButton.setEnabled(
castlings == CastlingResult.Big || castlings == CastlingResult.Both);
this.castlingButton.setEnabled(canDoCastling());
this.bigCastlingButton.setEnabled(canDoBigCastling());
}
@Override
@@ -292,7 +289,7 @@ public class Window extends JFrame implements GameListener, CommandSender {
}
@Override
public void onCastling(boolean bigCastling) {}
public void onCastling(boolean bigCastling, Move kingMove, Move rookMove) {}
@Override
public void onPawnPromoted(PromoteType promotion) {}