Skip to content

Commit

Permalink
[#112] Initial shop logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido-Barnea committed Feb 2, 2024
1 parent 12b8f01 commit 6efcedf
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 29 deletions.
10 changes: 7 additions & 3 deletions development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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(',');

Expand All @@ -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(',');

Expand Down
19 changes: 10 additions & 9 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Position> {
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -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;
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions development/code/logic/items/Inventory.ts
Original file line number Diff line number Diff line change
@@ -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<Item> = [];

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.`);
}
}

Expand Down
14 changes: 11 additions & 3 deletions development/code/logic/items/Items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@ 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;
}

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;
}

Expand Down
12 changes: 6 additions & 6 deletions development/code/logic/items/PiggyBank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ 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.
`);
}

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.`);
}
Expand Down
17 changes: 17 additions & 0 deletions development/code/logic/items/Shop.ts
Original file line number Diff line number Diff line change
@@ -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<Item> = [
new Trap(),
];

buy(item: Item, player: Player) {
if (isPlayerAllowedToAct(player) && player.gold >= item.price) {
player.gold -= item.price;
player.inventory.addItem(item, player);
}
}
}
6 changes: 3 additions & 3 deletions development/code/logic/items/Trap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion development/code/logic/pieces/PiecesUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

2 changes: 2 additions & 0 deletions development/code/ui/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
1 change: 1 addition & 0 deletions development/code/ui/BoardManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6efcedf

Please sign in to comment.