Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#109] Added inventory UI #187

Merged
merged 16 commits into from
Feb 14, 2024
Merged
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 { changeShownInventory, 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 itemPlacedDownRecently = false;
let fellOffTheBoardPiece: Piece | undefined;
let isGameFinished = false;

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

players.forEach((player) => {
initialiseInventoryUI(player.color);
});
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
}

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

Logger.logMessages();

players.forEach((player) => {
changeShownInventory(player);
});
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved

renderScreen();
itemPlacedDownRecently = false;

Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
// 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 changeItemPlacedRecently() {
itemPlacedDownRecently = true;
}

function wasItemPlacedRecently(){
return itemPlacedDownRecently;
}

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,
changeItemPlacedRecently,
wasItemPlacedRecently,
};
88 changes: 86 additions & 2 deletions 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,
destroyItemOnInventory,
} from './ui/BoardManager';
import { renderPlayersInformation } from './ui/Screen';
import { switchShownInventory, showItemOnInventory } from './ui/InventoriesUI';

export function renderScreen() {
renderPlayersInformation();
Expand Down Expand Up @@ -56,7 +58,6 @@ export function onActionTriggered(

const targetSquareId = getSquareIdByElement(targetElement);
if (!targetSquareId) return;

Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved
const targetElementPosition = getPositionFromSquareId(targetSquareId, boardId);

if (targetElement.classList.contains('piece')) {
Expand Down Expand Up @@ -194,3 +195,86 @@ export function changePieceToAnotherPlayer(piece: Piece) {
export function endGame(){
game.end();
}

export function changeShownInventory(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.wasItemPlacedRecently() || !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.changeItemPlacedRecently();

destroyItemOnInventory(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;

destroyItemOnInventory(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 @@ -3,6 +3,7 @@ export const HELL_BOARD_ID = 'board-hell';
export const HEAVEN_BOARD_ID = 'board-heaven';
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 @@ -24,3 +25,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 @@ -97,6 +97,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;
}
Monkey-bored marked this conversation as resolved.
Show resolved Hide resolved

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
Loading