2020-01-15 18:01:07 +01:00
|
|
|
import * as Card from GUI.Card;
|
2020-01-16 16:45:23 +01:00
|
|
|
import * as RiverCard from GUI.Card.RiverCard;
|
|
|
|
import * as TurnedCard from GUI.Card.TurnedCard;
|
2020-01-15 18:01:07 +01:00
|
|
|
import * as State from GUI.Screen.Game.State;
|
|
|
|
import * as Async from UnitJS.Async;
|
|
|
|
import * as Dom from UnitJS.Dom;
|
|
|
|
|
|
|
|
var queue = [];
|
|
|
|
|
|
|
|
return {
|
|
|
|
animate: animate,
|
|
|
|
catchUp: catchUp,
|
|
|
|
delay: delay,
|
|
|
|
getQueue: getQueue
|
|
|
|
};
|
|
|
|
|
|
|
|
function catchUp() {
|
|
|
|
if(queue.length > 0) {
|
|
|
|
var length = queue.length;
|
|
|
|
Async.run.apply(null, queue.concat(
|
|
|
|
Async.apply(function() {
|
|
|
|
queue = queue.slice(length);
|
|
|
|
catchUp();
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function delay(animation) {
|
|
|
|
queue.push(animation);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getQueue() {
|
|
|
|
return queue;
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:09:06 +01:00
|
|
|
function animate(movement) {
|
|
|
|
return Async.bind(
|
|
|
|
Async.apply(cardMoves, movement),
|
|
|
|
function(movingCards) {
|
|
|
|
return Async.parallel.apply(null, movingCards);
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleCaptures(movement, movingCards, side) {
|
|
|
|
if(movement.captures != undefined) {
|
|
|
|
var dest = State.sets[side];
|
|
|
|
movingCards.push(moveCard('river', dest, Card.make(movement.captures)));
|
|
|
|
return {card: Card.make(movement.played), dest: dest};
|
|
|
|
} else {
|
|
|
|
return {card: RiverCard.make(movement.played), dest: State.sets.river};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleSource(movement, movingCards, side, cardLeft) {
|
|
|
|
if(movement.source == 'Hand') {
|
|
|
|
movingCards.push(moveCard(side, cardLeft.dest, cardLeft.card));
|
|
|
|
} else {
|
|
|
|
var cardSet = {};
|
|
|
|
var name = cardLeft.card.value.name;
|
|
|
|
cardSet[name] = State.state.turnedCard || TurnedCard.make(name);
|
|
|
|
State.state.turnedCard = null;
|
|
|
|
movingCards.push(
|
|
|
|
moveCard({card: cardSet, dom: State.dom.deck}, cardLeft.dest, cardLeft.card)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cardMoves(movement) {
|
|
|
|
var card;
|
|
|
|
var movingCards = [];
|
|
|
|
var side = (State.state.playing) ? 'you' : 'them';
|
|
|
|
var cardLeft = handleCaptures(movement, movingCards, side);
|
|
|
|
handleSource(movement, movingCards, side, cardLeft);
|
|
|
|
return movingCards;
|
|
|
|
}
|
|
|
|
|
2020-01-15 18:01:07 +01:00
|
|
|
function insertCard(toSet, card) {
|
|
|
|
if(toSet.dom != undefined) {
|
2020-01-16 16:45:23 +01:00
|
|
|
toSet.card[card.value.name] = card;
|
2020-01-15 18:01:07 +01:00
|
|
|
toSet.dom.appendChild(card.dom);
|
|
|
|
} else {
|
|
|
|
insertCard(toSet[card.value.family.class], card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 23:09:06 +01:00
|
|
|
function getSource(fromSet, card) {
|
|
|
|
var source, origin;
|
|
|
|
if(fromSet == 'river') {
|
|
|
|
source = State.sets.river;
|
|
|
|
} else if(typeof fromSet == 'object') {
|
|
|
|
source = fromSet;
|
2020-01-15 18:01:07 +01:00
|
|
|
} else {
|
2020-01-18 23:09:06 +01:00
|
|
|
source = State.sets[fromSet].hand;
|
|
|
|
if(fromSet == 'them') {
|
|
|
|
origin = source.dom.children[source.dom.children.length - 1];
|
|
|
|
}
|
2020-01-15 18:01:07 +01:00
|
|
|
}
|
2020-01-18 23:09:06 +01:00
|
|
|
origin = origin || source.card[card.value.name].dom;
|
|
|
|
delete source.card[card.value.name];
|
|
|
|
return {dom: source.dom, origin: origin};
|
|
|
|
}
|
|
|
|
|
|
|
|
function offsetInPlaceOf(referenceCard, targetCard) {
|
|
|
|
var from = referenceCard.getBoundingClientRect();
|
|
|
|
var to = targetCard.getBoundingClientRect();
|
|
|
|
targetCard.style.left = (from.left - to.left) + 'px';
|
|
|
|
targetCard.style.top = (from.top - to.top) + 'px';
|
|
|
|
targetCard.classList.add('moving');
|
|
|
|
targetCard.style.visibility = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function resetPostion(card) {
|
|
|
|
return function() {card.dom.style.left = 0; card.dom.style.top = 0;};
|
|
|
|
}
|
|
|
|
|
|
|
|
function cleanUpMove(source, card, slot) {
|
|
|
|
return function() {
|
|
|
|
source.dom.removeChild(slot);
|
|
|
|
card.dom.classList.remove('moving');
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function moveCard(fromSet, toSet, card) {
|
|
|
|
var source = getSource(fromSet, card);
|
|
|
|
var slot = Dom.make('li', {class: ['card', 'slot']});
|
|
|
|
source.dom.replaceChild(slot, source.origin);
|
2020-01-15 18:01:07 +01:00
|
|
|
card.dom.style.visibility = 'hidden';
|
|
|
|
insertCard(toSet, card);
|
2020-01-18 23:09:06 +01:00
|
|
|
offsetInPlaceOf(slot, card.dom)
|
2020-01-15 18:01:07 +01:00
|
|
|
return Async.sequence(
|
|
|
|
Async.wait(10),
|
2020-01-18 23:09:06 +01:00
|
|
|
Async.apply(resetPostion(card)),
|
2020-01-15 18:01:07 +01:00
|
|
|
Async.wait(1000),
|
2020-01-18 23:09:06 +01:00
|
|
|
Async.apply(cleanUpMove(source, card, slot))
|
2020-01-15 18:01:07 +01:00
|
|
|
);
|
|
|
|
}
|