Files
3DChess/app/src/main/java/chess/view/DDDrender/DDDView.java

104 lines
3.1 KiB
Java

package chess.view.DDDrender;
import org.joml.Vector2f;
import org.joml.Vector3f;
import chess.controller.CommandExecutor;
import chess.controller.commands.GetPieceAtCommand;
import chess.controller.event.GameAdaptator;
import chess.model.Color;
import chess.model.Coordinate;
import chess.model.Piece;
import chess.view.DDDrender.world.BoardEntity;
import chess.view.DDDrender.world.PieceEntity;
import chess.view.DDDrender.world.World;
public class DDDView extends GameAdaptator {
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 final CommandExecutor commandExecutor;
private final Window window;
private final Renderer renderer;
private final World world;
private BoardEntity boardEntity;
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);
}
// Invoked when a cell is clicked
private void onCellClick(Coordinate coordinate) {
System.out.println("Cell clicked : " + coordinate);
}
// Invoked when a cell is hovered
private void onCellEnter(Coordinate coordinate) {
// small test turning a cell red when hovered
this.boardEntity.setCellColor(coordinate, new Vector3f(1, 0, 0));
Piece p = pieceAt(coordinate);
if (p == null)
return;
this.world.getEntity(p).setColor(new Vector3f(1, 0, 0));
}
// Invoked when a cell is not hovered anymore
private void onCellExit(Coordinate coordinate) {
this.boardEntity.resetCellColor(coordinate);
Piece p = pieceAt(coordinate);
if (p == null)
return;
this.world.getEntity(p).setColor(p.getColor() == Color.White ? WHITE : BLACK);
}
private Piece pieceAt(Coordinate pos) {
GetPieceAtCommand cmd = new GetPieceAtCommand(pos);
this.commandExecutor.executeCommand(cmd);
return cmd.getPiece();
}
@Override
public void onGameStart() {
this.window.scheduleTask(() -> {
initBoard();
this.window.OnCellClick.connect(this::onCellClick);
this.window.OnCellEnter.connect(this::onCellEnter);
this.window.OnCellExit.connect(this::onCellExit);
});
}
private void initBoard() {
for (int i = 0; i < Coordinate.VALUE_MAX; i++) {
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
Coordinate pos = new Coordinate(i, j);
Piece piece = pieceAt(pos);
if (piece == null)
continue;
Vector2f pieceBoardPos = DDDPlacement.coordinates_to_vector(pos);
Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y());
this.world.addEntity(new PieceEntity(piece, pieceWorldPos,
piece.getColor() == Color.White ? WHITE : BLACK,
piece.getColor() == Color.White ? 0.0f : (float) Math.PI));
}
}
this.boardEntity = new BoardEntity();
this.world.addEntity(this.boardEntity);
}
public void run() {
// this.window.addRegularTask((delta) -> {
// final float angle = 1f;
// final Camera cam = this.world.getCamera();
// cam.setRotateAngle(cam.getRotateAngle() + angle * delta);
// });
this.window.run();
}
}