Skip to content

Commit

Permalink
add swap action
Browse files Browse the repository at this point in the history
  • Loading branch information
ditam committed Apr 26, 2021
1 parent 800270a commit 50bb13f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 41 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const decks = {
cards: [
{ type: 'pickaxe' },
{ type: 'pickaxe' },
{ type: 'lantern' }
{ type: 'pickaxe' },
{ type: 'pickaxe' },
{ type: 'sieve' }
]
},
player2: {
Expand Down Expand Up @@ -650,6 +652,13 @@ function playCard(cardElement, handIndex) {
el.removeClass('visible');
}, 4000);
break;
case 'sieve':
if (decks.shaft.revealedCount < 2) {
showError('Needs at least 2 revealed cards.');
return;
}
$('.card.treasure:not(.face-down)').addClass('swappable');
break;
default:
console.assert(false, 'playCard unknown card type:' + type)
break;
Expand Down Expand Up @@ -680,6 +689,37 @@ $(document).ready(function() {
playCard(card, indexInHand);
});

const swapPair = [];
playArea.on('click', '.card:not(.face-down).swappable', function() {
const card = $(this);
// note that all the shaft cards are generated, so we can count index amongst them directly
const cardIndex = card.index('.card.treasure');
card.addClass('swap-selected');
swapPair.push(cardIndex);
console.log('swap select:', card, cardIndex);
if ($('.swap-selected').length === 2) {
// swap on UI
const first = $('.swap-selected').first();
const last = $('.swap-selected').last();
const firstLeft = first.css('left');
first.css('left', last.css('left'));
last.css('left', firstLeft);
// swap in shaft
const i = swapPair[0];
const j = swapPair[1];
const tmp = {
type: decks.shaft.cards[i].type,
domElement: decks.shaft.cards[i].domElement
};
decks.shaft.cards[i] = decks.shaft.cards[j];
decks.shaft.cards[j] = tmp;
// cleanup
swapPair.pop();
swapPair.pop();
$('.card').removeClass('swap-selected');
}
});

playArea.on('click', '.card.treasure:not(.face-down):not(.unavailable):not(.forbidden)', function() {
const card = $(this);
// note that all the shaft cards are generated, so we can count index amongst them directly
Expand Down
4 changes: 4 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ body {
opacity: 0.99;
}

.card.swap-selected {
box-shadow: 6px 3px 10px green;
}

.card .cost {
position: absolute;
top: 5px;
Expand Down

0 comments on commit 50bb13f

Please sign in to comment.