diff game/js/input.js @ 4:2d46729a022e

sound processing and in-game sounds
author ark
date 2026-10-18 20:58 +0200
parents 5492efd60e2c
children 910b26feca76
line wrap: on
line diff
--- a/game/js/input.js	Sun Oct 18 20:09:40 2026 +0200
+++ b/game/js/input.js	Sun Oct 18 20:58:33 2026 +0200
@@ -14,9 +14,15 @@
         var c = app.canvas;
         c.addEventListener("pointerdown", function (e) {
             e.preventDefault();
+            MM.audio.unlock();
             var r = c.getBoundingClientRect();
             tap(app, e.clientX - r.left, e.clientY - r.top);
         });
+        // browsers count only some events as a user interaction that may
+        // start sound: pointerdown for the mouse, pointerup and touchend
+        // for touch
+        document.addEventListener("pointerup", MM.audio.unlock);
+        document.addEventListener("touchend", MM.audio.unlock);
         // block the context menu, double tap zoom and pinch gestures
         c.addEventListener("contextmenu", function (e) { e.preventDefault(); });
         c.addEventListener("touchstart", function (e) { e.preventDefault(); }, { passive: false });
@@ -28,6 +34,7 @@
         var game = app.game(), L = app.layout();
         var b = MM.ui.hit(game, L, x, y);
         if (b) {
+            MM.audio.play("button_tap");
             if (b.id === "restart") app.restart();
             else if (b.id === "pause") game.paused = !game.paused;
             else if (b.id === "speed") game.speedIndex = (game.speedIndex + 1) % game.speeds.length;
@@ -40,6 +47,7 @@
         if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return;
         var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
         if (!game.tapMap(mx, my, hitR)) {
+            MM.audio.play("placement_refused");
             app.view.refused = { x: mx, y: my, until: performance.now() + REFUSED_TIME };
         }
     }