comparison game/js/ui.js @ 17:fe680946164b

reorganised repo
author ark
date 2026-10-20 00:12 +0200
parents
children 2f555a54c332
comparison
equal deleted inserted replaced
16:30bd10284fd8 17:fe680946164b
1 // screen layout, top bar, tower shop, tower menu, splash and end screens,
2 // drawn from the supplied icons and drawings (art.md). numbers use the
3 // hand-drawn digits.
4
5 window.MM = window.MM || {};
6
7 MM.ui = (function () {
8 "use strict";
9
10 var cfg = MM.config;
11 var A = MM.assets;
12 var TOP = 60; // top bar height, css pixels
13 var SHOP = 96; // shop width (landscape) or height (portrait)
14 var BUTTON = 52; // top bar buttons, at least 48 for touch
15 var SHOP_W = 84; // shop items
16 var SHOP_H = 72;
17 var POPUP_W = 168; // tower menu bubble; its height follows the drawing
18 var DIGIT = 28; // digit height in the top bar
19 var PRICE = 18; // digit height of prices
20 var DIGIT_GAP = 0.08; // space between digits, a share of their height
21 var BUBBLE_FLIP = 400; // ms between the two drawings of the chosen shop item
22 var PULSE = 1.2; // seconds of one pulse of a screen button
23 var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button
24 var INK = "#3a3530";
25 var PAPER = "#f6f1e6";
26 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
27 var TOWERS = ["t0", "t1", "t2", "t3"];
28 // each screen drawing, its separate button image and the button's action
29 var SCREENS = {
30 splash: { image: "splash_play", id: "play" },
31 map_won: { image: "map_won_button", id: "next" },
32 game_lost: { image: "game_lost_button", id: "retry" },
33 game_won: { image: "game_won_button", id: "restart" }
34 };
35
36 // digit variants from the sprite table: character -> sprite names
37 var DIGITS = {};
38 Object.keys(cfg.sprites).forEach(function (n) {
39 var m = /^digit_(\d|slash)_\d+$/.exec(n);
40 if (!m) return;
41 var ch = m[1] === "slash" ? "/" : m[1];
42 (DIGITS[ch] = DIGITS[ch] || []).push(n);
43 });
44
45 // all screens share one scale, set by the largest drawing, so they keep
46 // the sizes of the drawings relative to each other
47 function screenScale(w, h) {
48 var mw = 0, mh = 0;
49 Object.keys(SCREENS).forEach(function (n) {
50 mw = Math.max(mw, cfg.sprites[n][0]);
51 mh = Math.max(mh, cfg.sprites[n][1]);
52 });
53 return Math.min(1, 0.92 * w / mw, 0.9 * h / mh);
54 }
55
56 // positions of everything on screen for a window of w x h css pixels
57 function layout(w, h, map) {
58 var land = w >= h;
59 var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP }
60 : { x: 0, y: TOP, w: w, h: h - TOP - SHOP };
61 var scale = Math.min(area.w / map.width, area.h / map.height);
62 var L = {
63 w: w, h: h, land: land, area: area, scale: scale,
64 ox: area.x + (area.w - map.width * scale) / 2,
65 oy: area.y + (area.h - map.height * scale) / 2,
66 shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP }
67 : { x: 0, y: h - SHOP, w: w, h: SHOP },
68 coinIcon: { x: 24, y: TOP / 2, size: 32 },
69 screen: screenScale(w, h)
70 };
71 return L;
72 }
73
74 // the screen shown over the map, or null during play
75 function screenName(game) {
76 if (game.splash) return "splash";
77 if (game.result === "lost") return "game_lost";
78 if (game.result) return game.lastMap() ? "game_won" : "map_won";
79 return null;
80 }
81
82 // the drawing of a screen centred in the window, and its button
83 function screenRects(name, L) {
84 var k = L.screen, s = cfg.sprites[name];
85 var bs = cfg.sprites[SCREENS[name].image], at = cfg.art.screen[name];
86 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k };
87 return { panel: p, button: { x: p.x + at[0] * k, y: p.y + at[1] * k, w: bs[0] * k, h: bs[1] * k } };
88 }
89
90 // the tower menu: a bubble above the selected tower, or below it when
91 // there is no room above, kept inside the map area. upgrade and sell each
92 // take half of the bubble; at the top level there is no upgrade and sell
93 // takes all of it. cx is where the button's drawing is centred.
94 function towerMenu(game, L) {
95 var t = game.selected, a = L.area, gap = 8;
96 var s = cfg.sprites.tower_popup, w = POPUP_W, h = POPUP_W * s[1] / s[0];
97 var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale;
98 var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap;
99 var top = y - half - h;
100 if (top < a.y + gap) top = y + half;
101 var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap);
102 var bubble = { x: left, y: top, w: w, h: h };
103 if (!MM.tower.upgradeCost(t)) {
104 return [{ id: "sell", x: left, y: top, w: w, h: h, cx: left + w / 2, bubble: bubble }];
105 }
106 return [
107 { id: "upgrade", x: left, y: top, w: w / 2, h: h, cx: left + w * 0.34, bubble: bubble },
108 { id: "sell", x: left + w / 2, y: top, w: w / 2, h: h, cx: left + w * 0.66, bubble: bubble }
109 ];
110 }
111
112 // the buttons for the current state, each {id, x, y, w, h}
113 function buttons(game, L) {
114 var out = [];
115 var name = screenName(game);
116 if (name) {
117 var b = screenRects(name, L).button;
118 b.id = SCREENS[name].id;
119 b.screen = name;
120 out.push(b);
121 return out;
122 }
123 // top bar, right to left
124 var y = (TOP - BUTTON) / 2, x = L.w - 8;
125 ["pause", "speed", "sound"].forEach(function (id) {
126 x -= BUTTON;
127 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
128 x -= 8;
129 });
130 if (game.waves.phase === "break") {
131 var s = cfg.sprites.next_wave, w = BUTTON * s[0] / s[1];
132 out.push({ id: "early", x: x - w, y: y, w: w, h: BUTTON });
133 }
134 TOWERS.forEach(function (t, i) {
135 var b = L.land ? { x: L.shop.x + (SHOP - SHOP_W) / 2, y: L.shop.y + 10 + i * (SHOP_H + 10) }
136 : { x: L.shop.x + 10 + i * (SHOP_W + 10), y: L.shop.y + (SHOP - SHOP_H) / 2 };
137 b.id = "shop";
138 b.tower = t;
139 b.w = SHOP_W;
140 b.h = SHOP_H;
141 out.push(b);
142 });
143 if (game.selected) out = out.concat(towerMenu(game, L));
144 return out;
145 }
146
147 function inside(b, x, y) {
148 return x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h;
149 }
150
151 function hit(game, L, x, y) {
152 var list = buttons(game, L);
153 for (var i = 0; i < list.length; i++) {
154 if (inside(list[i], x, y)) return list[i];
155 }
156 return null;
157 }
158
159 // system font text, for the debug stats only
160 function text(ctx, s, x, y, align, size) {
161 ctx.font = size ? FONT.replace("22px", size + "px") : FONT;
162 ctx.textAlign = align || "left";
163 ctx.textBaseline = "middle";
164 ctx.lineWidth = 4;
165 ctx.strokeStyle = PAPER;
166 ctx.strokeText(s, x, y);
167 ctx.fillStyle = INK;
168 ctx.fillText(s, x, y);
169 }
170
171 // a sprite fitted into a box of w x h centred on x, y, keeping its shape
172 function fit(ctx, name, x, y, w, h) {
173 var s = cfg.sprites[name], k = Math.min(w / s[0], h / s[1]);
174 ctx.drawImage(A.img(name), x - s[0] * k / 2, y - s[1] * k / 2, s[0] * k, s[1] * k);
175 }
176
177 // the digit sprites for string s. the variant of each digit depends on
178 // the digit and its place counted from the right, so a number keeps its
179 // look while other places change.
180 function glyphs(s) {
181 var out = [];
182 for (var i = 0; i < s.length; i++) {
183 var list = DIGITS[s[i]];
184 if (!list) continue;
185 var place = s.length - 1 - i;
186 out.push(list[(place * 5 + s.charCodeAt(i)) % list.length]);
187 }
188 return out;
189 }
190
191 function numberWidth(s, h) {
192 var g = glyphs(s), w = 0;
193 g.forEach(function (n) { w += cfg.sprites[n][0] * h / cfg.sprites[n][1]; });
194 return w + Math.max(0, g.length - 1) * h * DIGIT_GAP;
195 }
196
197 // number s in digits of height h, vertically centred on y. align is
198 // "left", "center" or "right" of x.
199 function number(ctx, s, x, y, h, align) {
200 s = String(s);
201 var w = numberWidth(s, h);
202 if (align === "center") x -= w / 2;
203 else if (align === "right") x -= w;
204 glyphs(s).forEach(function (n) {
205 var gw = cfg.sprites[n][0] * h / cfg.sprites[n][1];
206 ctx.drawImage(A.img(n), x, y - h / 2, gw, h);
207 x += gw + h * DIGIT_GAP;
208 });
209 }
210
211 // a price, the coin icon followed by the amount, centred on x, y
212 function price(ctx, amount, x, y) {
213 var s = String(amount), coin = PRICE * 0.8, gap = 3;
214 var left = x - (coin + gap + numberWidth(s, PRICE)) / 2;
215 fit(ctx, "coin_icon", left + coin / 2, y, coin, coin);
216 number(ctx, s, left + coin + gap, y, PRICE, "left");
217 }
218
219 function box(ctx, b, fill, line) {
220 ctx.fillStyle = fill;
221 ctx.strokeStyle = line || INK;
222 ctx.lineWidth = 2;
223 ctx.beginPath();
224 if (ctx.roundRect) ctx.roundRect(b.x, b.y, b.w, b.h, 10);
225 else ctx.rect(b.x, b.y, b.w, b.h);
226 ctx.fill();
227 ctx.stroke();
228 }
229
230 // a screen's button image over the same button in the drawing. it pulses
231 // slowly as a cue to tap; under the mouse it is drawn larger, lifted by
232 // a shadow.
233 function screenButton(ctx, b, view) {
234 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
235 var hover = view.hover && inside(b, view.hover.x, view.hover.y);
236 var k = hover ? 1.08 : 1 + 0.04 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / PULSE));
237 var w = b.w * k, h = b.h * k;
238 ctx.save();
239 if (hover) {
240 ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
241 ctx.shadowBlur = 12;
242 ctx.shadowOffsetY = 3;
243 }
244 ctx.drawImage(A.img(SCREENS[b.screen].image), cx - w / 2, cy - h / 2, w, h);
245 ctx.restore();
246 }
247
248 function drawButton(ctx, game, b, view) {
249 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
250 if (b.screen) {
251 screenButton(ctx, b, view);
252 } else if (b.id === "pause") {
253 // shows what a tap does: pause while running, play while paused
254 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
255 } else if (b.id === "speed") {
256 // debug speeds have no drawing and show their number
257 var sp = game.speed(), name = "speed_x" + sp;
258 if (cfg.sprites[name]) {
259 fit(ctx, name, cx, cy, b.w, b.h);
260 } else {
261 box(ctx, b, PAPER);
262 number(ctx, sp, cx, cy, DIGIT, "center");
263 }
264 } else if (b.id === "sound") {
265 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h);
266 } else if (b.id === "early") {
267 // the countdown fills the empty right part of the drawing
268 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h);
269 number(ctx, Math.ceil(game.waves.left), b.x + b.w * 0.66, cy, DIGIT, "center");
270 } else if (b.id === "shop") {
271 var def = cfg.tower[b.tower];
272 // the chosen item's bubble switches between its two drawings
273 var bubble = game.shop !== b.tower ? "shop_bubble"
274 : Math.floor(view.now / BUBBLE_FLIP) % 2 ? "shop_bubble_dark2" : "shop_bubble_dark";
275 ctx.drawImage(A.img(bubble), b.x, b.y, b.w, b.h);
276 ctx.globalAlpha = game.canAfford(def.cost[0]) && !game.paused ? 1 : 0.35;
277 fit(ctx, b.tower, cx, b.y + 28, 44, 38);
278 price(ctx, def.cost[0], cx, b.y + b.h - 15);
279 ctx.globalAlpha = 1;
280 } else if (b.id === "upgrade") {
281 var cost = MM.tower.upgradeCost(game.selected);
282 ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35;
283 fit(ctx, "upgrade_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46, 44);
284 price(ctx, cost, b.cx, b.bubble.y + b.bubble.h * 0.7);
285 ctx.globalAlpha = 1;
286 } else if (b.id === "sell") {
287 // armed by a first tap: a fast pulse with a red glow until the
288 // second tap sells
289 var k = 1;
290 ctx.save();
291 if (game.sellArmed) {
292 k = 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
293 ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
294 ctx.shadowBlur = 14;
295 }
296 fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k);
297 ctx.restore();
298 price(ctx, MM.tower.refund(game.selected), b.cx,
299 b.bubble.y + b.bubble.h * 0.7);
300 }
301 }
302
303 function draw(ctx, game, L, view) {
304 var dpr = view.dpr;
305 ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
306
307 // top bar: coins, hearts, wave
308 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
309 ctx.fillRect(0, 0, L.w, TOP);
310 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
311 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
312 ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
313 ctx.fillRect(0, TOP - 2, L.w, 2);
314 // each counter takes at least the width of its widest usual value, so
315 // the next one does not move while the numbers change
316 var ic = L.coinIcon, x = ic.x - ic.size / 2;
317 [["coin_icon", game.coins, "000"], ["heart", game.hearts, "00"],
318 ["monster_icon", game.waves.shown() + "/" + game.waves.list.length, "00/00"]
319 ].forEach(function (c) {
320 var s = String(c[1]);
321 fit(ctx, c[0], x + ic.size / 2, TOP / 2, ic.size, ic.size);
322 number(ctx, s, x + ic.size + 6, TOP / 2, DIGIT, "left");
323 x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(c[2], DIGIT)) + 18;
324 });
325
326 var screen = screenName(game);
327 var list = buttons(game, L);
328 if (game.paused && !screen) {
329 ctx.fillStyle = "rgba(255, 255, 255, 0.18)";
330 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale);
331 }
332 if (screen) {
333 ctx.fillStyle = game.splash ? "rgba(246, 241, 230, 0.55)" : "rgba(40, 35, 30, 0.45)";
334 ctx.fillRect(0, 0, L.w, L.h);
335 var p = screenRects(screen, L).panel;
336 ctx.drawImage(A.img(screen), p.x, p.y, p.w, p.h);
337 } else if (game.selected) {
338 var bub = list[list.length - 1].bubble;
339 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h);
340 }
341 list.forEach(function (b) { drawButton(ctx, game, b, view); });
342
343 if (cfg.debug.show_stats && view.stats) {
344 var st = view.stats;
345 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " +
346 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed,
347 8, L.h - 14, "left", 14);
348 }
349 }
350
351 return { layout: layout, hit: hit, draw: draw, BUBBLE_FLIP: BUBBLE_FLIP };
352 })();