Cleaner fen generation + first draft of Apocalypse + a few fixes
[xogo.git] / variants / Hex / class.js
index 37bd53c..0797c91 100644 (file)
@@ -32,10 +32,12 @@ export default class HexRules extends ChessRules {
   get hasReserve() {
     return false;
   }
-
   get noAnimate() {
     return true;
   }
+  get clickOnly() {
+    return true;
+  }
 
   doClick(coords) {
     if (
@@ -72,30 +74,30 @@ export default class HexRules extends ChessRules {
     return res;
   }
 
-  genRandInitFen() {
+  genRandInitBaseFen() {
     // NOTE: size.x == size.y (square boards)
     const emptyCount = C.FenEmptySquares(this.size.x);
-    return (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0";
+    return {
+      fen: (emptyCount + "/").repeat(this.size.x).slice(0, -1) + " w 0",
+      o: {}
+    };
   }
 
-  // TODO: re-enable rotation
   getSvgChessboard() {
     // NOTE: with small margin seems nicer
     let width = 173.2 * this.size.y + 173.2 * (this.size.y-1) / 2 + 30,
         height = 50 + Math.floor(150 * this.size.x) + 30,
         min_x = -86.6 - 15,
         min_y = -100 - 15;
-//    if (this.size.rotate) {
-//      [width, height] = [height, width];
-//      [min_x, min_y] = [min_y, min_x];
-//    }
+    if (this.size.ratio < 1) {
+      // Rotate by 30 degrees to display vertically
+      [width, height] = [height, width];
+      [min_x, min_y] = [min_y, min_x];
+    }
     let board = `
       <svg
         viewBox="${min_x} ${min_y} ${width} ${height}"
-        class="chessboard_SVG"`;
-//    if (this.size.rotate)
-//      board += ` transform="rotate(90 ${min_x} ${min_y})"`
-    board += `>
+        class="chessboard_SVG">
       <defs>
         <g id="hexa">
           <polygon
@@ -104,7 +106,10 @@ export default class HexRules extends ChessRules {
           />
         </g>
       </defs>`;
-    board += "<g>";
+    board += "<g";
+    if (this.size.ratio < 1)
+      board += ` transform="rotate(30)"`
+    board += ">";
     for (let i=0; i < this.size.x; i++) {
       for (let j=0; j < this.size.y; j++) {
         board += `
@@ -117,9 +122,11 @@ export default class HexRules extends ChessRules {
           />`;
       }
     }
-    board += `</g><g style="fill:none;stroke-width:10">`;
+    board += `</g><g style="fill:none;stroke-width:10"`;
+    if (this.size.ratio < 1)
+      board += ` transform="rotate(30)"`
     // Goals: up/down/left/right
-    board += `<polyline style="stroke:red" points="`
+    board += `><polyline style="stroke:red" points="`
     for (let i=0; i<=2*this.size.y; i++)
       board += ((i-1)*86.6) + ',' + (i % 2 == 0 ? -50 : -100) + ' ';
     board += `"/><polyline style="stroke:red" points="`;
@@ -159,32 +166,15 @@ export default class HexRules extends ChessRules {
     }
   }
 
-  initMouseEvents() {
-    const mousedown = (e) => {
-      if (e.touches && e.touches.length > 1)
-        e.preventDefault();
-      const cd = this.idToCoords(e.target.id);
-      if (cd) {
-        const move = this.doClick(cd);
-        if (move)
-          this.playPlusVisual(move);
-      }
-    };
-
-    if ('onmousedown' in window)
-      document.addEventListener("mousedown", mousedown);
-    if ('ontouchstart' in window)
-      document.addEventListener("touchstart", mousedown, {passive: false});
-  }
-
   get size() {
-    const baseRatio = 1.619191; // 2801.2 / 1730, "widescreen"
-    const rotate = window.innerWidth < window.innerHeight; //"vertical screen"
+    const baseRatio = 1.6191907514450865; //2801.2 / 1730, "widescreen"
+    const rc =
+      document.getElementById(this.containerId).getBoundingClientRect();
+    const rotate = rc.width < rc.height; //"vertical screen"
     return {
       x: this.options["bsize"],
       y: this.options["bsize"],
-      ratio: rotate ? 1 / baseRatio : baseRatio,
-      rotate: rotate
+      ratio: (rotate ? 1 / baseRatio : baseRatio)
     };
   }