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

layout fixes and balancing
author ark
date 2026-10-20 12:04 +0200
parents fe680946164b
children 3f2811aebb3d
comparison
equal deleted inserted replaced
22:ad1b478fc4dc 23:2f555a54c332
7 MM.ui = (function () { 7 MM.ui = (function () {
8 "use strict"; 8 "use strict";
9 9
10 var cfg = MM.config; 10 var cfg = MM.config;
11 var A = MM.assets; 11 var A = MM.assets;
12 var TOP = 60; // top bar height, css pixels 12 var TOP = 60; // height of one top bar row, css pixels
13 var SHOP = 96; // shop width (landscape) or height (portrait) 13 var SHOP = 96; // shop width (landscape) or height (portrait)
14 var BUTTON = 52; // top bar buttons, at least 48 for touch 14 var BUTTON = 52; // top bar buttons, at least 48 for touch
15 var SHOP_W = 84; // shop items 15 var SHOP_W = 84; // shop items
16 var SHOP_H = 72; 16 var SHOP_H = 72;
17 var POPUP_W = 168; // tower menu bubble; its height follows the drawing 17 var POPUP_W = 168; // tower menu bubble; its height follows the drawing
23 var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button 23 var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button
24 var INK = "#3a3530"; 24 var INK = "#3a3530";
25 var PAPER = "#f6f1e6"; 25 var PAPER = "#f6f1e6";
26 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif"; 26 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
27 var TOWERS = ["t0", "t1", "t2", "t3"]; 27 var TOWERS = ["t0", "t1", "t2", "t3"];
28 // widest usual values of the coin, heart and wave counters
29 var USUAL = ["000", "00", "00/00"];
28 // each screen drawing, its separate button image and the button's action 30 // each screen drawing, its separate button image and the button's action
29 var SCREENS = { 31 var SCREENS = {
30 splash: { image: "splash_play", id: "play" }, 32 splash: { image: "splash_play", id: "play" },
31 map_won: { image: "map_won_button", id: "next" }, 33 map_won: { image: "map_won_button", id: "next" },
32 game_lost: { image: "game_lost_button", id: "retry" }, 34 game_lost: { image: "game_lost_button", id: "retry" },
51 mh = Math.max(mh, cfg.sprites[n][1]); 53 mh = Math.max(mh, cfg.sprites[n][1]);
52 }); 54 });
53 return Math.min(1, 0.92 * w / mw, 0.9 * h / mh); 55 return Math.min(1, 0.92 * w / mw, 0.9 * h / mh);
54 } 56 }
55 57
56 // positions of everything on screen for a window of w x h css pixels 58 // width of the top bar counters, each at its widest usual value, for
59 // icons of the given size
60 function countersWidth(icon) {
61 var w = 0;
62 USUAL.forEach(function (s) { w += icon + 6 + numberWidth(s, DIGIT) + 18; });
63 return w - 18;
64 }
65
66 // width of the top bar buttons, the early start button included
67 function buttonsWidth() {
68 var s = cfg.sprites.next_wave;
69 return 3 * (BUTTON + 8) + BUTTON * s[0] / s[1];
70 }
71
72 // positions of everything on screen for a window of w x h css pixels.
73 // when the counters and the buttons do not fit side by side, the buttons
74 // get a second top bar row under the counters.
57 function layout(w, h, map) { 75 function layout(w, h, map) {
58 var land = w >= h; 76 var land = w >= h, icon = 32;
59 var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP } 77 var rows = 8 + countersWidth(icon) + 16 + buttonsWidth() + 8 > w ? 2 : 1;
60 : { x: 0, y: TOP, w: w, h: h - TOP - SHOP }; 78 var top = rows * TOP;
79 var area = land ? { x: 0, y: top, w: w - SHOP, h: h - top }
80 : { x: 0, y: top, w: w, h: h - top - SHOP };
61 var scale = Math.min(area.w / map.width, area.h / map.height); 81 var scale = Math.min(area.w / map.width, area.h / map.height);
62 var L = { 82 var L = {
63 w: w, h: h, land: land, area: area, scale: scale, 83 w: w, h: h, land: land, area: area, scale: scale,
84 top: top, // top bar height
85 buttonRow: top - TOP, // top of the button row
64 ox: area.x + (area.w - map.width * scale) / 2, 86 ox: area.x + (area.w - map.width * scale) / 2,
65 oy: area.y + (area.h - map.height * scale) / 2, 87 oy: area.y + (area.h - map.height * scale) / 2,
66 shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP } 88 shop: land ? { x: w - SHOP, y: top, w: SHOP, h: h - top }
67 : { x: 0, y: h - SHOP, w: w, h: SHOP }, 89 : { x: 0, y: h - SHOP, w: w, h: SHOP },
68 coinIcon: { x: 24, y: TOP / 2, size: 32 }, 90 coinIcon: { x: 8 + icon / 2, y: TOP / 2, size: icon },
69 screen: screenScale(w, h) 91 screen: screenScale(w, h)
70 }; 92 };
71 return L; 93 return L;
72 } 94 }
73 95
119 b.screen = name; 141 b.screen = name;
120 out.push(b); 142 out.push(b);
121 return out; 143 return out;
122 } 144 }
123 // top bar, right to left 145 // top bar, right to left
124 var y = (TOP - BUTTON) / 2, x = L.w - 8; 146 var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8;
125 ["pause", "speed", "sound"].forEach(function (id) { 147 ["pause", "speed", "sound"].forEach(function (id) {
126 x -= BUTTON; 148 x -= BUTTON;
127 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON }); 149 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
128 x -= 8; 150 x -= 8;
129 }); 151 });
304 var dpr = view.dpr; 326 var dpr = view.dpr;
305 ctx.setTransform(dpr, 0, 0, dpr, 0, 0); 327 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
306 328
307 // top bar: coins, hearts, wave 329 // top bar: coins, hearts, wave
308 ctx.fillStyle = "rgba(246, 241, 230, 0.92)"; 330 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
309 ctx.fillRect(0, 0, L.w, TOP); 331 ctx.fillRect(0, 0, L.w, L.top);
310 ctx.fillStyle = "rgba(246, 241, 230, 0.92)"; 332 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
311 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h); 333 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
312 ctx.fillStyle = "rgba(58, 53, 48, 0.25)"; 334 ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
313 ctx.fillRect(0, TOP - 2, L.w, 2); 335 ctx.fillRect(0, L.top - 2, L.w, 2);
314 // each counter takes at least the width of its widest usual value, so 336 // each counter takes at least the width of its widest usual value, so
315 // the next one does not move while the numbers change 337 // the next one does not move while the numbers change
316 var ic = L.coinIcon, x = ic.x - ic.size / 2; 338 var ic = L.coinIcon, x = ic.x - ic.size / 2;
317 [["coin_icon", game.coins, "000"], ["heart", game.hearts, "00"], 339 [["coin_icon", game.coins], ["heart", game.hearts],
318 ["monster_icon", game.waves.shown() + "/" + game.waves.list.length, "00/00"] 340 ["monster_icon", game.waves.shown() + "/" + game.waves.list.length]
319 ].forEach(function (c) { 341 ].forEach(function (c, i) {
320 var s = String(c[1]); 342 var s = String(c[1]);
321 fit(ctx, c[0], x + ic.size / 2, TOP / 2, ic.size, ic.size); 343 fit(ctx, c[0], x + ic.size / 2, ic.y, ic.size, ic.size);
322 number(ctx, s, x + ic.size + 6, TOP / 2, DIGIT, "left"); 344 number(ctx, s, x + ic.size + 6, ic.y, DIGIT, "left");
323 x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(c[2], DIGIT)) + 18; 345 x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(USUAL[i], DIGIT)) + 18;
324 }); 346 });
325 347
326 var screen = screenName(game); 348 var screen = screenName(game);
327 var list = buttons(game, L); 349 var list = buttons(game, L);
328 if (game.paused && !screen) { 350 if (game.paused && !screen) {