Skip to content

Commit

Permalink
Added generateModelsForPiecesLegalMoveHighlights(), reducing depend…
Browse files Browse the repository at this point in the history
…ancy, and hovering over arrow indicators now highlights the jumping moves of the piece too!
  • Loading branch information
Naviary2 committed Dec 29, 2024
1 parent bf2bf94 commit 32962c4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 57 deletions.
44 changes: 7 additions & 37 deletions src/client/scripts/esm/game/rendering/arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ import camera from './camera.js';
import board from './board.js';
import math from '../../util/math.js';
import moveutil from '../../chess/util/moveutil.js';
import { createModel, createModel_Instanced } from './buffermodel.js';
import { createModel } from './buffermodel.js';
import colorutil from '../../chess/util/colorutil.js';
import jsutil from '../../util/jsutil.js';
import coordutil from '../../chess/util/coordutil.js';
import space from '../misc/space.js';
import spritesheet from './spritesheet.js';
import preferences from '../../components/header/preferences.js';
import legalmoveshapes from './legalmoveshapes.js';
// Import End

/**
Expand Down Expand Up @@ -437,32 +435,16 @@ function onPieceIndicatorHover(type, pieceCoords, direction) {

// Calculate the mesh...



// Determine what color the legal move highlights should be...
const pieceColor = colorutil.getPieceColorFromType(type);
const opponentColor = onlinegame.areInOnlineGame() ? colorutil.getOppositeColor(onlinegame.getOurColor()) : colorutil.getOppositeColor(gamefile.whosTurn);
const isOpponentPiece = pieceColor === opponentColor;
const isOurTurn = gamefile.whosTurn === pieceColor;
const color = options.getLegalMoveHighlightColor({ isOpponentPiece, isPremove: !isOurTurn });
const usingDots = preferences.getLegalMovesShape() === 'dots';

/** The vertex data OF A SINGLE INSTANCE of the NON-CAPTURING legal move highlight. Stride 6 (2 position, 4 color) */
const vertexData_NonCapture = usingDots ? legalmoveshapes.getDataLegalMoveDot(color) : legalmoveshapes.getDataLegalMoveSquare(color);
/** The instance-specific data of the NON-CAPTURING legal move highlights mesh. Stride 2 (2 instanceposition) */
const instanceData_NonCapture = [];
/** The vertex data OF A SINGLE INSTANCE of the CAPTURING legal move highlight. Stride 6 (2 position, 4 color) */
const vertexData_Capture = usingDots ? legalmoveshapes.getDataLegalMoveCornerTris(color) : legalmoveshapes.getDataLegalMoveSquare(color);
/** The instance-specific data of the CAPTURING legal move highlights mesh. Stride 2 (2 instanceposition) */
const instanceData_Capture = [];

legalmovehighlights.concatData_HighlightedMoves_Individual(instanceData_NonCapture, instanceData_Capture, thisPieceLegalMoves, gamefile);
legalmovehighlights.concatData_HighlightedMoves_Sliding(instanceData_NonCapture, instanceData_Capture, pieceCoords, thisPieceLegalMoves, gamefile);
const model_NonCapture = createModel_Instanced(vertexData_NonCapture, instanceData_NonCapture, "TRIANGLES", true);
const model_Capture = createModel_Instanced(vertexData_Capture, instanceData_Capture, "TRIANGLES", true);

const { NonCaptureModel, CaptureModel } = legalmovehighlights.generateModelsForPiecesLegalMoveHighlights(pieceCoords, thisPieceLegalMoves, color);
// Store both these objects inside piecesHoveredOver

piecesHoveredOver[key] = { legalMoves: thisPieceLegalMoves, model_NonCapture, model_Capture, color };
piecesHoveredOver[key] = { legalMoves: thisPieceLegalMoves, model_NonCapture: NonCaptureModel, model_Capture: CaptureModel, color };
}

/**
Expand Down Expand Up @@ -527,28 +509,16 @@ function regenModelsOfHoveredPieces() {
if (!Object.keys(piecesHoveredOver).length) return; // No arrows being hovered over

console.log("Updating models of hovered piece's legal moves..");
const usingDots = preferences.getLegalMovesShape() === 'dots';
const gamefile = game.getGamefile();

for (const [coordsKey, hoveredArrow] of Object.entries(piecesHoveredOver)) { // { legalMoves, model, color }
const coords = coordutil.getCoordsFromKey(coordsKey);

// Calculate the mesh...
const { NonCaptureModel, CaptureModel } = legalmovehighlights.generateModelsForPiecesLegalMoveHighlights(coords, hoveredArrow.legalMoves, hoveredArrow.color);

/** The vertex data OF A SINGLE INSTANCE of the NON-CAPTURING legal move highlight. Stride 6 (2 position, 4 color) */
const vertexData_NonCapture = usingDots ? legalmoveshapes.getDataLegalMoveDot(hoveredArrow.color) : legalmoveshapes.getDataLegalMoveSquare(hoveredArrow.color);
/** The instance-specific data of the NON-CAPTURING legal move highlights mesh. Stride 2 (2 instanceposition) */
const instanceData_NonCapture = [];
/** The vertex data OF A SINGLE INSTANCE of the CAPTURING legal move highlight. Stride 6 (2 position, 4 color) */
const vertexData_Capture = usingDots ? legalmoveshapes.getDataLegalMoveCornerTris(hoveredArrow.color) : legalmoveshapes.getDataLegalMoveSquare(hoveredArrow.color);
/** The instance-specific data of the CAPTURING legal move highlights mesh. Stride 2 (2 instanceposition) */
const instanceData_Capture = [];

legalmovehighlights.concatData_HighlightedMoves_Individual(instanceData_NonCapture, instanceData_Capture, hoveredArrow.legalMoves, gamefile);
legalmovehighlights.concatData_HighlightedMoves_Sliding(instanceData_NonCapture, instanceData_Capture, coords, hoveredArrow.legalMoves, gamefile);
// Overwrite the model inside piecesHoveredOver
hoveredArrow.model_NonCapture = createModel_Instanced(vertexData_NonCapture, instanceData_NonCapture, "TRIANGLES", true);
hoveredArrow.model_Capture = createModel_Instanced(vertexData_Capture, instanceData_Capture, "TRIANGLES", true);
hoveredArrow.model_NonCapture = NonCaptureModel;
hoveredArrow.model_Capture = CaptureModel;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import game from '../../chess/game.js';

// TO DO: MOVE TO coordutil.ts ONCE THAT'S CONVERTED TO TS
type Coords = [number,number];
// TO DO: MOVE TO colorutil.ts ONCE THAT'S CONVERTED TO TS
type Color = [number,number,number,number];


// Variables -----------------------------------------------------------------------------
Expand Down Expand Up @@ -144,7 +146,7 @@ function isPieceSelected() {
function onPieceSelected(piece: Piece, legalMoves: LegalMoves) {
pieceSelected = piece;
selectedPieceLegalMoves = legalMoves;
regenSelectedPieceHighlightsModel();
regenSelectedPieceLegalMovesHighlightsModel();
}

function onPieceUnselected() {
Expand Down Expand Up @@ -311,20 +313,38 @@ function getDimensionsOfPerspectiveViewRange(): Coords {
* and the models of pieces legal moves of which we're currently hovering over their arrow.
*/
function regenerateAll() {
regenSelectedPieceHighlightsModel();
regenSelectedPieceLegalMovesHighlightsModel();
arrows.regenModelsOfHoveredPieces();

frametracker.onVisualChange();
}

// Regenerates the model for all highlighted squares. Expensive, minimize calling this.
function regenSelectedPieceHighlightsModel() {
// Regenerates the model for all highlighted legal moves.
function regenSelectedPieceLegalMovesHighlightsModel() {
if (!isPieceSelected()) return;
// console.log("Regenerating legal moves model..");

const coords = pieceSelected!.coords;
// The model of the selected piece's legal moves
const color = options.getLegalMoveHighlightColor(); // [r,g,b,a]
console.log(color);
const { NonCaptureModel, CaptureModel } = generateModelsForPiecesLegalMoveHighlights(pieceSelected!.coords, selectedPieceLegalMoves!, color);
model_NonCapture = NonCaptureModel;
model_Capture = CaptureModel;

// The selected piece highlight model
const coords = pieceSelected!.coords;
const offsetCoord = coordutil.subtractCoordinates(coords, model_Offset);
const dataSelectedPieceHighlight = shapes.getDataQuad_Color_FromCoord(offsetCoord, color);
model_SelectedPiece = createModel(dataSelectedPieceHighlight, 2, "TRIANGLES", true);
}

/**
* Generates the renderable instanced rendering buffer models for the
* legal move highlights of the given piece's legal moves.
* @param coords - The coordinates of the piece with the provided legal moves
* @param legalMoves - The legal moves of which to generate the highlights models for.
* @param color - The color to use, which may vary depending on if the highlights are for your piece, opponent's, or a premove.
*/
function generateModelsForPiecesLegalMoveHighlights(coords: Coords, legalMoves: LegalMoves, color: Color): { NonCaptureModel: BufferModelInstanced, CaptureModel: BufferModelInstanced } {
const usingDots = preferences.getLegalMovesShape() === 'dots';

/** The vertex data OF A SINGLE INSTANCE of the NON-CAPTURING legal move highlight. Stride 6 (2 position, 4 color) */
Expand All @@ -339,20 +359,16 @@ function regenSelectedPieceHighlightsModel() {
const gamefile = game.getGamefile();

// Data of short range moves within 3 tiles
concatData_HighlightedMoves_Individual(instanceData_NonCapture, instanceData_Capture, selectedPieceLegalMoves!, gamefile);
concatData_HighlightedMoves_Individual(instanceData_NonCapture, instanceData_Capture, legalMoves!, gamefile);
// Potentially infinite data on sliding moves...
concatData_HighlightedMoves_Sliding(instanceData_NonCapture, instanceData_Capture, coords, selectedPieceLegalMoves!, gamefile);

// The selected piece highlight model
const offsetCoord = coordutil.subtractCoordinates(coords, model_Offset);
const dataSelectedPieceHighlight = shapes.getDataQuad_Color_FromCoord(offsetCoord, color);
model_SelectedPiece = createModel(dataSelectedPieceHighlight, 2, "TRIANGLES", true);
concatData_HighlightedMoves_Sliding(instanceData_NonCapture, instanceData_Capture, coords, legalMoves!, gamefile);

// The NON-CAPTURING legal move highlights model
model_NonCapture = createModel_Instanced(vertexData_NonCapture, instanceData_NonCapture, "TRIANGLES", true);

// The CAPTURING legal move highlights model
model_Capture = createModel_Instanced(vertexData_Capture, instanceData_Capture, "TRIANGLES", true);
return {
// The NON-CAPTURING legal move highlights model
NonCaptureModel: createModel_Instanced(vertexData_NonCapture, instanceData_NonCapture, "TRIANGLES", true),
// The CAPTURING legal move highlights model
CaptureModel: createModel_Instanced(vertexData_Capture, instanceData_Capture, "TRIANGLES", true),
};
}

/**
Expand Down Expand Up @@ -545,6 +561,5 @@ export default {
getOffset,
onPieceSelected,
onPieceUnselected,
concatData_HighlightedMoves_Individual,
concatData_HighlightedMoves_Sliding
generateModelsForPiecesLegalMoveHighlights,
};

0 comments on commit 32962c4

Please sign in to comment.