feat: working pgn simulation
All checks were successful
Linux arm64 / Build (push) Successful in 56s

This commit is contained in:
2025-05-17 17:52:30 +02:00
parent e52988e511
commit 01f0caf672
3 changed files with 45 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ package chess;
import chess.controller.CommandExecutor;
import chess.controller.commands.NewGameCommand;
import chess.model.Game;
import chess.pgn.PgnFileSimulator;
import chess.view.DDDrender.DDDView;
public class OpenGLMain {
@@ -10,8 +11,12 @@ public class OpenGLMain {
Game game = new Game();
CommandExecutor commandExecutor = new CommandExecutor(game);
PgnFileSimulator fileSimulator = new PgnFileSimulator(commandExecutor, "games/FoolCheckmate.pgn");
DDDView ddd = new DDDView(commandExecutor);
commandExecutor.addListener(ddd);
commandExecutor.addListener(fileSimulator);
commandExecutor.executeCommand(new NewGameCommand());

View File

@@ -23,7 +23,7 @@ public class Camera {
public Camera() {
this.pos = new Vector3f(0.0f, camHeight, 0.0f);
setRotateAngle(0.0f);
setRotateAngle((float) Math.PI);
}
public void move(float x, float y) {

View File

@@ -6,6 +6,7 @@ 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;
@@ -22,6 +23,7 @@ import chess.model.Piece;
import chess.view.DDDrender.world.BoardEntity;
import chess.view.DDDrender.world.PieceEntity;
import chess.view.DDDrender.world.World;
import common.Signal0;
public class DDDView extends GameAdapter implements CommandSender {
@@ -29,6 +31,7 @@ public class DDDView extends GameAdapter implements CommandSender {
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 static final Vector3f BLUE = new Vector3f(0.0f, 0.0f, 1.0f);
private final CommandExecutor commandExecutor;
private final Window window;
@@ -37,7 +40,7 @@ public class DDDView extends GameAdapter implements CommandSender {
private final Camera camera;
private Coordinate click = null;
private static final float animationTime = 1.5f; // in seconds
private static final float animationTime = 0.5f; // in seconds
private static final int animationTurns = 1;
private float moveProgress = 0.0f;
@@ -45,6 +48,8 @@ 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();
@@ -75,6 +80,7 @@ public class DDDView extends GameAdapter implements CommandSender {
}
setClick(coordinate);
previewMoves(coordinate);
// this.boardEntity.setCellColor(coordinate, BLUE);
System.out.println("First click on " + coordinate);
return;
}
@@ -127,15 +133,17 @@ public class DDDView extends GameAdapter implements CommandSender {
if (this.click == null) {
// small test turning a cell red when hovered
this.boardEntity.setCellColor(coordinate, RED);
Piece p = pieceAt(coordinate);
if (p == null)
PieceEntity pEntity = this.world.getPiece(coordinate);
if (pEntity == null)
return;
this.world.getPiece(coordinate).setColor(RED);
pEntity.setColor(RED);
List<Coordinate> allowedMoves = getPieceAllowedMoves(coordinate);
if (allowedMoves.isEmpty())
return;
for (Coordinate destCoord : allowedMoves) {
this.boardEntity.setCellColor(destCoord, RED);
this.boardEntity.setCellColor(destCoord, YELLOW);
}
}
}
@@ -147,6 +155,7 @@ public class DDDView extends GameAdapter implements CommandSender {
Piece p = pieceAt(coordinate);
if (p == null)
return;
PieceEntity pEntity = this.world.getPiece(coordinate);
if (pEntity == null)
return;
@@ -195,7 +204,20 @@ public class DDDView extends GameAdapter implements CommandSender {
this.window.OnCellExit.connect(this::onCellExit);
this.window.OnImGuiTopRender.connect(this::onHeaderRender);
this.window.OnImGuiBottomRender.connect(this::onFooterRender);
synchronized (this) {
notifyAll();
}
});
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void onHeaderRender() {
@@ -291,14 +313,14 @@ public class DDDView extends GameAdapter implements CommandSender {
this.moveProgress = 0.0f;
Consumer<Float> moveConsumer = (delta) -> {
this.moveProgress += delta;
this.moveProgress += delta / animationTime;
pieceTick(this.moveProgress, pEntity, pMove);
};
this.window.addRegularTask(moveConsumer);
try {
Thread.sleep(1000);
Thread.sleep((long) (animationTime * 1000.0f));
} catch (InterruptedException e) {
}
this.window.removeRegularTask(moveConsumer);
@@ -314,7 +336,6 @@ public class DDDView extends GameAdapter implements CommandSender {
@Override
public void onMove(Move move, boolean captured) {
move3DPiece(move);
cameraRotate();
}
private void cameraTick(float delta) {
@@ -336,6 +357,11 @@ public class DDDView extends GameAdapter implements CommandSender {
this.camera.setRotateAngle(end);
}
@Override
public void onPlayerTurn(Color color, boolean undone) {
cameraRotate();
}
public void run() {
this.window.run();
@@ -403,4 +429,9 @@ public class DDDView extends GameAdapter implements CommandSender {
openPopup(color == Color.White ? "White surrender" : "Black surrender");
openPopup(color == Color.White ? "Black victory" : "White victory");
}
@Override
public void onCastling(boolean bigCastling) {
}
}