Skip to content

Commit

Permalink
feat: open castle menu on single click
Browse files Browse the repository at this point in the history
  • Loading branch information
kacperwyczawski committed Aug 24, 2024
1 parent b0c44af commit 8f371fb
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 54 deletions.
5 changes: 2 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ <h1>Arx Chess</h1>
expanding your forces and upgrading your castles. -->Have fun!
</li>
<li>
To buy new pieces: <span class="touch">long-press</span><span class="no-touch">right-click
on</span> the castle.
To buy new pieces: click on the castle.
</li>
<li>
To move a piece: <span class="no-touch">left-</span>click on the piece, then on the desired destination.
To move a piece: click on the piece, then on the desired destination.
</li>
<li><a href="/pieces.html">All pieces</a></li>
<li><a href="https://github.com/kacperwyczawski/arx-chess">Source code</a></li>
Expand Down
18 changes: 0 additions & 18 deletions play.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,6 @@ menu {
}
}

span.touch {
display: none;
}

span.no-touch {
display: inline;
}

@media not (hover: hover) {
span.touch {
display: inline;
}

span.no-touch {
display: none;
}
}

@keyframes blink {
0% {
opacity: 1;
Expand Down
8 changes: 4 additions & 4 deletions src/game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ export default class Game {
return piece.getAvailableMoves(this.board, point);
}

getPiecesToBuy(point: Point): { piece: Piece; available: boolean }[] {
getPiecesToBuy(): { piece: Piece; isAvailable: boolean }[] {
const unlocked = getAllPieces(this.currentPlayer.color).filter((p) =>
this.currentPlayer.hasUnlocked(p),
);
if (this.board.cellAt(point).piece || !this.currentPlayer.canBuyPiece()) {
if (!this.currentPlayer.canBuyPiece()) {
return unlocked.map((p) => ({
piece: p,
available: false,
isAvailable: false,
}));
}
return unlocked.map((p) => ({
piece: p,
available: p.cost <= this.currentPlayer.gold,
isAvailable: p.cost <= this.currentPlayer.gold,
}));
}

Expand Down
58 changes: 29 additions & 29 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,35 +108,8 @@ function renderGame() {
tableCell.classList.remove("selected", "highlighted", "piece-to-move");
tableCell.style.setProperty("--outline", "");
tableCell.style.setProperty("--background-image-url", "");
tableCell.oncontextmenu = null;
if (cell.building === "wall") {
tableCell.classList.add("wall");
} else if (cell.building === "castle") {
if (cell.owner === game.currentPlayer) {
tableCell.oncontextmenu = (event) => {
event.preventDefault();
castleMenu.showModal();
castleMenuPieces.innerHTML = "";
for (const { piece, available } of game.getPiecesToBuy(point)) {
const li = document.createElement("li");
li.classList.add("cell");
li.style.backgroundImage = `url('/${piece.name}-${piece.color}.png')`;
if (available) {
li.onclick = () => {
game.buyPiece(point, piece);
castleMenu.close();
};
} else {
li.classList.add("not-available");
}
const div = document.createElement("div");
div.classList.add("cell-annotation");
div.textContent = piece.cost.toString();
li.appendChild(div);
castleMenuPieces.appendChild(li);
}
};
}
}
if (cell.piece) {
tableCell.style.setProperty(
Expand All @@ -153,9 +126,14 @@ function renderGame() {
}
tableCell.onclick = () => {
if (
cell.piece?.color === game.currentPlayer.color &&
!game.hasSelectedPoint
cell.piece?.color === game.currentPlayer.color
) {
if (game.hasSelectedPoint) {
for (const c of allTableCells()) {
c.classList.remove("selected", "highlighted");
game.unselect();
}
}
tableCell.classList.add("selected");
game.select(point);
for (const destination of game.getAvailableMoves(point)) {
Expand All @@ -164,6 +142,28 @@ function renderGame() {
}
} else if (tableCell.classList.contains("highlighted")) {
game.moveTo(point);
}
else if (cell.building === "castle" && cell.owner === game.currentPlayer) {
castleMenu.showModal();
castleMenuPieces.innerHTML = "";
for (const { piece, isAvailable } of game.getPiecesToBuy()) {
const li = document.createElement("li");
li.classList.add("cell");
li.style.backgroundImage = `url('/${piece.name}-${piece.color}.png')`;
if (isAvailable) {
li.onclick = () => {
game.buyPiece(point, piece);
castleMenu.close();
};
} else {
li.classList.add("not-available");
}
const div = document.createElement("div");
div.classList.add("cell-annotation");
div.textContent = piece.cost.toString();
li.appendChild(div);
castleMenuPieces.appendChild(li);
}
} else {
for (const c of allTableCells()) {
c.classList.remove("selected", "highlighted");
Expand Down

0 comments on commit 8f371fb

Please sign in to comment.