diff game/js/game.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/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);