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