Skip to content

Commit

Permalink
[#113] Minor fixes to shield
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Feb 20, 2024
1 parent e552a15 commit 9ccc8d1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
48 changes: 30 additions & 18 deletions development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
highlightLastMove,
getPieceElementBySquareId,
highlightLegalMove,
spawnItemOnChildElement,
} from './ui/BoardManager';
import { renderPlayersInformation } from './ui/Screen';
import { switchShownInventory, showItemOnInventory, destroyItemInInventory } from './ui/InventoriesUI';
Expand Down Expand Up @@ -177,6 +178,15 @@ export function spawnItemOnBoard(item: Item) {
spawnItemElementOnBoard(item, squareId);
}

export function spawnItemOnPiece(item: Item) {
if (!item.position) return;

const itemCoordinates = item.position.coordinates;
const squareId = itemCoordinates.join(',');

spawnItemOnChildElement(item, squareId, true);
}

export function changePieceToAnotherPlayer(piece: Piece) {
const squareId = piece.position.coordinates.join(',');
const boadrId = piece.position.boardId;
Expand Down Expand Up @@ -207,35 +217,37 @@ export function switchInventory(player: Player) {
export function canPlaceItemOnBoard(itemElement: HTMLElement, targetElement: HTMLElement): boolean {
if (game.getWasItemPlacedThisTurn() || !targetElement) return false;

switch (itemElement.id) {
case 'trap': {
if (!targetElement.classList.contains('square')) {
return false;
}
break;
}
default:
break;
}


const currentOpenBoardId = getCurrentBoardId();
if (!currentOpenBoardId) return false;
const currentBoardId = getCurrentBoardId();
if (!currentBoardId) return false;

const squareId = getSquareIdByElement(targetElement);
if (!squareId) return false;

const squarePosition = getPositionFromSquareId(squareId, currentOpenBoardId);
const squarePosition = getPositionFromSquareId(squareId, currentBoardId);

const usedItem = getCurrentPlayerInventoryItemById(itemElement.id);
if (!usedItem) return false;

usedItem.setPosition(squarePosition);
spawnItemOnBoard(usedItem);
game.addItem(usedItem);

switch (itemElement.id) {
case 'trap': {
if (!targetElement.classList.contains('square')) return false;
spawnItemOnBoard(usedItem);
game.addItem(usedItem);
break;
}
case 'shield': {
if (!targetElement.classList.contains('piece')) return false;
spawnItemOnPiece(usedItem);
break;
}
default:
break;
}

game.getCurrentPlayer().inventory.removeItem(usedItem);
game.switchWasItemPlacedThisTurn();

destroyItemInInventory(itemElement);

return true;
Expand Down
18 changes: 18 additions & 0 deletions development/code/ui/BoardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ export function spawnItemElementOnBoard(item: Item, targetSquareId: string) {
board.spawnElementOnBoard(itemElement, squareElement);
}

export function spawnItemOnChildElement(item: Item, targetSquareId: string, isUntargetable = false) {
if (!item.position) return;
const board = getBoardbyId(item.position.boardId);

const squareElement = board.boardElement.querySelectorAll(`
[square-id="${targetSquareId}"]
`)[0] as HTMLElement;

const childElement = squareElement.firstChild as HTMLElement;

const itemElement = board.createItemElement(item);
if (isUntargetable) {
itemElement.classList.remove('item');
itemElement.classList.add('untargetable-item');
}
childElement.insertBefore(itemElement, childElement.firstChild);
}

function findSquareElement(element: HTMLElement): HTMLElement | undefined {
while (element && !element.classList.contains('square')) {
element = element.parentElement as HTMLElement;
Expand Down
2 changes: 1 addition & 1 deletion development/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ path {
z-index: 9;
}

.piece, .item, .inventory-item, .shop-item {
.piece, .item, .inventory-item, .shop-item, .untargetable-item {
height: var(--square-size);
width: var(--square-size);
position: absolute;
Expand Down

0 comments on commit 9ccc8d1

Please sign in to comment.