changeset 11:a77857594150

adjusted tower building, added preview dragging
author ark
date 2026-10-19 17:56 +0200
parents 22b604680c5a
children dc314bfbb1cf
files doc/architecture.md doc/game_design.md doc/roadmap.md game/js/game.js game/js/input.js game/js/render.js game/js/ui.js
diffstat 7 files changed, 157 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/doc/architecture.md	Mon Oct 19 16:56:38 2026 +0200
+++ b/doc/architecture.md	Mon Oct 19 17:56:56 2026 +0200
@@ -79,7 +79,7 @@
 
     ff_screenshot -w 1024,768 -s 2 -S 3 -J 'document.getElementById("game")
         .dispatchEvent(new PointerEvent("pointerdown",
-        {clientX: 512, clientY: 444, bubbles: true}))' \
+        {clientX: 497, clientY: 560, bubbles: true}))' \
         file://$PWD/game/index.html temp/play.png
 
 `ff_screenshot -h` lists the other options.
@@ -252,13 +252,16 @@
 - nothing relies on hover; selection and confirmation are by tap. with a
   mouse, hover enlarges the screen buttons and shows the pointer cursor over
   any button
+- buttons and tower selection act when the pointer goes down. placing
+  follows the pointer until it is let go, and building happens then
 - the page blocks browser zoom and scroll gestures on the canvas
 
 placement:
 
 - the build's placement grid marks every cell where a tower may stand
-- on a tap, the game searches outward from the tap for the nearest legal cell
-  that keeps the minimum distance to other towers, within the snap radius
+- while the pointer is down, the game searches outward from it for the
+  nearest legal cell that keeps the minimum distance to other towers, within
+  the snap radius. where there is none, the preview stays where it was
 - t0 checks once, at placement, which of its four sides face the path, and
   only draws spikes on those
 
--- a/doc/game_design.md	Mon Oct 19 16:56:38 2026 +0200
+++ b/doc/game_design.md	Mon Oct 19 17:56:56 2026 +0200
@@ -68,9 +68,13 @@
   more targets
 - towers are placed next to the path, never on it. a tap or click snaps to the
   nearest legal position, so the player does not have to hit an exact spot
-- placing: pick a tower in the shop, tap the map. a preview with its range
-  appears at the snapped position; tapping the preview builds the tower,
-  tapping elsewhere moves the preview (suggestion)
+- placing: pick a tower in the shop, press on the map and drag. a preview
+  with its range follows, snapped to the nearest legal position, and stays
+  where the pointer is let go. tapping the preview builds the tower.
+  dragging the preview, or pressing elsewhere and dragging, moves it again.
+  changed after user testing, where players wanted to drag the preview
+- the preview pulses in time with the chosen shop item. some testers took
+  the preview for a built tower
 - tapping a built tower shows its range and the upgrade and sell buttons
 - tower upgrades cost tower base cost * tower level
     the level is the new level: building is level 1 and costs the base cost,
--- a/doc/roadmap.md	Mon Oct 19 16:56:38 2026 +0200
+++ b/doc/roadmap.md	Mon Oct 19 17:56:56 2026 +0200
@@ -192,11 +192,19 @@
   another button or the map, cancels it; there is no timeout
 - the pause refund counts pauses: each purchase records the pause it was
   made in. a purchase made during the current pause is refunded in full
-- placement: a tap within the footprint radius or 28 css pixels of the
-  preview builds. the shop choice is cleared after building, and a second tap
-  on the chosen shop button cancels it. a tap on a built tower selects it and
-  shows its range. a refused tap shows a red cross for 0.5 s. shop buttons
-  the player cannot afford are dimmed and do nothing
+- placement: a press on the map puts the preview at the nearest legal
+  position, and the preview follows the pointer until it is let go. only
+  the first pointer is followed. a press within the footprint radius or 28
+  css pixels of the preview grabs it: let go after moving at most 10 css
+  pixels, it builds; a longer move drags it, keeping its offset to the
+  pointer. the shop choice is cleared after building, and a second tap on
+  the chosen shop button cancels it. a tap on a built tower selects it and
+  shows its range. a press that finds no legal position shows a red cross
+  for 0.5 s where the pointer is let go. shop buttons the player cannot
+  afford are dimmed and do nothing
+- the preview is drawn at 45 % to 80 % opacity and 100 % to 108 % size. it
+  grows while the chosen shop item shows shop_bubble_dark and shrinks while
+  it shows shop_bubble_dark2. the range circle does not pulse
 - flying coins: one sprite per coin, at most 8, started 0.05 s apart, flying
   in game time on an arc. they are dropped when the map ends
 - top bar: coin_icon, heart and monster_icon with their counters. each
--- a/game/js/game.js	Mon Oct 19 16:56:38 2026 +0200
+++ b/game/js/game.js	Mon Oct 19 17:56:56 2026 +0200
@@ -156,35 +156,47 @@
         this.shop = this.shop === type ? null : type;
     };
 
-    // a tap on the map at map position x, y. hitRadius is how close the tap
-    // must be to the preview to build. returns false when no legal position
-    // was found for a preview.
-    Game.prototype.tapMap = function (x, y, hitRadius) {
+    // a press on the map at map position x, y. hitRadius is how close the
+    // press must be to the preview to count as a press on it. returns
+    // "preview" for a press on the preview, "place" for any other press
+    // while a shop tower is chosen, and null when the press selected or
+    // deselected a built tower.
+    Game.prototype.pressMap = function (x, y, hitRadius) {
+        var onPreview = this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius;
         var t = this.towerAt(x, y);
         this.sellArmed = false;
-        if (t && !(this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius)) {
+        if (t && !onPreview) {
             this.shop = null;
             this.preview = null;
             this.selected = t === this.selected ? null : t;
-            return true;
+            return null;
         }
         this.selected = null;
-        if (!this.shop) return true;
-        if (this.preview && Math.hypot(this.preview.x - x, this.preview.y - y) <= hitRadius) {
-            return this.build();
-        }
-        this.preview = this.map.snap(x, y, this.towers);
-        if (!this.preview) return false;
+        if (!this.shop) return null;
+        return onPreview ? "preview" : "place";
+    };
+
+    // move the preview to the nearest legal position to map position x, y.
+    // where there is none, the preview stays where it was. returns false
+    // when no legal position was found.
+    Game.prototype.movePreview = function (x, y) {
+        if (!this.shop) return false;
+        var pv = this.map.snap(x, y, this.towers);
+        if (!pv) return false;
+        if (this.preview && this.preview.x === pv.x && this.preview.y === pv.y) return true;
         // t0 draws spikes only towards the path, judged by its top level range
         var def = cfg.tower[this.shop];
         if (def.projectile_speed === 0) {
-            this.preview.sides = this.map.facingSides(this.preview.x, this.preview.y,
-                                                      def.range[def.range.length - 1]);
+            pv.sides = this.map.facingSides(pv.x, pv.y, def.range[def.range.length - 1]);
         }
+        this.preview = pv;
         return true;
     };
 
+    // build the shop tower at the preview. returns false when there is no
+    // preview or the tower cannot be afforded.
     Game.prototype.build = function () {
+        if (!this.shop || !this.preview) return false;
         var cost = cfg.tower[this.shop].cost;
         if (!this.canAfford(cost)) return false;
         this.pay(cost);
--- 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 };
--- a/game/js/render.js	Mon Oct 19 16:56:38 2026 +0200
+++ b/game/js/render.js	Mon Oct 19 17:56:56 2026 +0200
@@ -72,8 +72,9 @@
         }
     }
 
-    function drawTower(ctx, t, alpha) {
-        var scale = cfg.scale.sprite;
+    // k scales the drawing, for the pulsing preview
+    function drawTower(ctx, t, alpha, k) {
+        var scale = cfg.scale.sprite * (k || 1);
         ctx.globalAlpha = alpha;
         if (t.def.projectile_speed === 0) drawSpikes(ctx, t, scale);
         A.draw(ctx, MM.tower.sprite(t), t.x, t.y, scale, t.angle);
@@ -142,7 +143,10 @@
         if (game.preview) {
             var pv = MM.tower.create(game.shop, game.preview.x, game.preview.y, game.preview.sides);
             drawRange(ctx, pv.x, pv.y, MM.tower.range(pv), s);
-            drawTower(ctx, pv, 0.65);
+            // the preview pulses with the chosen shop item: it grows while
+            // the item shows one drawing and shrinks while it shows the other
+            var k = 0.5 - 0.5 * Math.cos(Math.PI * view.now / MM.ui.BUBBLE_FLIP);
+            drawTower(ctx, pv, MM.util.lerp(0.45, 0.8, k), MM.util.lerp(1, 1.08, k));
         }
 
         game.monsters.slice().sort(function (a, b) { return a.y - b.y; })
--- a/game/js/ui.js	Mon Oct 19 16:56:38 2026 +0200
+++ b/game/js/ui.js	Mon Oct 19 17:56:56 2026 +0200
@@ -348,5 +348,5 @@
         }
     }
 
-    return { layout: layout, hit: hit, draw: draw };
+    return { layout: layout, hit: hit, draw: draw, BUBBLE_FLIP: BUBBLE_FLIP };
 })();