Factorize code for turned cards and make sure selected hand cards are unselected after being played
This commit is contained in:
parent
c21a8f6512
commit
0abc020d13
1 changed files with 23 additions and 20 deletions
43
www/game.js
43
www/game.js
|
@ -174,7 +174,7 @@ function Game(modules) {
|
|||
movingCards.push([sets[side].hand, dest, card]);
|
||||
} else {
|
||||
var cardSet = {};
|
||||
cardSet[card.name] = turnedCard(card.name);
|
||||
cardSet[card.name] = new TurnedCard(card.name);
|
||||
movingCards.push([{card: cardSet, dom: deck}, dest, card]);
|
||||
}
|
||||
return movingCards;
|
||||
|
@ -300,18 +300,11 @@ function Game(modules) {
|
|||
}
|
||||
}
|
||||
|
||||
function turnedCard(cardName) {
|
||||
var card = new Card(cardName);
|
||||
card.dom.id = "turned";
|
||||
deck.appendChild(card.dom);
|
||||
return card;
|
||||
}
|
||||
|
||||
function setTurned(cardName) {
|
||||
turnedCard(cardName);
|
||||
turnedCard = new TurnedCard(cardName);
|
||||
if(status.playing) {
|
||||
selected = cardName;
|
||||
showCandidates(modules.hanafuda.Card[selected], true);
|
||||
selected = turnedCard;
|
||||
showCandidates(modules.hanafuda.Card[cardName], true);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -319,6 +312,12 @@ function Game(modules) {
|
|||
matchingInRiver(card).forEach(function(riverCard) {riverCard.setCandidate(yes);});
|
||||
}
|
||||
|
||||
function setSelected(yes) {
|
||||
selected = yes ? this : null;
|
||||
this.dom.classList.toggle('selected', yes);
|
||||
showCandidates(this.value, yes);
|
||||
}
|
||||
|
||||
function Card(name) {
|
||||
this.value = modules.hanafuda.Card[name];
|
||||
this.name = name;
|
||||
|
@ -343,9 +342,8 @@ function Game(modules) {
|
|||
var card = this;
|
||||
return function() {
|
||||
if(card.candidate) {
|
||||
var withCard = selected;
|
||||
selected = null;
|
||||
showCandidates(card.value, false);
|
||||
var withCard = selected.name;
|
||||
selected.setSelected(false);
|
||||
play(
|
||||
status.step == 'ToPlay' ? {capture: [withCard, card.name]} : {choose: card.name}
|
||||
);
|
||||
|
@ -358,6 +356,15 @@ function Game(modules) {
|
|||
this.dom.classList.toggle("candidate", yes);
|
||||
}
|
||||
|
||||
function TurnedCard() {
|
||||
Card.apply(this, arguments);
|
||||
this.dom.id = "turned";
|
||||
deck.appendChild(this.dom);
|
||||
}
|
||||
|
||||
TurnedCard.prototype.onClick = Card.prototype.onClick;
|
||||
TurnedCard.prototype.setSelected = setSelected;
|
||||
|
||||
function HandCard() {
|
||||
Card.apply(this, arguments);
|
||||
}
|
||||
|
@ -367,7 +374,7 @@ function Game(modules) {
|
|||
return function() {
|
||||
if(status.playing && status.step == "ToPlay") {
|
||||
if(selected != undefined) {
|
||||
sets.you.hand.card[selected].setSelected(false);
|
||||
selected.setSelected(false);
|
||||
} else {
|
||||
card.play();
|
||||
}
|
||||
|
@ -375,11 +382,7 @@ function Game(modules) {
|
|||
};
|
||||
};
|
||||
|
||||
HandCard.prototype.setSelected = function(yes) {
|
||||
selected = yes ? this.name : null;
|
||||
this.dom.classList.toggle('selected', yes);
|
||||
showCandidates(this.value, yes);
|
||||
}
|
||||
HandCard.prototype.setSelected = setSelected;
|
||||
|
||||
HandCard.prototype.play = function() {
|
||||
var matching = matchingInRiver(this.value);
|
||||
|
|
Loading…
Reference in a new issue