Skip to content

Commit

Permalink
support double click
Browse files Browse the repository at this point in the history
  • Loading branch information
vuonghy2442 committed Sep 27, 2024
1 parent 07f3a2b commit 5055f3d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions js/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ export class Card {
this.#draggable = val;
}

/** @returns {number | null} */
get placeId() {
if (this.#element === null) return null;
console.log(this.container.dataset);
return parseInt(this.container.dataset.placeId);
}

deleteDOM() {
if (this.#element === null) return;
this.#element.remove();
Expand Down
18 changes: 16 additions & 2 deletions js/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ let handlingMove = false;

function moveCard(event, card) {
const origin_cont = card.container;
const origin = parseInt(origin_cont.dataset.placeId);
const origin = card.placeId;

let moving_cards = [card];

Expand Down Expand Up @@ -292,7 +292,6 @@ function moveCard(event, card) {

function onPointerDown(event) {
if (event.which !== 1 || !event.isPrimary || handlingMove) return;

const cardDOM = event.target.closest(".card");

if (cardDOM) {
Expand Down Expand Up @@ -324,4 +323,19 @@ function initGame() {
});

document.querySelector("#game_box").addEventListener("pointerdown", onPointerDown);
document.querySelector("#game_box").addEventListener("dblclick", (event) => {
const cardDOM = event.target.closest(".card");

if (cardDOM) {
const card = cardArray[parseInt(cardDOM.dataset.cardId)];
const placeId = parseInt(card.placeId);
if (game.stack[card.suit] === card.rank) {
card.moveTo(stackContainers[card.suit], 0, 0, 0);
game.makeMove(card, placeId, parseInt(stackContainers[card.suit].dataset.placeId));
}

event.preventDefault();
return;
}
});
}

0 comments on commit 5055f3d

Please sign in to comment.