Skip to content

Commit

Permalink
Merge pull request #187 from Ido-Barnea/backup-backup-109
Browse files Browse the repository at this point in the history
[#109] Added inventory UI
  • Loading branch information
Monkey-bored authored Feb 14, 2024
2 parents c316ee2 + 5f28804 commit 3d283cb
Show file tree
Hide file tree
Showing 22 changed files with 330 additions and 58 deletions.
29 changes: 28 additions & 1 deletion development/code/Game.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { renderScreen } from './LogicAdapter';
import { switchInventory, renderScreen } from './LogicAdapter';
import { OVERWORLD_BOARD_ID } from './logic/Constants';
import { Player, PlayerColors } from './logic/Players';
import { Item } from './logic/items/Items';
Expand All @@ -12,6 +12,7 @@ import { Rook } from './logic/pieces/Rook';
import { RulesManager } from './logic/rules/RulesManager';
import { showWinningAlert as showGameEndAlert } from './ui/Screen';
import { Logger } from './ui/logs/Logger';
import { initialiseInventoryUI } from './ui/InventoriesUI';

let rulesManager: RulesManager;
const whitePlayer = new Player(PlayerColors.WHITE);
Expand Down Expand Up @@ -59,11 +60,16 @@ let deathCounter = 0;
let isCastling = false;
let isFriendlyFire = false;
let isPieceKilled = false;
let wasItemPlacedThisTurn = false;
let fellOffTheBoardPiece: Piece | undefined;
let isGameFinished = false;

function initializeGame() {
rulesManager = new RulesManager();

players.forEach((player) => {
initialiseInventoryUI(player.color);
});
}

function endTurn() {
Expand All @@ -82,7 +88,13 @@ function endTurn() {
}

Logger.logMessages();

players.forEach((player) => {
switchInventory(player);
});

renderScreen();
wasItemPlacedThisTurn = false;

// element.remove() is scheduled to run in the next event sycle while alert() runs immedietely.
// To make sure the element is removed before displaying the winning alert, we need to add
Expand Down Expand Up @@ -144,6 +156,10 @@ function getItems(): Array<Item> {
return items;
}

function addItem(item: Item) {
items.push(item);
}

function setItems(updatedItems: Array<Item>) {
items = updatedItems;
}
Expand Down Expand Up @@ -200,6 +216,14 @@ function endGame() {
isGameFinished = true;
}

function switchWasItemPlacedThisTurn() {
wasItemPlacedThisTurn = true;
}

function getWasItemPlacedThisTurn(){
return wasItemPlacedThisTurn;
}

export const game = {
initialize: initializeGame,
end: endGame,
Expand All @@ -211,6 +235,7 @@ export const game = {
setPieces,
getItems,
setItems,
addItem,
getRoundCounter,
increaseRoundCounter,
getDeathCounter,
Expand All @@ -222,4 +247,6 @@ export const game = {
setIsPieceKilled,
getFellOffTheBoardPiece,
setFellOffTheBoardPiece,
switchWasItemPlacedThisTurn,
getWasItemPlacedThisTurn,
};
86 changes: 85 additions & 1 deletion development/code/LogicAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { game } from './Game';
import { isPlayerAllowedToAct, onPieceFellOffTheBoard, onPlayerAction } from './logic/PieceLogic';
import { PlayerColors } from './logic/Players';
import { Player, PlayerColors } from './logic/Players';
import { comparePositions, convertSquareIdToPosition } from './logic/Utilities';
import { Item } from './logic/items/Items';
import { Piece } from './logic/pieces/Piece';
Expand All @@ -15,8 +15,10 @@ import {
spawnPieceElementOnBoard,
highlightLastMove,
getPieceElementBySquareId,
destroyItemInInventory,
} from './ui/BoardManager';
import { renderPlayersInformation } from './ui/Screen';
import { switchShownInventory, showItemOnInventory } from './ui/InventoriesUI';

export function renderScreen() {
renderPlayersInformation();
Expand Down Expand Up @@ -194,3 +196,85 @@ export function changePieceToAnotherPlayer(piece: Piece) {
export function endGame(){
game.end();
}

export function switchInventory(player: Player) {
if (switchShownInventory(player.color)) {
player.inventory.items.forEach((item) => {
showItemOnInventory(item,player.color);
});
}
}

export function placeItemOnBoard(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 squareId = getSquareIdByElement(targetElement);
if (!squareId) return false;

const squarePosition = getPositionFromSquareId(squareId, currentOpenBoardId);


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

usedItem.position = squarePosition;
spawnItemOnBoard(usedItem);
game.addItem(usedItem);
game.getCurrentPlayer().inventory.removeItem(usedItem);
game.switchWasItemPlacedThisTurn();

destroyItemInInventory(itemElement);

return true;
}

export function getCurrentBoardId(): string | undefined {
const boards = document.getElementsByClassName('board');
let currentOpenBoard;
for (let i = 0; i < boards.length; i++) {
if (!boards[i].classList.contains('collapsed')) {
currentOpenBoard = boards[i];
break;
}
}

return currentOpenBoard?.id;
}

export function getCurrentPlayerItemById(itemId: string): Item | undefined {
const player = game.getCurrentPlayer();
let draggedItem = undefined;
player.inventory.items.forEach((item) => {
if (item.name === itemId) {
draggedItem = item;
return;
}
});

return draggedItem;
}

export function returnItemToInventory(itemElement: HTMLElement) {
const usedItem = getCurrentPlayerItemById(itemElement.id);
if (!usedItem) return;

destroyItemInInventory(itemElement);

const player = game.getCurrentPlayer();
showItemOnInventory(usedItem, player.color);
}
2 changes: 2 additions & 0 deletions development/code/logic/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const HEAVEN_BOARD_ID = 'board-heaven';
export const VOID_BOARD_ID = 'board-void';
export const BOTTOM_NOTATION_ID = 'bottom-notations';
export const LEFT_NOTATION_ID = 'left-notations';
export const INVENTORY_CLASS_ID = 'inventory-container';

export const OVERWORLD_BOARD_BUTTON_ID = 'board-overworld-button';
export const HELL_BOARD_BUTTON_ID = 'board-hell-button';
Expand All @@ -25,3 +26,4 @@ export const NOTATIONS_NUMBERS = ['8', '7', '6', '5', '4', '3', '2', '1'];
export const BOARD_WIDTH = 8;

export const MIN_KILLINGS_FOR_BOUNTY = 3;
export const INVENTORY_WIDTH = 4;
1 change: 1 addition & 0 deletions development/code/logic/PieceLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export function onPlayerAction(
const targetSquare = (target instanceof Item)
? { position: target.position }
: (target as Square);

onActionNonAttackMove(draggedPiece, targetSquare);
}
}
Expand Down
6 changes: 3 additions & 3 deletions development/code/logic/Players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ export enum PlayerColors {
};

export class Player implements PlayerType {
color: string;
color: PlayerColors;
xp: number;
gold: number;
inDebtForTurns: number;
inventory: Inventory;

constructor(color: string) {
constructor(color: PlayerColors) {
this.color = color;
this.xp = 0;
this.gold = 0;
this.inDebtForTurns = 0;
this.inventory = new Inventory();
this.inventory = new Inventory(this);
}
}
22 changes: 8 additions & 14 deletions development/code/logic/items/Inventory.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { Log } from '../../ui/logs/Log';
import { INVENTORY_CLASS_ID, INVENTORY_WIDTH } from '../Constants';
import { Player } from '../Players';
import { Item } from './Items';

export class Inventory {
items: Array<Item> = [];
player: Player;

addItem(item: Item, player: Player) {
constructor(player: Player) {
this.player = player;
}

addItem(item: Item) {
this.items.push(item);
new Log(`${player.color} received a ${item.name}.`).addToQueue();
new Log(`${this.player.color} received a ${item.name}.`).addToQueue();
}

removeItem(item: Item) {
Expand All @@ -16,16 +22,4 @@ export class Inventory {
this.items.splice(index, 1);
}
}

toHTMLElement(): HTMLElement {
const inventoryElement = document.createElement('ul');
this.items.forEach((item) => {
const inventoryItemElement = document.createElement('li');
inventoryItemElement.innerHTML = item.name;

inventoryElement.appendChild(inventoryItemElement);
});

return inventoryElement;
}
}
5 changes: 5 additions & 0 deletions development/code/logic/items/Items.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
2 changes: 1 addition & 1 deletion development/code/logic/items/Shop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class Shop {
buy(item: Item, player: Player) {
if (isPlayerAllowedToAct(player) && player.gold >= item.price) {
player.gold -= item.price;
player.inventory.addItem(item, player);
player.inventory.addItem(item);
}
}
}
5 changes: 5 additions & 0 deletions development/code/logic/pieces/Bishop.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
5 changes: 5 additions & 0 deletions development/code/logic/pieces/King.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
5 changes: 5 additions & 0 deletions development/code/logic/pieces/Knight.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
5 changes: 5 additions & 0 deletions development/code/logic/pieces/Pawn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
5 changes: 5 additions & 0 deletions development/code/logic/pieces/Queen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
5 changes: 5 additions & 0 deletions development/code/logic/pieces/Rook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ jest.mock('../../ui/Screen.ts', () => ({
}));
jest.mock('../../ui/logs/Logger.ts');
jest.mock('../../ui/Events.ts', () => ({}));
jest.mock('../../ui/InventoriesUI.ts', () => ({
initialiseInventoryUI: jest.fn(),
switchShownInventory: jest.fn(),
showItemOnInventory: jest.fn(),
}));

game.getCurrentPlayer = jest.fn().mockReturnValue(whitePlayer);

Expand Down
4 changes: 2 additions & 2 deletions development/code/ui/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PiggyBank } from '../logic/items/PiggyBank';
import { Item } from '../logic/items/Items';
import { Piece } from '../logic/pieces/Piece';
import { Position } from '../logic/pieces/PiecesUtilities';
import { dragPieceElement } from './Events';
import { dragElement } from './Events';

export class ChessBoard {
boardId: string;
Expand Down Expand Up @@ -130,7 +130,7 @@ export class ChessBoard {

pieceElement.innerHTML = piece.resource;

dragPieceElement(pieceElement);
dragElement(pieceElement);

return pieceElement;
}
Expand Down
Loading

0 comments on commit 3d283cb

Please sign in to comment.