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(); Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(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); DDDView ddd = new DDDView(commandExecutor);

View File

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

View File

@@ -44,6 +44,16 @@ public interface CommandSender {
return cmd.getCastlingResult(); 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) { default Piece getPieceAt(int x, int y) {
return getPieceAt(new Coordinate(x, y)); return getPieceAt(new Coordinate(x, y));
} }
@@ -77,7 +87,7 @@ public interface CommandSender {
} }
default CommandResult sendBigCastling() { default CommandResult sendBigCastling() {
return sendCommand(new CastlingCommand(false)); return sendCommand(new CastlingCommand(true));
} }
default CommandResult sendMove(Move move) { default CommandResult sendMove(Move move) {

View File

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

View File

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

View File

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

View File

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

View File

@@ -90,8 +90,10 @@ public interface GameListener {
* Invoked when a castling is done * Invoked when a castling is done
* *
* @param bigCastling if it's queen side castling * @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 * Invoked when a pawn is promoted

View File

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

View File

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