From 0c6ab1df4be634c44c48616ec8903501657b959f Mon Sep 17 00:00:00 2001 From: Persson-dev Date: Mon, 28 Apr 2025 17:52:29 +0200 Subject: [PATCH] add 3d position cache --- .../java/chess/view/DDDrender/DDDView.java | 37 ++++++++++++------ .../java/chess/view/DDDrender/Window.java | 4 +- .../view/DDDrender/world/ModelEntity.java | 39 +++++++++++++++++++ .../view/DDDrender/world/PieceEntity.java | 36 ++--------------- .../chess/view/DDDrender/world/World.java | 28 +++++++++---- 5 files changed, 90 insertions(+), 54 deletions(-) create mode 100644 app/src/main/java/chess/view/DDDrender/world/ModelEntity.java diff --git a/app/src/main/java/chess/view/DDDrender/DDDView.java b/app/src/main/java/chess/view/DDDrender/DDDView.java index c7982e5..1073735 100644 --- a/app/src/main/java/chess/view/DDDrender/DDDView.java +++ b/app/src/main/java/chess/view/DDDrender/DDDView.java @@ -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(); } diff --git a/app/src/main/java/chess/view/DDDrender/Window.java b/app/src/main/java/chess/view/DDDrender/Window.java index bf4eeb6..5ae081b 100644 --- a/app/src/main/java/chess/view/DDDrender/Window.java +++ b/app/src/main/java/chess/view/DDDrender/Window.java @@ -42,9 +42,9 @@ public class Window { public final Signal1 OnCellEnter = new Signal1<>(); public final Signal1 OnCellExit = new Signal1<>(); - public Window(Renderer renderer, World world) { + public Window(Renderer renderer, World world, Camera camera) { this.renderer = new Renderer(); - this.cam = world.getCamera(); + this.cam = camera; this.tasks = new ConcurrentLinkedDeque<>(); this.world = world; this.regularTasks = new ArrayList<>(); diff --git a/app/src/main/java/chess/view/DDDrender/world/ModelEntity.java b/app/src/main/java/chess/view/DDDrender/world/ModelEntity.java new file mode 100644 index 0000000..4304eea --- /dev/null +++ b/app/src/main/java/chess/view/DDDrender/world/ModelEntity.java @@ -0,0 +1,39 @@ +package chess.view.DDDrender.world; + +import org.joml.Vector3f; + +import chess.view.DDDrender.DDDModel; +import chess.view.DDDrender.Renderer; + +public class ModelEntity extends Entity { + + protected Vector3f position; + protected Vector3f color; + protected float rotation; + protected DDDModel model; + + public ModelEntity(DDDModel model, Vector3f position, Vector3f color, float rotation) { + this.position = position; + this.color = color; + this.rotation = rotation; + this.model = model; + } + + @Override + public void render(Renderer renderer) { + renderer.Render(model, color, position, rotation); + } + + public void setPosition(Vector3f position) { + this.position = position; + } + + public void setColor(Vector3f color) { + this.color = color; + } + + public void setRotation(float rotation) { + this.rotation = rotation; + } + +} diff --git a/app/src/main/java/chess/view/DDDrender/world/PieceEntity.java b/app/src/main/java/chess/view/DDDrender/world/PieceEntity.java index 4f8d880..7192134 100644 --- a/app/src/main/java/chess/view/DDDrender/world/PieceEntity.java +++ b/app/src/main/java/chess/view/DDDrender/world/PieceEntity.java @@ -5,51 +5,21 @@ import java.io.IOException; import org.joml.Vector3f; import chess.model.Piece; -import chess.view.DDDrender.DDDModel; -import chess.view.DDDrender.Renderer; import chess.view.DDDrender.loader.Piece3DModel; -public class PieceEntity extends Entity { +public class PieceEntity extends ModelEntity { private static final Piece3DModel modelLoader = new Piece3DModel(); private final Piece piece; - private Vector3f position; - private Vector3f color; - private float rotation; - private DDDModel model; - public PieceEntity(Piece piece, Vector3f position, Vector3f color, float rotation) { + public PieceEntity(Piece piece, Vector3f position, Vector3f color, float rotation) throws IOException { + super(modelLoader.getModel(piece), position, color, rotation); this.piece = piece; - this.position = position; - this.color = color; - this.rotation = rotation; - try { - this.model = modelLoader.getModel(piece); - } catch (IOException e) { - e.printStackTrace(); - } } public Piece getPiece() { return piece; } - - @Override - public void render(Renderer renderer) { - renderer.Render(model, color, position, rotation); - } - - public void setPosition(Vector3f position) { - this.position = position; - } - - public void setColor(Vector3f color) { - this.color = color; - } - - public void setRotation(float rotation) { - this.rotation = rotation; - } } diff --git a/app/src/main/java/chess/view/DDDrender/world/World.java b/app/src/main/java/chess/view/DDDrender/world/World.java index 0d30f44..8c98ba8 100644 --- a/app/src/main/java/chess/view/DDDrender/world/World.java +++ b/app/src/main/java/chess/view/DDDrender/world/World.java @@ -3,16 +3,19 @@ package chess.view.DDDrender.world; import java.util.ArrayList; import java.util.List; +import chess.model.Coordinate; import chess.model.Piece; -import chess.view.DDDrender.Camera; public class World { - private final Camera camera; + /** Renderable entity list */ private final List entites; - public World(Camera camera) { - this.camera = camera; + /** provides fast access to 3d pieces */ + private final PieceEntity[] pieces; + + public World() { this.entites = new ArrayList<>(); + this.pieces = new PieceEntity[Coordinate.VALUE_MAX * Coordinate.VALUE_MAX]; } public PieceEntity getEntity(Piece piece) { @@ -25,12 +28,21 @@ public class World { return null; } - public void addEntity(Entity entity) { - this.entites.add(entity); + public void addPiece(PieceEntity entity, Coordinate coordinate) { + addEntity(entity); + movePiece(entity, coordinate); + } + + public void movePiece(PieceEntity entity, Coordinate coordinate) { + pieces[coordinate.toIndex()] = entity; } - public Camera getCamera() { - return camera; + public PieceEntity getPiece(Coordinate coordinate) { + return pieces[coordinate.toIndex()]; + } + + public void addEntity(Entity entity) { + this.entites.add(entity); } public List getEntites() {