Skip to content

Commit

Permalink
[trivial] Remove all highighlights on click on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Feb 29, 2024
1 parent a60ace7 commit 375957e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
7 changes: 7 additions & 0 deletions core/development/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ function highlightLegalMoves(piece: Piece, boardId: string) {
}
}

export function removeAllHighlights(boardId: string) {
const allSquareElements = getAllSquareElements(boardId);
for (const squareElement of allSquareElements) {
highlightLegalMove(squareElement, false);
}
}

export function onPieceSelected(pieceElement: HTMLElement, boardId: string) {
const squareId = getSquareIdByElement(pieceElement);
if (!squareId) return;
Expand Down
33 changes: 26 additions & 7 deletions core/development/ui/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ import {
onPieceSelected,
canPlaceItemOnBoard,
returnItemToInventory,
removeAllHighlights,
} from '../LogicAdapter';
import {
HEAVEN_BOARD_BUTTON_ID,
HEAVEN_BOARD_ID,
HELL_BOARD_BUTTON_ID,
HELL_BOARD_ID,
OVERWORLD_BOARD_BUTTON_ID,
OVERWORLD_BOARD_ID,
} from '../Constants';
import { HEAVEN_BOARD, HELL_BOARD, OVERWORLD_BOARD } from './BoardManager';

Expand Down Expand Up @@ -40,15 +44,16 @@ export function initializeEventListeners() {
const pieces = document.querySelectorAll('.piece');
pieces.forEach((pieceElement) => {
pieceElement.addEventListener('mousedown', onPieceMouseDown);
pieceElement.addEventListener('click', onMouseClick);
pieceElement.addEventListener('click', onPieceMouseClick);
});

// Listen for boards' buttons clicks
OVERWORLD_BOARD_BUTTON?.addEventListener('click', handleButtonPress);
HELL_BOARD_BUTTON?.addEventListener('click', handleButtonPress);
HEAVEN_BOARD_BUTTON?.addEventListener('click', handleButtonPress);
OVERWORLD_BOARD_BUTTON?.addEventListener('click', handleBoardButtonPress);
HELL_BOARD_BUTTON?.addEventListener('click', handleBoardButtonPress);
HEAVEN_BOARD_BUTTON?.addEventListener('click', handleBoardButtonPress);

SHOP_UPGRADE_SWAPPER?.addEventListener('click', swapShopAndUpgrade);

document.addEventListener('click', onClickOnScreen);
}

function onPieceMouseDown(event: Event) {
Expand Down Expand Up @@ -200,7 +205,7 @@ export function initializeDraggingListeners(element: HTMLElement) {
}
}

function onMouseClick(event: Event) {
function onPieceMouseClick(event: Event) {
let element = event.target as HTMLElement;
// Prevent clicking if the user clicked on an untargetable area
while (element.classList.contains('untargetable')) {
Expand Down Expand Up @@ -244,7 +249,7 @@ function showBoard(boardId: string) {
boardElement.classList.remove('collapsed');
}

function handleButtonPress(event: Event) {
function handleBoardButtonPress(event: Event) {
const buttonValue = (event.target as HTMLButtonElement).value;
showBoard(buttonValue);
}
Expand Down Expand Up @@ -277,3 +282,17 @@ function swapShopAndUpgrade() {
SHOP_CONTAINER?.classList.add('collapsed');
UPGRADES_CONTAINER?.classList.remove('collapsed');
}

function onClickOnScreen(event: Event) {
let element = event.target as HTMLElement;
// Prevent clicking if the user clicked on an untargetable area
while (element.classList.contains('untargetable')) {
element = element.parentElement as HTMLElement;
}

if (element.classList.contains('piece')) return;

removeAllHighlights(OVERWORLD_BOARD_ID);
removeAllHighlights(HELL_BOARD_ID);
removeAllHighlights(HEAVEN_BOARD_ID);
}
14 changes: 8 additions & 6 deletions core/development/ui/UpgradeUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function showUpgradeablePiecesElements(
if (!upgradesContainer) return;

upgradesContainer.innerHTML = '';

if (upgradeablePieces.length === 0) {
const noAvailableUpgradesTextElement = document.createElement('p');
noAvailableUpgradesTextElement.classList.add('piece-upgrades-message');
Expand All @@ -20,25 +20,27 @@ export function showUpgradeablePiecesElements(
upgradesContainer.appendChild(noAvailableUpgradesTextElement);
} else {
upgradeablePieces.forEach((upgradeablePieceType) => {
const upgradeablePiece = new upgradeablePieceType(game.getCurrentPlayer());

const upgradeablePiece = new upgradeablePieceType(
game.getCurrentPlayer(),
);

const upgradeElement = document.createElement('div');
upgradeElement.id = upgradeablePiece.name;
upgradeElement.classList.add('upgraded-piece');
upgradeElement.innerHTML = upgradeablePiece.resource;
upgradeElement.addEventListener('click', () => {
upgradePiece(piece, upgradeablePiece);
});

const upgradePriceElement = document.createElement('p');
upgradePriceElement.classList.add('piece-upgrade-item-price');
upgradePriceElement.innerHTML = upgradeablePiece.price.toString();

const upgradeSquare = document.createElement('div');
upgradeSquare.classList.add('upgrade-square');
upgradeSquare.appendChild(upgradeElement);
upgradeSquare.appendChild(upgradePriceElement);

upgradesContainer.appendChild(upgradeSquare);
});
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"lint": "npx nx run-many --target lint",
"format": "npx nx format:write",
"test": "npx nx run-many --target test --all",
"build": "webpack --config webpack.config.ts",
"build": "webpack --mode production --config webpack.config.ts",
"start": "node server.js",
"dev": "webpack serve --config webpack.config.ts"
"dev": "webpack serve --mode development --config webpack.config.ts"
},
"author": "Ido Barnea",
"license": "Apache-2.0",
Expand Down
1 change: 0 additions & 1 deletion webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface CustomConfiguration extends webpack.Configuration {
}

const config: CustomConfiguration = {
mode: 'production',
entry: {
room: './core/development/pages/Room.ts',
},
Expand Down

0 comments on commit 375957e

Please sign in to comment.