add 3d position cache
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
package chess.view.DDDrender;
|
package chess.view.DDDrender;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.joml.Vector2f;
|
import org.joml.Vector2f;
|
||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
@@ -8,6 +10,7 @@ import chess.controller.commands.GetPieceAtCommand;
|
|||||||
import chess.controller.event.GameAdaptator;
|
import chess.controller.event.GameAdaptator;
|
||||||
import chess.model.Color;
|
import chess.model.Color;
|
||||||
import chess.model.Coordinate;
|
import chess.model.Coordinate;
|
||||||
|
import chess.model.Move;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.view.DDDrender.world.BoardEntity;
|
import chess.view.DDDrender.world.BoardEntity;
|
||||||
import chess.view.DDDrender.world.PieceEntity;
|
import chess.view.DDDrender.world.PieceEntity;
|
||||||
@@ -27,8 +30,8 @@ public class DDDView extends GameAdaptator {
|
|||||||
public DDDView(CommandExecutor commandExecutor) {
|
public DDDView(CommandExecutor commandExecutor) {
|
||||||
this.commandExecutor = commandExecutor;
|
this.commandExecutor = commandExecutor;
|
||||||
this.renderer = new Renderer();
|
this.renderer = new Renderer();
|
||||||
this.world = new World(new Camera());
|
this.world = new World();
|
||||||
this.window = new Window(this.renderer, this.world);
|
this.window = new Window(this.renderer, this.world, new Camera());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoked when a cell is clicked
|
// Invoked when a cell is clicked
|
||||||
@@ -43,7 +46,7 @@ public class DDDView extends GameAdaptator {
|
|||||||
Piece p = pieceAt(coordinate);
|
Piece p = pieceAt(coordinate);
|
||||||
if (p == null)
|
if (p == null)
|
||||||
return;
|
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
|
// Invoked when a cell is not hovered anymore
|
||||||
@@ -52,7 +55,7 @@ public class DDDView extends GameAdaptator {
|
|||||||
Piece p = pieceAt(coordinate);
|
Piece p = pieceAt(coordinate);
|
||||||
if (p == null)
|
if (p == null)
|
||||||
return;
|
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) {
|
private Piece pieceAt(Coordinate pos) {
|
||||||
@@ -64,7 +67,11 @@ public class DDDView extends GameAdaptator {
|
|||||||
@Override
|
@Override
|
||||||
public void onGameStart() {
|
public void onGameStart() {
|
||||||
this.window.scheduleTask(() -> {
|
this.window.scheduleTask(() -> {
|
||||||
|
try {
|
||||||
initBoard();
|
initBoard();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
// start listening to mouse events
|
// start listening to mouse events
|
||||||
this.window.OnCellClick.connect(this::onCellClick);
|
this.window.OnCellClick.connect(this::onCellClick);
|
||||||
this.window.OnCellEnter.connect(this::onCellEnter);
|
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 i = 0; i < Coordinate.VALUE_MAX; i++) {
|
||||||
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
for (int j = 0; j < Coordinate.VALUE_MAX; j++) {
|
||||||
Coordinate pos = new Coordinate(i, j);
|
Coordinate pos = new Coordinate(i, j);
|
||||||
@@ -83,15 +90,23 @@ public class DDDView extends GameAdaptator {
|
|||||||
Vector2f pieceBoardPos = DDDPlacement.coordinates_to_vector(pos);
|
Vector2f pieceBoardPos = DDDPlacement.coordinates_to_vector(pos);
|
||||||
Vector3f pieceWorldPos = new Vector3f(pieceBoardPos.x(), 0, pieceBoardPos.y());
|
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 ? 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.boardEntity = new BoardEntity();
|
||||||
this.world.addEntity(this.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() {
|
public void run() {
|
||||||
// this.window.addRegularTask((delta) -> {
|
// this.window.addRegularTask((delta) -> {
|
||||||
// final float angle = 1f;
|
// final float angle = 1f;
|
||||||
|
|||||||
@@ -42,9 +42,9 @@ public class Window {
|
|||||||
public final Signal1<Coordinate> OnCellEnter = new Signal1<>();
|
public final Signal1<Coordinate> OnCellEnter = new Signal1<>();
|
||||||
public final Signal1<Coordinate> OnCellExit = new Signal1<>();
|
public final Signal1<Coordinate> OnCellExit = new Signal1<>();
|
||||||
|
|
||||||
public Window(Renderer renderer, World world) {
|
public Window(Renderer renderer, World world, Camera camera) {
|
||||||
this.renderer = new Renderer();
|
this.renderer = new Renderer();
|
||||||
this.cam = world.getCamera();
|
this.cam = camera;
|
||||||
this.tasks = new ConcurrentLinkedDeque<>();
|
this.tasks = new ConcurrentLinkedDeque<>();
|
||||||
this.world = world;
|
this.world = world;
|
||||||
this.regularTasks = new ArrayList<>();
|
this.regularTasks = new ArrayList<>();
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -5,51 +5,21 @@ import java.io.IOException;
|
|||||||
import org.joml.Vector3f;
|
import org.joml.Vector3f;
|
||||||
|
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.view.DDDrender.DDDModel;
|
|
||||||
import chess.view.DDDrender.Renderer;
|
|
||||||
import chess.view.DDDrender.loader.Piece3DModel;
|
import chess.view.DDDrender.loader.Piece3DModel;
|
||||||
|
|
||||||
public class PieceEntity extends Entity {
|
public class PieceEntity extends ModelEntity {
|
||||||
|
|
||||||
private static final Piece3DModel modelLoader = new Piece3DModel();
|
private static final Piece3DModel modelLoader = new Piece3DModel();
|
||||||
|
|
||||||
private final Piece piece;
|
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.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() {
|
public Piece getPiece() {
|
||||||
return piece;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,16 +3,19 @@ package chess.view.DDDrender.world;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import chess.model.Coordinate;
|
||||||
import chess.model.Piece;
|
import chess.model.Piece;
|
||||||
import chess.view.DDDrender.Camera;
|
|
||||||
|
|
||||||
public class World {
|
public class World {
|
||||||
private final Camera camera;
|
/** Renderable entity list */
|
||||||
private final List<Entity> entites;
|
private final List<Entity> entites;
|
||||||
|
|
||||||
public World(Camera camera) {
|
/** provides fast access to 3d pieces */
|
||||||
this.camera = camera;
|
private final PieceEntity[] pieces;
|
||||||
|
|
||||||
|
public World() {
|
||||||
this.entites = new ArrayList<>();
|
this.entites = new ArrayList<>();
|
||||||
|
this.pieces = new PieceEntity[Coordinate.VALUE_MAX * Coordinate.VALUE_MAX];
|
||||||
}
|
}
|
||||||
|
|
||||||
public PieceEntity getEntity(Piece piece) {
|
public PieceEntity getEntity(Piece piece) {
|
||||||
@@ -25,12 +28,21 @@ public class World {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addEntity(Entity entity) {
|
public void addPiece(PieceEntity entity, Coordinate coordinate) {
|
||||||
this.entites.add(entity);
|
addEntity(entity);
|
||||||
|
movePiece(entity, coordinate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Camera getCamera() {
|
public void movePiece(PieceEntity entity, Coordinate coordinate) {
|
||||||
return camera;
|
pieces[coordinate.toIndex()] = entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PieceEntity getPiece(Coordinate coordinate) {
|
||||||
|
return pieces[coordinate.toIndex()];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addEntity(Entity entity) {
|
||||||
|
this.entites.add(entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Entity> getEntites() {
|
public List<Entity> getEntites() {
|
||||||
|
|||||||
Reference in New Issue
Block a user