diff game/js/input.js @ 11:a77857594150

adjusted tower building, added preview dragging
author ark
date 2026-10-19 17:56 +0200
parents 2c4fed8baed9
children 8df95050fc8b
line wrap: on
line diff
--- a/game/js/input.js	Mon Oct 19 16:56:38 2026 +0200
+++ b/game/js/input.js	Mon Oct 19 17:56:56 2026 +0200
@@ -8,26 +8,54 @@
 
     // how close a tap must be to the placement preview to build, css pixels
     var PREVIEW_HIT = 28;
+    // how far a press on the preview may move and still count as a tap that
+    // builds, css pixels. a longer move drags the preview
+    var DRAG_SLOP = 10;
     var REFUSED_TIME = 500;
 
     // app: {canvas, view, game(), layout(), and the screen actions play(),
     // next(), retry(), restart()}
     function attach(app) {
         var c = app.canvas;
+        // the press that moves the placement preview, followed until the
+        // pointer is released. other pointers are ignored meanwhile
+        var drag = null;
+
+        function pos(e) {
+            var r = c.getBoundingClientRect();
+            return { x: e.clientX - r.left, y: e.clientY - r.top };
+        }
+
         c.addEventListener("pointerdown", function (e) {
             e.preventDefault();
             MM.audio.unlock();
-            var r = c.getBoundingClientRect();
-            tap(app, e.clientX - r.left, e.clientY - r.top);
+            if (drag) return;
+            var p = pos(e);
+            drag = press(app, p.x, p.y);
+            if (!drag) return;
+            drag.id = e.pointerId;
+            // keep receiving the pointer when it leaves the canvas
+            try { c.setPointerCapture(e.pointerId); } catch (err) { /* not supported */ }
         });
-        // mouse only: the position for the hover highlight, and the pointer
-        // cursor over buttons
         c.addEventListener("pointermove", function (e) {
+            var p = pos(e);
+            if (drag && e.pointerId === drag.id) move(app, drag, p.x, p.y);
+            // mouse only: the position for the hover highlight, and the
+            // pointer cursor over buttons
             if (e.pointerType !== "mouse" || !app.game()) return;
-            var r = c.getBoundingClientRect();
-            var h = { x: e.clientX - r.left, y: e.clientY - r.top };
-            app.view.hover = h;
-            c.style.cursor = MM.ui.hit(app.game(), app.layout(), h.x, h.y) ? "pointer" : "";
+            app.view.hover = p;
+            c.style.cursor = MM.ui.hit(app.game(), app.layout(), p.x, p.y) ? "pointer" : "";
+        });
+        c.addEventListener("pointerup", function (e) {
+            if (!drag || e.pointerId !== drag.id) return;
+            var p = pos(e);
+            release(app, drag, p.x, p.y);
+            drag = null;
+        });
+        // the browser took the pointer, for example for a system gesture:
+        // the preview stays where it is and nothing is built
+        c.addEventListener("pointercancel", function (e) {
+            if (drag && e.pointerId === drag.id) drag = null;
         });
         c.addEventListener("pointerleave", function () {
             app.view.hover = null;
@@ -45,7 +73,25 @@
         document.addEventListener("dblclick", function (e) { e.preventDefault(); });
     }
 
-    function tap(app, x, y) {
+    // map position of css position x, y, or null outside the map
+    function mapPos(game, L, x, y) {
+        var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
+        if (mx < 0 || my < 0 || mx > game.map.width || my > game.map.height) return null;
+        return { x: mx, y: my };
+    }
+
+    // a press at css position x, y. buttons and tower selection act at once.
+    // a press on the map while a shop tower is chosen returns the drag that
+    // places the preview:
+    //   game      the game it belongs to
+    //   onPreview the press was on the preview; it builds on release unless
+    //             the pointer moved further than DRAG_SLOP
+    //   moved     the pointer moved further than DRAG_SLOP
+    //   found     a legal position was found during the drag
+    //   sx, sy    css position of the press
+    //   dx, dy    offset from the pointer to the preview in map pixels, so
+    //             a preview grabbed off centre does not jump
+    function press(app, x, y) {
         var game = app.game(), L = app.layout();
         var b = MM.ui.hit(game, L, x, y);
         if (b) {
@@ -66,16 +112,50 @@
             else if (b.id === "shop" && game.canAfford(MM.config.tower[b.tower].cost)) game.chooseShop(b.tower);
             else if (b.id === "upgrade") game.upgrade();
             else if (b.id === "sell") game.sell();
-            return;
+            return null;
+        }
+        if (game.result || game.splash) return null;
+        var m = mapPos(game, L, x, y);
+        if (!m) return null;
+        var hitR = Math.max(MM.config.placement.footprint_radius, PREVIEW_HIT / L.scale);
+        var kind = game.pressMap(m.x, m.y, hitR);
+        if (!kind) return null;
+        var d = { game: game, onPreview: kind === "preview", moved: false, found: false,
+                  sx: x, sy: y, dx: 0, dy: 0 };
+        if (d.onPreview) {
+            d.dx = game.preview.x - m.x;
+            d.dy = game.preview.y - m.y;
+            d.found = true;
+        } else {
+            d.found = game.movePreview(m.x, m.y);
         }
-        if (game.result || game.splash) return;
-        var mx = (x - L.ox) / L.scale, my = (y - L.oy) / L.scale;
-        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 };
+        return d;
+    }
+
+    // the pointer of a drag moved to css position x, y
+    function move(app, d, x, y) {
+        var game = app.game();
+        if (game !== d.game || game.result || !game.shop) return;
+        if (!d.moved && Math.hypot(x - d.sx, y - d.sy) <= DRAG_SLOP) {
+            // a press on the preview stays a tap until it moves far enough
+            if (d.onPreview) return;
+        } else {
+            d.moved = true;
         }
+        var m = mapPos(game, app.layout(), x, y);
+        if (m && game.movePreview(m.x + d.dx, m.y + d.dy)) d.found = true;
+    }
+
+    // the pointer of a drag was released at css position x, y. a tap on the
+    // preview builds; a drag that never found a legal position is refused
+    function release(app, d, x, y) {
+        var game = app.game(), L = app.layout();
+        if (game !== d.game || game.result) return;
+        var ok = d.onPreview && !d.moved ? game.build() : d.found;
+        if (ok) return;
+        MM.audio.play("placement_refused");
+        app.view.refused = { x: (x - L.ox) / L.scale, y: (y - L.oy) / L.scale,
+                             until: performance.now() + REFUSED_TIME };
     }
 
     return { attach: attach };