Skip to content

Commit

Permalink
Merge pull request #170 from Ido-Barnea/112-create-shop-initial-logic
Browse files Browse the repository at this point in the history
Initial shop logic
  • Loading branch information
Ido-Barnea authored Feb 2, 2024
2 parents 634538f + 46b0ac4 commit 8638eb8
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 26 deletions.
8 changes: 6 additions & 2 deletions development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ 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));
Expand Down Expand Up @@ -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 @@ -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
6 changes: 4 additions & 2 deletions development/code/logic/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { Piece } from './pieces/Pieces';
import { Position } from './pieces/PiecesUtilities';

export function comparePositions(
firstPosition: Position,
secondPosition: Position,
firstPosition: Position | undefined,
secondPosition: Position | undefined,
): boolean {
if (!firstPosition || !secondPosition) return false;

const arePositionsEqual =
firstPosition.coordinates[0] === secondPosition.coordinates[0] &&
firstPosition.coordinates[1] === secondPosition.coordinates[1];
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: 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 8638eb8

Please sign in to comment.