/* ===================================================================== swipe-controls.js — finger-swipe controls for the Sport on Earth Games arcade. mega run 3, Phase 5. Loaded once on the arcade page after the game scripts. A swipe that starts on a game's canvas is translated into that game's own keyboard controls (synthetic keydown/keyup on window — every arcade game self-gates by its playing state, so a stray event is harmless), which keeps keyboard/mouse play intact and gives every game directional swipes with zero per-game forks. Per-game semantics: - board-battle (Box Out): swipe UP = JUMP (Space). ◀ ▶ hold-move. - fantasy-faceoff: swipes run (arrow holds); PASS/SHOOT are the game's own big on-screen buttons. - pinball: swipe ◀ holds the left flipper (Z), ▶ the right (/), ▼ pulls the plunger (ArrowDown), ▲ = Space. - lane-bandit: lanes are tap-to-jump; a swipe synthesizes a tap one lane over (the library tracks the current lane from taps). - full-court-press / three-point-shootout: directional holds. - the Space-action shooting games: swipe ▲ taps Space (shoot), ◀ ▶ dispatch arrow holds where the game uses them. ===================================================================== */ (function () { "use strict"; var MIN = 26; // px before a touch counts as a swipe function key(code, k) { return { code: code, key: k }; } var ARROWS = { left: [key("ArrowLeft", "ArrowLeft")], right: [key("ArrowRight", "ArrowRight")], up: [key("ArrowUp", "ArrowUp")], down: [key("ArrowDown", "ArrowDown")] }; var SPACE = [key("Space", " ")]; var CONFIG = { "board-battle": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 260 }, "fantasy-faceoff": { left: ARROWS.left, right: ARROWS.right, up: ARROWS.up, down: ARROWS.down, holdMs: 300 }, "full-court-press": { left: ARROWS.left, right: ARROWS.right, up: ARROWS.up, down: ARROWS.down, holdMs: 260 }, "three-point-shootout": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 150 }, "pinball": { left: [key("KeyZ", "z")], right: [key("Slash", "/")], up: SPACE, down: ARROWS.down, holdMs: 220 }, "lane-bandit": { special: "lanes" }, "around-the-world": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 200 }, "buzzer-beater": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 200 }, "dunk-contest": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 200 }, "free-throw-frenzy": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 200 }, "hoops-blitz": { left: ARROWS.left, right: ARROWS.right, up: SPACE, down: ARROWS.down, holdMs: 200 } }; function fireKey(k, type) { try { window.dispatchEvent(new KeyboardEvent(type, { code: k.code, key: k.key, bubbles: true, cancelable: true })); } catch (e) { /* older engines: ignore */ } } function tapKeys(list, holdMs) { (list || []).forEach(function (k) { fireKey(k, "keydown"); }); setTimeout(function () { (list || []).forEach(function (k) { fireKey(k, "keyup"); }); }, holdMs || 70); } function gameIdOf(el) { var n = el; while (n && n !== document) { if (n.getAttribute) { if (n.getAttribute("data-hoops-game")) return n.getAttribute("data-hoops-game"); if (n.id && CONFIG[n.id]) return n.id; } n = n.parentNode; } return null; } /* Lane Bandit: track the player's lane from canvas taps (the canvas is 4 equal lanes) so a swipe can tap one lane over. */ var lbLane = 1; function laneFromEvent(cv, clientX) { var r = cv.getBoundingClientRect(); if (!r.width) return lbLane; var lane = Math.floor(((clientX - r.left) / r.width) * 4); return Math.max(0, Math.min(3, lane)); } function laneTap(cv, lane) { var r = cv.getBoundingClientRect(); var x = r.left + (lane + 0.5) * (r.width / 4); var y = r.top + r.height / 2; try { cv.dispatchEvent(new PointerEvent("pointerdown", { clientX: x, clientY: y, bubbles: true, cancelable: true, pointerId: 9871, isPrimary: true })); } catch (e) { /* ignore */ } lbLane = lane; } var start = null; document.addEventListener("touchstart", function (e) { if (e.touches.length !== 1) { start = null; return; } var t = e.target; if (!t || t.tagName !== "CANVAS") { start = null; return; } var gid = gameIdOf(t); if (!gid) { start = null; return; } var touch = e.touches[0]; start = { x: touch.clientX, y: touch.clientY, cv: t, gid: gid }; if (gid === "lane-bandit") lbLane = laneFromEvent(t, touch.clientX); }, { passive: true }); document.addEventListener("touchend", function (e) { if (!start) return; var touch = e.changedTouches && e.changedTouches[0]; var s = start; start = null; if (!touch) return; var dx = touch.clientX - s.x, dy = touch.clientY - s.y; if (Math.max(Math.abs(dx), Math.abs(dy)) < MIN) return; // a tap: the game handles it var dir = Math.abs(dx) > Math.abs(dy) ? (dx > 0 ? "right" : "left") : (dy > 0 ? "down" : "up"); var cfg = CONFIG[s.gid]; if (!cfg) return; if (cfg.special === "lanes") { if (dir === "left") laneTap(s.cv, lbLane - 1); else if (dir === "right") laneTap(s.cv, lbLane + 1); else if (dir === "up") tapKeys(SPACE, 70); return; } if (cfg[dir]) tapKeys(cfg[dir], cfg.holdMs); }, { passive: true }); })();