add 3d position cache

This commit is contained in:
2025-04-28 17:52:29 +02:00
parent 5f70daea91
commit 0c6ab1df4b
5 changed files with 90 additions and 54 deletions

View File

@@ -1,5 +1,7 @@
package chess.view.DDDrender;
import java.io.IOException;
import org.joml.Vector2f;
import org.joml.Vector3f;
@@ -8,6 +10,7 @@ import chess.controller.commands.GetPieceAtCommand;
import chess.controller.event.GameAdaptator;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Move;
import chess.model.Piece;
import chess.view.DDDrender.world.BoardEntity;
import chess.view.DDDrender.world.PieceEntity;
@@ -27,8 +30,8 @@ public class DDDView extends GameAdaptator {
public DDDView(CommandExecutor commandExecutor) {
this.commandExecutor = commandExecutor;
this.renderer = new Renderer();
this.world = new World(new Camera());
this.window = new Window(this.renderer, this.world);
this.world = new World();
this.window = new Window(this.renderer, this.world, new Camera());
}
// Invoked when a cell is clicked
@@ -43,7 +46,7 @@ public class DDDView extends GameAdaptator {
Piece p = pieceAt(coordinate);
if (p == null)
return;
this.world.getEntity(p).setColor(new Vector3f(1, 0, 0));
this.world.getPiece(coordinate).setColor(new Vector3f(1, 0, 0));
}
// Invoked when a cell is not hovered anymore
@@ -52,7 +55,7 @@ public class DDDView extends GameAdaptator {
Piece p = pieceAt(coordinate);
if (p == null)
return;
this.world.getEntity(p).setColor(p.getColor() == Color.White ? WHITE : BLACK);
this.world.getPiece(coordinate).setColor(p.getColor() == Color.White ? WHITE : BLACK);
}
private Piece pieceAt(Coordinate pos) {
@@ -64,7 +67,11 @@ public class DDDView extends GameAdaptator {
@Override
public void onGameStart() {
this.window.scheduleTask(() -> {
initBoard();
try {
initBoard();
} catch (IOException e) {
e.printStackTrace();
}
// start listening to mouse events
this.window.OnCellClick.connect(this::onCellClick);
this.window.OnCellEnter.connect(this::onCellEnter);
@@ -72,7 +79,7 @@ public class DDDView extends GameAdaptator {
});
}
private void initBoard() {
private void initBoard() throws IOException {
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Coordinate pos = new Coordinate(i, j);
@@ -83,20 +90,28 @@ public class DDDView extends GameAdaptator {
Vector2f pieceBoardPos = DDDPlacement.coordinates_to_vector(pos);
Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y());
this.world.addEntity(new PieceEntity(piece, pieceWorldPos,
PieceEntity entity = new PieceEntity(piece, pieceWorldPos,
piece.getColor() == Color.White ? WHITE : BLACK,
piece.getColor() == Color.White ? 0.0f : (float) Math.PI));
piece.getColor() == Color.White ? 0.0f : (float) Math.PI);
this.world.addPiece(entity, pos);
}
}
this.boardEntity = new BoardEntity();
this.world.addEntity(this.boardEntity);
}
@Override
public void onMove(Move move) {
// update world internal positions
this.world.movePiece(this.world.getPiece(move.getStart()), move.getFinish());
}
public void run() {
// this.window.addRegularTask((delta) -> {
// final float angle = 1f;
// final Camera cam = this.world.getCamera();
// cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
// final float angle = 1f;
// final Camera cam = this.world.getCamera();
// cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
// });
this.window.run();
}