X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=base_rules.js;h=82eec3a6cedf8a44051322be3848dc26d5a3568a;hb=cb17fed82eff544b3e8a340584dd8f61a905f06f;hp=5b4907731ce2a30df7ad7e3972350b16bf31ed12;hpb=cc2c71836442d7495ec570f8f78c006ea60852e0;p=xogo.git diff --git a/base_rules.js b/base_rules.js index 5b49077..82eec3a 100644 --- a/base_rules.js +++ b/base_rules.js @@ -760,7 +760,7 @@ export default class ChessRules { } updateReserve(color, piece, count) { - if (this.options["cannibal"] && C.CannibalKing[piece]) + if (this.options["cannibal"] && C.CannibalKings[piece]) piece = "k"; //capturing cannibal king: back to king form const oldCount = this.reserve[color][piece]; this.reserve[color][piece] = count; @@ -876,6 +876,8 @@ export default class ChessRules { startPiece, curPiece = null, sqSize; const mousedown = (e) => { + // Disable zoom on smartphones: + if (e.touches && e.touches.length > 1) e.preventDefault(); r = container.getBoundingClientRect(); sqSize = this.getSquareWidth(r.width); const square = this.idToCoords(e.target.id); @@ -943,9 +945,10 @@ export default class ChessRules { document.addEventListener("mouseup", mouseup); } if ('ontouchstart' in window) { - document.addEventListener("touchstart", mousedown); - document.addEventListener("touchmove", mousemove); - document.addEventListener("touchend", mouseup); + // https://stackoverflow.com/a/42509310/12660887 + document.addEventListener("touchstart", mousedown, {passive: false}); + document.addEventListener("touchmove", mousemove, {passive: false}); + document.addEventListener("touchend", mouseup, {passive: false}); } } @@ -1391,6 +1394,7 @@ export default class ChessRules { return; //king isn't captured this way } const steps = pieces[p].attack || pieces[p].steps; + if (!steps) return; //cannibal king for example (TODO...) const range = pieces[p].range; steps.forEach(s => { // From x,y: revert step @@ -1542,11 +1546,19 @@ export default class ChessRules { addPawnMoves([x1, y1], [x2, y2], moves, promotions) { let finalPieces = ["p"]; const color = this.getColor(x1, y1); + const oppCol = C.GetOppCol(color); const lastRank = (color == "w" ? 0 : this.size.x - 1); if (x2 == lastRank && (!this.options["rifle"] || this.board[x2][y2] == "")) { // promotions arg: special override for Hiddenqueen variant - if (promotions) finalPieces = promotions; + if ( + this.options["cannibal"] && + this.board[x2][y2] != "" && + this.getColor(x2, y2) == oppCol + ) { + finalPieces = [this.getPieceType(x2, y2)]; + } + else if (promotions) finalPieces = promotions; else if (this.pawnSpecs.promotions) finalPieces = this.pawnSpecs.promotions; } @@ -2159,20 +2171,21 @@ export default class ChessRules { }; animateRec(0); }; - const checkDisplayThenAnimate = () => { + // Delay if user wasn't focused: + const checkDisplayThenAnimate = (delay) => { if (boardContainer.style.display == "none") { alert("New move! Let's go back to game..."); document.getElementById("gameInfos").style.display = "none"; boardContainer.style.display = "block"; setTimeout(launchAnimation, 700); } - else launchAnimation(); //focused user! + else setTimeout(launchAnimation, delay || 0); }; let boardContainer = document.getElementById("boardContainer"); if (document.hidden) { document.onvisibilitychange = () => { document.onvisibilitychange = undefined; - checkDisplayThenAnimate(); + checkDisplayThenAnimate(700); }; } else checkDisplayThenAnimate();