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

Reordered inventory items #298

Merged
merged 3 commits into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Reordered inventory items when item is used
  • Loading branch information
Ido-Barnea committed May 4, 2024
commit 7c704b3665c4ed69bb18f216104609f2639e625d
14 changes: 6 additions & 8 deletions core/development/LogicAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,10 @@ export function canPlaceItemOnBoard(
}
}

game
.getPlayersTurnSwitcher()
.getCurrentPlayer()
.inventory.removeItem(usedItem);
const currentPlayer = game.getPlayersTurnSwitcher().getCurrentPlayer();
currentPlayer.inventory.removeItem(usedItem);
game.switchWasItemPlacedThisTurn();
destroyItemInInventory(itemElement);
destroyItemInInventory(itemElement, currentPlayer.color);

return true;
}
Expand Down Expand Up @@ -398,10 +396,10 @@ export function returnItemToInventory(itemElement: HTMLElement) {
const usedItem = getCurrentPlayerInventoryItemById(itemElement.id);
if (!usedItem) return;

destroyItemInInventory(itemElement);
const currentPlayer = game.getPlayersTurnSwitcher().getCurrentPlayer();

const player = game.getPlayersTurnSwitcher().getCurrentPlayer();
showItemOnInventory(usedItem, player.color);
destroyItemInInventory(itemElement, currentPlayer.color);
showItemOnInventory(usedItem, currentPlayer.color);
}

export function getShopItemById(itemId: string) {
Expand Down
23 changes: 22 additions & 1 deletion core/development/ui/InventoriesUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ export function showItemOnInventory(
}
}

export function sortInventoryItems(playerColor: PlayerColor) {
const playerInventoryElement = document.getElementById(playerColor);
if (!playerInventoryElement) return;

const childNodes = Array.from(playerInventoryElement.childNodes);
const emptySlots = [];

for (let i = 0; i < childNodes.length; i++) {
const child = childNodes[i];
if (!child.hasChildNodes()) {
emptySlots.push(child);
}
}

for (const emptySlot of emptySlots) {
playerInventoryElement.appendChild(emptySlot);
}
}

export function initializeInventoryUI(playerColor: PlayerColor) {
const playerInventoryElement = createPlayerInventoryElement(playerColor);

Expand Down Expand Up @@ -92,6 +111,8 @@ function removeItemElements(playerInventoryElement: HTMLElement) {
});
}

export function destroyItemInInventory(itemElement: HTMLElement) {
export function destroyItemInInventory(itemElement: HTMLElement, playerColor: PlayerColor) {
itemElement.remove();

sortInventoryItems(playerColor);
}