wobblyhead hg > minor_monsters
view game/js/ui.js @ 8:2c4fed8baed9
art update, persistent sound setting, tower selling as double action
| author | ark |
|---|---|
| date | 2026-10-19 13:37 +0200 |
| parents | 910b26feca76 |
| children | a77857594150 |
line wrap: on
line source
// screen layout, top bar, tower shop, tower menu, splash and end screens, // drawn from the supplied icons and drawings (art.md). numbers use the // hand-drawn digits. window.MM = window.MM || {}; MM.ui = (function () { "use strict"; var cfg = MM.config; var A = MM.assets; var TOP = 60; // top bar height, 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 var SHOP_H = 72; var POPUP_W = 168; // tower menu bubble; its height follows the drawing var DIGIT = 28; // digit height in the top bar var PRICE = 18; // digit height of prices var DIGIT_GAP = 0.08; // space between digits, a share of their height var BUBBLE_FLIP = 400; // ms between the two drawings of the chosen shop item var PULSE = 1.2; // seconds of one pulse of a screen button var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button var INK = "#3a3530"; var PAPER = "#f6f1e6"; var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif"; var TOWERS = ["t0", "t1", "t2", "t3"]; // each screen drawing, its separate button image and the button's action var SCREENS = { splash: { image: "splash_play", id: "play" }, map_won: { image: "map_won_button", id: "next" }, game_lost: { image: "game_lost_button", id: "retry" }, game_won: { image: "game_won_button", id: "restart" } }; // digit variants from the sprite table: character -> sprite names var DIGITS = {}; Object.keys(cfg.sprites).forEach(function (n) { var m = /^digit_(\d|slash)_\d+$/.exec(n); if (!m) return; var ch = m[1] === "slash" ? "/" : m[1]; (DIGITS[ch] = DIGITS[ch] || []).push(n); }); // all screens share one scale, set by the largest drawing, so they keep // the sizes of the drawings relative to each other function screenScale(w, h) { var mw = 0, mh = 0; Object.keys(SCREENS).forEach(function (n) { mw = Math.max(mw, cfg.sprites[n][0]); mh = Math.max(mh, cfg.sprites[n][1]); }); 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 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 scale = Math.min(area.w / map.width, area.h / map.height); var L = { w: w, h: h, land: land, area: area, scale: scale, 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 } : { x: 0, y: h - SHOP, w: w, h: SHOP }, coinIcon: { x: 24, y: TOP / 2, size: 32 }, screen: screenScale(w, h) }; return L; } // the screen shown over the map, or null during play function screenName(game) { if (game.splash) return "splash"; if (game.result === "lost") return "game_lost"; if (game.result) return game.lastMap() ? "game_won" : "map_won"; return null; } // the drawing of a screen centred in the window, and its button function screenRects(name, L) { var k = L.screen, s = cfg.sprites[name]; var bs = cfg.sprites[SCREENS[name].image], at = cfg.art.screen[name]; var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k }; return { panel: p, button: { x: p.x + at[0] * k, y: p.y + at[1] * k, w: bs[0] * k, h: bs[1] * k } }; } // the tower menu: a bubble above the selected tower, or below it when // there is no room above, kept inside the map area. upgrade and sell each // take half of the bubble; at the top level there is no upgrade and sell // takes all of it. cx is where the button's drawing is centred. function towerMenu(game, L) { var t = game.selected, a = L.area, gap = 8; var s = cfg.sprites.tower_popup, w = POPUP_W, h = POPUP_W * s[1] / s[0]; var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale; var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap; var top = y - half - h; if (top < a.y + gap) top = y + half; var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap); var bubble = { x: left, y: top, w: w, h: h }; if (!MM.tower.upgradeCost(t)) { return [{ id: "sell", x: left, y: top, w: w, h: h, cx: left + w / 2, bubble: bubble }]; } return [ { id: "upgrade", x: left, y: top, w: w / 2, h: h, cx: left + w * 0.34, bubble: bubble }, { id: "sell", x: left + w / 2, y: top, w: w / 2, h: h, cx: left + w * 0.66, bubble: bubble } ]; } // the buttons for the current state, each {id, x, y, w, h} function buttons(game, L) { var out = []; var name = screenName(game); if (name) { var b = screenRects(name, L).button; b.id = SCREENS[name].id; b.screen = name; out.push(b); return out; } // top bar, right to left var y = (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 }); x -= 8; }); if (game.waves.phase === "break") { var s = cfg.sprites.next_wave, w = BUTTON * s[0] / s[1]; out.push({ id: "early", x: x - w, y: y, w: w, h: BUTTON }); } TOWERS.forEach(function (t, i) { var b = L.land ? { x: L.shop.x + (SHOP - SHOP_W) / 2, y: L.shop.y + 10 + i * (SHOP_H + 10) } : { x: L.shop.x + 10 + i * (SHOP_W + 10), y: L.shop.y + (SHOP - SHOP_H) / 2 }; b.id = "shop"; b.tower = t; b.w = SHOP_W; b.h = SHOP_H; out.push(b); }); if (game.selected) out = out.concat(towerMenu(game, L)); return out; } function inside(b, x, y) { return x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h; } function hit(game, L, x, y) { var list = buttons(game, L); for (var i = 0; i < list.length; i++) { if (inside(list[i], x, y)) return list[i]; } return null; } // system font text, for the debug stats only function text(ctx, s, x, y, align, size) { ctx.font = size ? FONT.replace("22px", size + "px") : FONT; ctx.textAlign = align || "left"; ctx.textBaseline = "middle"; ctx.lineWidth = 4; ctx.strokeStyle = PAPER; ctx.strokeText(s, x, y); ctx.fillStyle = INK; ctx.fillText(s, x, y); } // a sprite fitted into a box of w x h centred on x, y, keeping its shape function fit(ctx, name, x, y, w, h) { var s = cfg.sprites[name], k = Math.min(w / s[0], h / s[1]); ctx.drawImage(A.img(name), x - s[0] * k / 2, y - s[1] * k / 2, s[0] * k, s[1] * k); } // the digit sprites for string s. the variant of each digit depends on // the digit and its place counted from the right, so a number keeps its // look while other places change. function glyphs(s) { var out = []; for (var i = 0; i < s.length; i++) { var list = DIGITS[s[i]]; if (!list) continue; var place = s.length - 1 - i; out.push(list[(place * 5 + s.charCodeAt(i)) % list.length]); } return out; } function numberWidth(s, h) { var g = glyphs(s), w = 0; g.forEach(function (n) { w += cfg.sprites[n][0] * h / cfg.sprites[n][1]; }); return w + Math.max(0, g.length - 1) * h * DIGIT_GAP; } // number s in digits of height h, vertically centred on y. align is // "left", "center" or "right" of x. function number(ctx, s, x, y, h, align) { s = String(s); var w = numberWidth(s, h); if (align === "center") x -= w / 2; else if (align === "right") x -= w; glyphs(s).forEach(function (n) { var gw = cfg.sprites[n][0] * h / cfg.sprites[n][1]; ctx.drawImage(A.img(n), x, y - h / 2, gw, h); x += gw + h * DIGIT_GAP; }); } // a price, the coin icon followed by the amount, centred on x, y function price(ctx, amount, x, y) { var s = String(amount), coin = PRICE * 0.8, gap = 3; var left = x - (coin + gap + numberWidth(s, PRICE)) / 2; fit(ctx, "coin_icon", left + coin / 2, y, coin, coin); number(ctx, s, left + coin + gap, y, PRICE, "left"); } function box(ctx, b, fill, line) { ctx.fillStyle = fill; ctx.strokeStyle = line || INK; ctx.lineWidth = 2; ctx.beginPath(); if (ctx.roundRect) ctx.roundRect(b.x, b.y, b.w, b.h, 10); else ctx.rect(b.x, b.y, b.w, b.h); ctx.fill(); ctx.stroke(); } // a screen's button image over the same button in the drawing. it pulses // slowly as a cue to tap; under the mouse it is drawn larger, lifted by // a shadow. function screenButton(ctx, b, view) { var cx = b.x + b.w / 2, cy = b.y + b.h / 2; var hover = view.hover && inside(b, view.hover.x, view.hover.y); var k = hover ? 1.08 : 1 + 0.04 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / PULSE)); var w = b.w * k, h = b.h * k; ctx.save(); if (hover) { ctx.shadowColor = "rgba(0, 0, 0, 0.35)"; ctx.shadowBlur = 12; ctx.shadowOffsetY = 3; } ctx.drawImage(A.img(SCREENS[b.screen].image), cx - w / 2, cy - h / 2, w, h); ctx.restore(); } function drawButton(ctx, game, b, view) { var cx = b.x + b.w / 2, cy = b.y + b.h / 2; if (b.screen) { screenButton(ctx, b, view); } else if (b.id === "pause") { // shows what a tap does: pause while running, play while paused fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h); } else if (b.id === "speed") { // debug speeds have no drawing and show their number var sp = game.speed(), name = "speed_x" + sp; if (cfg.sprites[name]) { fit(ctx, name, cx, cy, b.w, b.h); } else { box(ctx, b, PAPER); number(ctx, sp, cx, cy, DIGIT, "center"); } } else if (b.id === "sound") { fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h); } else if (b.id === "early") { // the countdown fills the empty right part of the drawing ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h); number(ctx, Math.ceil(game.waves.left), b.x + b.w * 0.66, cy, DIGIT, "center"); } else if (b.id === "shop") { var def = cfg.tower[b.tower]; // the chosen item's bubble switches between its two drawings var bubble = game.shop !== b.tower ? "shop_bubble" : Math.floor(view.now / BUBBLE_FLIP) % 2 ? "shop_bubble_dark2" : "shop_bubble_dark"; ctx.drawImage(A.img(bubble), b.x, b.y, b.w, b.h); ctx.globalAlpha = game.canAfford(def.cost) ? 1 : 0.35; fit(ctx, b.tower, cx, b.y + 28, 44, 38); price(ctx, def.cost, cx, b.y + b.h - 15); ctx.globalAlpha = 1; } else if (b.id === "upgrade") { var cost = MM.tower.upgradeCost(game.selected); ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35; fit(ctx, "upgrade_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46, 44); price(ctx, cost, b.cx, b.bubble.y + b.bubble.h * 0.7); ctx.globalAlpha = 1; } else if (b.id === "sell") { // armed by a first tap: a fast pulse with a red glow until the // second tap sells var k = 1; ctx.save(); if (game.sellArmed) { k = 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE)); ctx.shadowColor = "rgba(220, 30, 20, 0.9)"; ctx.shadowBlur = 14; } fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k); ctx.restore(); price(ctx, MM.tower.refund(game.selected, game.pauseNumber()), b.cx, b.bubble.y + b.bubble.h * 0.7); } } function draw(ctx, game, L, view) { var dpr = view.dpr; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); // top bar: coins, hearts, wave ctx.fillStyle = "rgba(246, 241, 230, 0.92)"; ctx.fillRect(0, 0, L.w, 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); // 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) { 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; }); var screen = screenName(game); var list = buttons(game, L); if (game.paused && !screen) { ctx.fillStyle = "rgba(255, 255, 255, 0.18)"; ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale); } if (screen) { ctx.fillStyle = game.splash ? "rgba(246, 241, 230, 0.55)" : "rgba(40, 35, 30, 0.45)"; ctx.fillRect(0, 0, L.w, L.h); var p = screenRects(screen, L).panel; ctx.drawImage(A.img(screen), p.x, p.y, p.w, p.h); } else if (game.selected) { var bub = list[list.length - 1].bubble; ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h); } list.forEach(function (b) { drawButton(ctx, game, b, view); }); if (cfg.debug.show_stats && view.stats) { var st = view.stats; text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " + game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed, 8, L.h - 14, "left", 14); } } return { layout: layout, hit: hit, draw: draw }; })();
