feat: change board cells color
This commit is contained in:
@@ -9,6 +9,7 @@ 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;
|
||||
|
||||
@@ -21,15 +22,13 @@ public class DDDView extends GameAdaptator {
|
||||
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);
|
||||
this.window.OnCellClick.connect(this::onCellClick);
|
||||
this.window.OnCellEnter.connect(this::onCellEnter);
|
||||
this.window.OnCellExit.connect(this::onCellExit);
|
||||
}
|
||||
|
||||
// Invoked when a cell is clicked
|
||||
@@ -39,7 +38,8 @@ public class DDDView extends GameAdaptator {
|
||||
|
||||
// Invoked when a cell is hovered
|
||||
private void onCellEnter(Coordinate coordinate) {
|
||||
// small test turning a piece red when hovered
|
||||
// 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;
|
||||
@@ -48,6 +48,7 @@ public class DDDView extends GameAdaptator {
|
||||
|
||||
// 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;
|
||||
@@ -64,6 +65,9 @@ public class DDDView extends GameAdaptator {
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -83,6 +87,8 @@ public class DDDView extends GameAdaptator {
|
||||
piece.getColor() == Color.White ? 0.0f : (float) Math.PI));
|
||||
}
|
||||
}
|
||||
this.boardEntity = new BoardEntity();
|
||||
this.world.addEntity(this.boardEntity);
|
||||
}
|
||||
|
||||
public void run() {
|
||||
|
||||
@@ -6,7 +6,6 @@ import org.joml.Matrix4f;
|
||||
import org.joml.Vector3f;
|
||||
import org.lwjgl.opengl.GL30;
|
||||
|
||||
import chess.view.DDDrender.loader.BoardModelLoader;
|
||||
import chess.view.DDDrender.opengl.VertexArray;
|
||||
import chess.view.DDDrender.shader.BoardShader;
|
||||
import chess.view.DDDrender.shader.PieceShader;
|
||||
@@ -15,7 +14,6 @@ import chess.view.DDDrender.shader.ShaderProgram;
|
||||
public class Renderer {
|
||||
private BoardShader boardShader;
|
||||
private PieceShader pieceShader;
|
||||
private VertexArray boardVao;
|
||||
|
||||
public Renderer() {
|
||||
this.boardShader = new BoardShader();
|
||||
@@ -25,7 +23,6 @@ public class Renderer {
|
||||
public void Init() {
|
||||
boardShader.LoadShader();
|
||||
pieceShader.LoadShader();
|
||||
this.boardVao = BoardModelLoader.GetBoardModel();
|
||||
}
|
||||
|
||||
public void Update(Camera cam) {
|
||||
@@ -35,10 +32,6 @@ public class Renderer {
|
||||
this.pieceShader.SetCamMatrix(cam);
|
||||
}
|
||||
|
||||
public void RenderBoard() {
|
||||
RenderVao(this.boardShader, this.boardVao);
|
||||
}
|
||||
|
||||
public void Render(DDDModel model, Vector3f color, Vector3f position, float rotation) {
|
||||
this.pieceShader.Start();
|
||||
this.pieceShader.setModelColor(color);
|
||||
@@ -59,4 +52,8 @@ public class Renderer {
|
||||
GL30.glDrawElements(GL30.GL_TRIANGLES, vertexArray.GetVertexCount(), GL_UNSIGNED_INT, 0);
|
||||
vertexArray.Unbind();
|
||||
}
|
||||
|
||||
public BoardShader getBoardShader() {
|
||||
return boardShader;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,9 +126,7 @@ public class Window {
|
||||
private void render(float delta, float aspectRatio) {
|
||||
cam.setAspectRatio(aspectRatio);
|
||||
renderer.Update(cam);
|
||||
renderer.RenderBoard();
|
||||
renderWorld();
|
||||
// renderPieces();
|
||||
}
|
||||
|
||||
private void renderWorld() {
|
||||
|
||||
@@ -44,4 +44,8 @@ public class VertexArray {
|
||||
private void BindElementArrayBuffer() {
|
||||
this.elementBuffer.Bind();
|
||||
}
|
||||
|
||||
public List<VertexBuffer> getVertexBuffers() {
|
||||
return vertexBuffers;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,6 +22,12 @@ public class VertexBuffer {
|
||||
Unbind();
|
||||
}
|
||||
|
||||
public void UpdateData(int offset, float[] data) {
|
||||
Bind();
|
||||
GL30.glBufferSubData(GL30.GL_ARRAY_BUFFER, offset, data);
|
||||
Unbind();
|
||||
}
|
||||
|
||||
public void Destroy() {
|
||||
GL30.glDeleteBuffers(id);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package chess.view.DDDrender.world;
|
||||
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import chess.model.Coordinate;
|
||||
import chess.view.DDDrender.Renderer;
|
||||
import chess.view.DDDrender.loader.BoardModelLoader;
|
||||
import chess.view.DDDrender.opengl.VertexArray;
|
||||
import chess.view.DDDrender.opengl.VertexBuffer;
|
||||
|
||||
public class BoardEntity extends Entity {
|
||||
|
||||
private final VertexArray vao;
|
||||
private final VertexBuffer colorVbo;
|
||||
|
||||
private static final Vector3f WHITE = new Vector3f(1, 1, 1);
|
||||
private static final Vector3f BLACK = new Vector3f(0, 0, 0);
|
||||
|
||||
public BoardEntity() {
|
||||
this.vao = BoardModelLoader.GetBoardModel();
|
||||
this.colorVbo = this.vao.getVertexBuffers().get(1);
|
||||
}
|
||||
|
||||
public void setCellColor(Coordinate coord, Vector3f color) {
|
||||
float[] data = { color.x, color.y, color.z, color.x, color.y, color.z, color.x, color.y, color.z, color.x,
|
||||
color.y, color.z};
|
||||
int cellNumber = (Coordinate.VALUE_MAX - 1 - coord.getX()) * Coordinate.VALUE_MAX + Coordinate.VALUE_MAX - 1 - coord.getY();
|
||||
int offset = cellNumber * 4 * 4 * 3;
|
||||
this.colorVbo.UpdateData(offset, data);
|
||||
}
|
||||
|
||||
public void resetCellColor(Coordinate coord) {
|
||||
setCellColor(coord, (coord.getX() + coord.getY()) % 2 == 0 ? WHITE : BLACK);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(Renderer renderer) {
|
||||
renderer.RenderVao(renderer.getBoardShader(), vao);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user