diff --git a/development/code/LogicAdapter.ts b/development/code/LogicAdapter.ts index ae89055a..c57af6c1 100644 --- a/development/code/LogicAdapter.ts +++ b/development/code/LogicAdapter.ts @@ -71,7 +71,7 @@ export function onActionTriggered( onPlayerAction(draggedPiece, targetPiece); } else if (targetElement.classList.contains('item')) { game.getItems().forEach((item) => { - if (comparePositions(item.position, targetElementPosition)) { + if (item.position && comparePositions(item.position, targetElementPosition)) { onPlayerAction(draggedPiece, item); } }); @@ -92,7 +92,7 @@ export function onFellOffTheBoardTriggered( const draggedElementPosition = getPositionFromSquareId(squareId, boardId); const draggedPiece = findPieceAtPosition(draggedElementPosition); - if (!draggedPiece || !isPlayerAllowedToAct(draggedPiece)) return; + if (!draggedPiece || !isPlayerAllowedToAct(draggedPiece.player)) return; onPieceFellOffTheBoard(draggedPiece); } @@ -124,7 +124,7 @@ export function onPieceSelected( const pieceElementPosition = getPositionFromSquareId(squareId, boardId); const piece = findPieceAtPosition(pieceElementPosition); - if (!piece || !isPlayerAllowedToAct(piece)) return; + if (!piece || !isPlayerAllowedToAct(piece.player)) return; highlightLegalMoves(piece, boardId); } @@ -157,6 +157,8 @@ export function destroyPieceOnBoard(piece: Piece) { } export function destroyItemOnBoard(item: Item) { + if (!item.position) return; + const itemCoordinates = item.position.coordinates; const squareId = itemCoordinates.join(','); @@ -171,6 +173,8 @@ export function spawnPieceOnBoard(piece: Piece) { } export function spawnItemOnBoard(item: Item) { + if (!item.position) return; + const itemCoordinates = item.position.coordinates; const squareId = itemCoordinates.join(','); diff --git a/development/code/logic/PieceLogic.ts b/development/code/logic/PieceLogic.ts index 6996da99..57f0f204 100644 --- a/development/code/logic/PieceLogic.ts +++ b/development/code/logic/PieceLogic.ts @@ -14,17 +14,18 @@ import { King } from './pieces/King'; import { Pawn } from './pieces/Pawn'; import { Piece } from './pieces/Pieces'; import { Position, Square } from './pieces/PiecesUtilities'; +import { Player } from './Players'; function validatePlayerAction( draggedPiece: Piece, target: Piece | Square | Item, ): boolean { - if (!isPlayerAllowedToAct(draggedPiece)) return false; + if (!isPlayerAllowedToAct(draggedPiece.player)) return false; if (draggedPiece === target) return false; - if (draggedPiece.position.boardId !== target.position.boardId) return false; + if (draggedPiece.position.boardId !== target.position?.boardId) return false; const legalMoves = draggedPiece.getLegalMoves(); - return legalMoves.some(position => comparePositions(position, target.position)); + return legalMoves.some(position => target.position && comparePositions(position, target.position)); } function getPathPositions(start: Position, end: Position): Array { @@ -55,7 +56,7 @@ function simulatePath(piece: Piece, targetPosition: Position) { pathPositions.forEach(position => { game.getItems().forEach(item => { - if (comparePositions(item.position, position)) { + if (item.position && comparePositions(item.position, position)) { onActionPieceToItem(piece, item); } }); @@ -68,7 +69,7 @@ export function onPlayerAction( draggedPiece: Piece, target: Piece | Square | Item, ) { - if (!validatePlayerAction(draggedPiece, target)) { + if (!validatePlayerAction(draggedPiece, target) || !target.position) { movePieceOnBoard(draggedPiece, draggedPiece); return; } @@ -170,8 +171,8 @@ function castle( return true; } -export function isPlayerAllowedToAct(draggedPiece: Piece) { - return draggedPiece.player === game.getCurrentPlayer(); +export function isPlayerAllowedToAct(player: Player) { + return player === game.getCurrentPlayer(); } function move( @@ -250,7 +251,7 @@ function handlePieceSpawning(targetPiece: Piece) { }); game.getItems().forEach((item) => { - if (comparePositions(targetPiece.position, item.position)) { + if (item.position && comparePositions(targetPiece.position, item.position)) { onActionPieceToItem(targetPiece, item); } }); @@ -305,7 +306,7 @@ function pieceMovedOnTrap( game.setItems(game.getItems().filter((item) => item !== trap)); destroyItemOnBoard(trap); - if (draggedPiece.position.boardId === OVERWORLD_BOARD_ID) { + if (draggedPiece.position.boardId === OVERWORLD_BOARD_ID && trap.position) { draggedPiece.position.coordinates = trap.position.coordinates; draggedPiece.position.boardId = draggedPiece.hasKilled ? HELL_BOARD_ID diff --git a/development/code/logic/items/Inventory.ts b/development/code/logic/items/Inventory.ts index e1988470..6b95b89a 100644 --- a/development/code/logic/items/Inventory.ts +++ b/development/code/logic/items/Inventory.ts @@ -1,21 +1,20 @@ import { Logger } from '../../ui/Logger'; -import { PlayerColors } from '../Players'; +import { Player } from '../Players'; import { Item } from './Items'; export class Inventory { items: Array = []; - addItem(playerColor: PlayerColors, item: Item) { + addItem(item: Item, player: Player) { this.items.push(item); - Logger.logGeneral(`${playerColor} received a ${item.name}.`); + Logger.logGeneral(`${player.color} received a ${item.name}.`); } removeItem(item: Item) { const index = this.items.indexOf(item); if (index !== -1) { this.items.splice(index, 1); - Logger.logGeneral(`${item.name} was destroyed.`); } } diff --git a/development/code/logic/items/Items.ts b/development/code/logic/items/Items.ts index bd3a7976..a3a10388 100644 --- a/development/code/logic/items/Items.ts +++ b/development/code/logic/items/Items.ts @@ -6,7 +6,8 @@ import { Player } from '../Players'; interface ItemType { name: string, resource: string, - position: Position, + price: number, + position?: Position | undefined, use: (piece: Piece) => void; drop: (player: Player) => void; } @@ -14,15 +15,22 @@ interface ItemType { export class Item implements ItemType { name: string; resource: string; - position: Position; + price: number; + position: Position | undefined; constructor( name: string, resource: string, - position: Position, + price: number, + position?: Position, ) { this.name = name; this.resource = resource; + this.price = price; + this.position = position; + } + + setPosition(position: Position) { this.position = position; } diff --git a/development/code/logic/items/PiggyBank.ts b/development/code/logic/items/PiggyBank.ts index 567acf68..0d481f8a 100644 --- a/development/code/logic/items/PiggyBank.ts +++ b/development/code/logic/items/PiggyBank.ts @@ -2,21 +2,21 @@ import { Item } from './Items'; import { Piece } from '../pieces/Pieces'; import { Logger } from '../../ui/Logger'; import { coinResource } from '../../ui/Resources'; -import { Position } from '../pieces/PiecesUtilities'; import { Player } from '../Players'; +import { Position } from '../pieces/PiecesUtilities'; export class PiggyBank extends Item { - constructor(position: Position) { - super('piggy bank', coinResource, position); + constructor(position?: Position) { + super('piggy bank', coinResource, 1, position); } - getRandomAmountOfCoins(player: Player, max: number | undefined) { + getRandomAmountOfCoins(max?: number) { max = max || 5; return Math.floor(Math.random() * (max - 1)) + 1; } use(piece: Piece): void { - const amountOfCoins = this.getRandomAmountOfCoins(piece.player, undefined); + const amountOfCoins = this.getRandomAmountOfCoins(); piece.player.gold += amountOfCoins; Logger.logGeneral(` ${piece.player.color} ${piece.name} opened a ${this.name} and recieved ${amountOfCoins} gold coins. @@ -24,7 +24,7 @@ export class PiggyBank extends Item { } drop(player: Player): void { - const amountOfCoins = this.getRandomAmountOfCoins(player, player.gold); + const amountOfCoins = this.getRandomAmountOfCoins(player.gold); player.gold -= amountOfCoins; Logger.logGeneral(`${player.color} dropped a ${this.name} and lost ${amountOfCoins} gold coins.`); } diff --git a/development/code/logic/items/Shop.ts b/development/code/logic/items/Shop.ts new file mode 100644 index 00000000..c199757d --- /dev/null +++ b/development/code/logic/items/Shop.ts @@ -0,0 +1,17 @@ +import { isPlayerAllowedToAct } from '../PieceLogic'; +import { Player } from '../Players'; +import { Item } from './Items'; +import { Trap } from './Trap'; + +export class Shop { + items: Array = [ + new Trap(), + ]; + + buy(item: Item, player: Player) { + if (isPlayerAllowedToAct(player) && player.gold >= item.price) { + player.gold -= item.price; + player.inventory.addItem(item, player); + } + } +} diff --git a/development/code/logic/items/Trap.ts b/development/code/logic/items/Trap.ts index 0fd34f91..61267e71 100644 --- a/development/code/logic/items/Trap.ts +++ b/development/code/logic/items/Trap.ts @@ -2,13 +2,13 @@ import { Item } from './Items'; import { Piece } from '../pieces/Pieces'; import { Logger } from '../../ui/Logger'; import { trapResource } from '../../ui/Resources'; -import { Position } from '../pieces/PiecesUtilities'; import { spawnItemOnBoard } from '../../LogicAdapter'; import { game } from '../../Game'; +import { Position } from '../pieces/PiecesUtilities'; export class Trap extends Item { - constructor(position: Position) { - super('trap', trapResource, position); + constructor(position?: Position) { + super('trap', trapResource, 3, position); } use(piece: Piece): void { diff --git a/development/code/logic/pieces/PiecesUtilities.ts b/development/code/logic/pieces/PiecesUtilities.ts index f21e0aa9..25289a14 100644 --- a/development/code/logic/pieces/PiecesUtilities.ts +++ b/development/code/logic/pieces/PiecesUtilities.ts @@ -26,6 +26,6 @@ export interface PieceType { export function getItemByPosition( position: Position, ): Item | undefined { - return game.getItems().find((item) => comparePositions(position, item.position)); + return game.getItems().find((item) => item.position && comparePositions(position, item.position)); } diff --git a/development/code/ui/Board.ts b/development/code/ui/Board.ts index af0badd7..dbd21025 100644 --- a/development/code/ui/Board.ts +++ b/development/code/ui/Board.ts @@ -169,6 +169,8 @@ export class ChessBoard { } spawnItemOnBoard(item: Item) { + if (!item.position) return; + const itemCoordinates = item.position.coordinates; const square = this.boardElement.querySelectorAll(`[square-id="${itemCoordinates}"]`)[0]; diff --git a/development/code/ui/BoardManager.ts b/development/code/ui/BoardManager.ts index 0df61f83..36e8cf51 100644 --- a/development/code/ui/BoardManager.ts +++ b/development/code/ui/BoardManager.ts @@ -161,6 +161,7 @@ export function spawnPieceElementOnBoard(piece: Piece, targetSquareId: string) { } export function spawnItemElementOnBoard(item: Item, targetSquareId: string) { + if (!item.position) return; const board = getBoardbyId(item.position.boardId); const squareElement = board.boardElement.querySelectorAll(`[square-id="${targetSquareId}"]`)[0] as HTMLElement;