diff game/js/ui.js @ 23:2f555a54c332

layout fixes and balancing
author ark
date 2026-10-20 12:04 +0200
parents fe680946164b
children 3f2811aebb3d
line wrap: on
line diff
--- a/game/js/ui.js	Tue Oct 20 03:08:56 2026 +0200
+++ b/game/js/ui.js	Tue Oct 20 12:04:42 2026 +0200
@@ -9,7 +9,7 @@
 
     var cfg = MM.config;
     var A = MM.assets;
-    var TOP = 60;           // top bar height, css pixels
+    var TOP = 60;           // height of one top bar row, css pixels
     var SHOP = 96;          // shop width (landscape) or height (portrait)
     var BUTTON = 52;        // top bar buttons, at least 48 for touch
     var SHOP_W = 84;        // shop items
@@ -25,6 +25,8 @@
     var PAPER = "#f6f1e6";
     var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
     var TOWERS = ["t0", "t1", "t2", "t3"];
+    // widest usual values of the coin, heart and wave counters
+    var USUAL = ["000", "00", "00/00"];
     // each screen drawing, its separate button image and the button's action
     var SCREENS = {
         splash: { image: "splash_play", id: "play" },
@@ -53,19 +55,39 @@
         return Math.min(1, 0.92 * w / mw, 0.9 * h / mh);
     }
 
-    // positions of everything on screen for a window of w x h css pixels
+    // width of the top bar counters, each at its widest usual value, for
+    // icons of the given size
+    function countersWidth(icon) {
+        var w = 0;
+        USUAL.forEach(function (s) { w += icon + 6 + numberWidth(s, DIGIT) + 18; });
+        return w - 18;
+    }
+
+    // width of the top bar buttons, the early start button included
+    function buttonsWidth() {
+        var s = cfg.sprites.next_wave;
+        return 3 * (BUTTON + 8) + BUTTON * s[0] / s[1];
+    }
+
+    // positions of everything on screen for a window of w x h css pixels.
+    // when the counters and the buttons do not fit side by side, the buttons
+    // get a second top bar row under the counters.
     function layout(w, h, map) {
-        var land = w >= h;
-        var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP }
-                        : { x: 0, y: TOP, w: w, h: h - TOP - SHOP };
+        var land = w >= h, icon = 32;
+        var rows = 8 + countersWidth(icon) + 16 + buttonsWidth() + 8 > w ? 2 : 1;
+        var top = rows * TOP;
+        var area = land ? { x: 0, y: top, w: w - SHOP, h: h - top }
+                        : { x: 0, y: top, w: w, h: h - top - SHOP };
         var scale = Math.min(area.w / map.width, area.h / map.height);
         var L = {
             w: w, h: h, land: land, area: area, scale: scale,
+            top: top,                   // top bar height
+            buttonRow: top - TOP,       // top of the button row
             ox: area.x + (area.w - map.width * scale) / 2,
             oy: area.y + (area.h - map.height * scale) / 2,
-            shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP }
+            shop: land ? { x: w - SHOP, y: top, w: SHOP, h: h - top }
                        : { x: 0, y: h - SHOP, w: w, h: SHOP },
-            coinIcon: { x: 24, y: TOP / 2, size: 32 },
+            coinIcon: { x: 8 + icon / 2, y: TOP / 2, size: icon },
             screen: screenScale(w, h)
         };
         return L;
@@ -121,7 +143,7 @@
             return out;
         }
         // top bar, right to left
-        var y = (TOP - BUTTON) / 2, x = L.w - 8;
+        var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8;
         ["pause", "speed", "sound"].forEach(function (id) {
             x -= BUTTON;
             out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
@@ -306,21 +328,21 @@
 
         // top bar: coins, hearts, wave
         ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
-        ctx.fillRect(0, 0, L.w, TOP);
+        ctx.fillRect(0, 0, L.w, L.top);
         ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
         ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
         ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
-        ctx.fillRect(0, TOP - 2, L.w, 2);
+        ctx.fillRect(0, L.top - 2, L.w, 2);
         // each counter takes at least the width of its widest usual value, so
         // the next one does not move while the numbers change
         var ic = L.coinIcon, x = ic.x - ic.size / 2;
-        [["coin_icon", game.coins, "000"], ["heart", game.hearts, "00"],
-         ["monster_icon", game.waves.shown() + "/" + game.waves.list.length, "00/00"]
-        ].forEach(function (c) {
+        [["coin_icon", game.coins], ["heart", game.hearts],
+         ["monster_icon", game.waves.shown() + "/" + game.waves.list.length]
+        ].forEach(function (c, i) {
             var s = String(c[1]);
-            fit(ctx, c[0], x + ic.size / 2, TOP / 2, ic.size, ic.size);
-            number(ctx, s, x + ic.size + 6, TOP / 2, DIGIT, "left");
-            x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(c[2], DIGIT)) + 18;
+            fit(ctx, c[0], x + ic.size / 2, ic.y, ic.size, ic.size);
+            number(ctx, s, x + ic.size + 6, ic.y, DIGIT, "left");
+            x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(USUAL[i], DIGIT)) + 18;
         });
 
         var screen = screenName(game);