wobblyhead hg > minor_monsters
comparison game/js/ui.js @ 6:910b26feca76
full game flow
| author | ark |
|---|---|
| date | 2026-10-19 01:05 +0200 |
| parents | 5492efd60e2c |
| children | 2c4fed8baed9 |
comparison
equal
deleted
inserted
replaced
| 5:67bf3c505145 | 6:910b26feca76 |
|---|---|
| 1 // screen layout, top bar, tower shop and end screens. icons are placeholder | 1 // screen layout, top bar, tower shop, tower menu, splash and end screens. icons are placeholder |
| 2 // shapes until the drawings arrive (art.md); numbers use a system font. | 2 // shapes until the drawings arrive (art.md); numbers use a system font. |
| 3 | 3 |
| 4 window.MM = window.MM || {}; | 4 window.MM = window.MM || {}; |
| 5 | 5 |
| 6 MM.ui = (function () { | 6 MM.ui = (function () { |
| 10 var A = MM.assets; | 10 var A = MM.assets; |
| 11 var TOP = 60; // top bar height, css pixels | 11 var TOP = 60; // top bar height, css pixels |
| 12 var SHOP = 96; // shop width (landscape) or height (portrait) | 12 var SHOP = 96; // shop width (landscape) or height (portrait) |
| 13 var BUTTON = 52; // top bar buttons, at least 48 for touch | 13 var BUTTON = 52; // top bar buttons, at least 48 for touch |
| 14 var SHOP_BUTTON = 80; | 14 var SHOP_BUTTON = 80; |
| 15 var MENU_BUTTON = 64; // upgrade and sell, next to the selected tower | |
| 15 var INK = "#3a3530"; | 16 var INK = "#3a3530"; |
| 16 var PAPER = "#f6f1e6"; | 17 var PAPER = "#f6f1e6"; |
| 17 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif"; | 18 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif"; |
| 18 var TOWERS = ["t0", "t1", "t2", "t3"]; | 19 var TOWERS = ["t0", "t1", "t2", "t3"]; |
| 19 | 20 |
| 32 coinIcon: { x: 26, y: TOP / 2, size: 28 } | 33 coinIcon: { x: 26, y: TOP / 2, size: 28 } |
| 33 }; | 34 }; |
| 34 return L; | 35 return L; |
| 35 } | 36 } |
| 36 | 37 |
| 38 // upgrade and sell buttons above the selected tower, or below it when | |
| 39 // there is no room above, kept inside the map area. no upgrade button | |
| 40 // at the top level. | |
| 41 function towerMenu(game, L) { | |
| 42 var t = game.selected, a = L.area, gap = 8, out = []; | |
| 43 var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale; | |
| 44 var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap; | |
| 45 var w = 2 * MENU_BUTTON + gap; | |
| 46 var top = y - half - MENU_BUTTON; | |
| 47 if (top < a.y + gap) top = y + half; | |
| 48 var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap); | |
| 49 if (MM.tower.upgradeCost(t)) { | |
| 50 out.push({ id: "upgrade", x: left, y: top, w: MENU_BUTTON, h: MENU_BUTTON }); | |
| 51 } | |
| 52 out.push({ id: "sell", x: left + MENU_BUTTON + gap, y: top, w: MENU_BUTTON, h: MENU_BUTTON }); | |
| 53 return out; | |
| 54 } | |
| 55 | |
| 37 // the buttons for the current state, each {id, x, y, w, h} | 56 // the buttons for the current state, each {id, x, y, w, h} |
| 38 function buttons(game, L) { | 57 function buttons(game, L) { |
| 39 var out = []; | 58 var out = []; |
| 40 var y = (TOP - BUTTON) / 2; | 59 var y = (TOP - BUTTON) / 2; |
| 60 if (game.splash) { | |
| 61 out.push({ id: "play", x: L.w / 2 - 56, y: L.h / 2 + 10, w: 112, h: 112 }); | |
| 62 return out; | |
| 63 } | |
| 64 // map lost: back to the first map. map won: on to the next map, or | |
| 65 // after the last map back to the splash screen | |
| 41 if (game.result) { | 66 if (game.result) { |
| 42 out.push({ id: "restart", x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 }); | 67 var id = game.result === "lost" ? "retry" : game.lastMap() ? "restart" : "next"; |
| 68 out.push({ id: id, x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 }); | |
| 43 return out; | 69 return out; |
| 44 } | 70 } |
| 45 out.push({ id: "pause", x: L.w - BUTTON - 8, y: y, w: BUTTON, h: BUTTON }); | 71 out.push({ id: "pause", x: L.w - BUTTON - 8, y: y, w: BUTTON, h: BUTTON }); |
| 46 out.push({ id: "speed", x: L.w - 2 * BUTTON - 16, y: y, w: BUTTON, h: BUTTON }); | 72 out.push({ id: "speed", x: L.w - 2 * BUTTON - 16, y: y, w: BUTTON, h: BUTTON }); |
| 47 if (game.waves.phase === "break") { | 73 if (game.waves.phase === "break") { |
| 54 b.id = "shop"; | 80 b.id = "shop"; |
| 55 b.tower = t; | 81 b.tower = t; |
| 56 b.w = b.h = SHOP_BUTTON; | 82 b.w = b.h = SHOP_BUTTON; |
| 57 out.push(b); | 83 out.push(b); |
| 58 }); | 84 }); |
| 85 if (game.selected) out = out.concat(towerMenu(game, L)); | |
| 59 return out; | 86 return out; |
| 60 } | 87 } |
| 61 | 88 |
| 62 function hit(game, L, x, y) { | 89 function hit(game, L, x, y) { |
| 63 var list = buttons(game, L); | 90 var list = buttons(game, L); |
| 134 ctx.moveTo(tx + 12, ty - 4); | 161 ctx.moveTo(tx + 12, ty - 4); |
| 135 ctx.lineTo(tx - 8, ty - 12); | 162 ctx.lineTo(tx - 8, ty - 12); |
| 136 ctx.lineTo(tx - 2, ty + 10); | 163 ctx.lineTo(tx - 2, ty + 10); |
| 137 ctx.closePath(); | 164 ctx.closePath(); |
| 138 ctx.fill(); | 165 ctx.fill(); |
| 166 } | |
| 167 | |
| 168 function arrowRight(ctx, x, y, s) { | |
| 169 ctx.save(); | |
| 170 ctx.translate(x, y); | |
| 171 ctx.rotate(Math.PI / 2); | |
| 172 arrowUp(ctx, 0, 0, s); | |
| 173 ctx.restore(); | |
| 174 } | |
| 175 | |
| 176 function arrowUp(ctx, x, y, s) { | |
| 177 ctx.beginPath(); | |
| 178 ctx.moveTo(x, y - s * 0.5); | |
| 179 ctx.lineTo(x + s * 0.45, y); | |
| 180 ctx.lineTo(x + s * 0.17, y); | |
| 181 ctx.lineTo(x + s * 0.17, y + s * 0.5); | |
| 182 ctx.lineTo(x - s * 0.17, y + s * 0.5); | |
| 183 ctx.lineTo(x - s * 0.17, y); | |
| 184 ctx.lineTo(x - s * 0.45, y); | |
| 185 ctx.closePath(); | |
| 186 ctx.fill(); | |
| 187 } | |
| 188 | |
| 189 function cross(ctx, x, y, s) { | |
| 190 ctx.lineWidth = 6; | |
| 191 ctx.lineCap = "round"; | |
| 192 ctx.beginPath(); | |
| 193 ctx.moveTo(x - s / 2, y - s / 2); | |
| 194 ctx.lineTo(x + s / 2, y + s / 2); | |
| 195 ctx.moveTo(x + s / 2, y - s / 2); | |
| 196 ctx.lineTo(x - s / 2, y + s / 2); | |
| 197 ctx.stroke(); | |
| 198 ctx.lineCap = "butt"; | |
| 199 } | |
| 200 | |
| 201 // a coin and an amount along the bottom of button b | |
| 202 function price(ctx, b, s) { | |
| 203 icon(ctx, "c0", b.x + 16, b.y + b.h - 13, 16); | |
| 204 text(ctx, s, b.x + 27, b.y + b.h - 12, "left", 18); | |
| 139 } | 205 } |
| 140 | 206 |
| 141 function box(ctx, b, fill, line) { | 207 function box(ctx, b, fill, line) { |
| 142 ctx.fillStyle = fill; | 208 ctx.fillStyle = fill; |
| 143 ctx.strokeStyle = line || INK; | 209 ctx.strokeStyle = line || INK; |
| 186 ctx.globalAlpha = ok ? 1 : 0.35; | 252 ctx.globalAlpha = ok ? 1 : 0.35; |
| 187 icon(ctx, b.tower, cx, b.y + 30, 46); | 253 icon(ctx, b.tower, cx, b.y + 30, 46); |
| 188 icon(ctx, "c0", b.x + 22, b.y + b.h - 14, 16); | 254 icon(ctx, "c0", b.x + 22, b.y + b.h - 14, 16); |
| 189 text(ctx, def.cost + "", b.x + 34, b.y + b.h - 13, "left", 18); | 255 text(ctx, def.cost + "", b.x + 34, b.y + b.h - 13, "left", 18); |
| 190 ctx.globalAlpha = 1; | 256 ctx.globalAlpha = 1; |
| 191 } else if (b.id === "restart") { | 257 } else if (b.id === "upgrade") { |
| 258 var cost = MM.tower.upgradeCost(game.selected); | |
| 259 box(ctx, b, PAPER); | |
| 260 ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35; | |
| 261 ctx.fillStyle = INK; | |
| 262 arrowUp(ctx, cx, b.y + 22, 28); | |
| 263 price(ctx, b, cost + ""); | |
| 264 ctx.globalAlpha = 1; | |
| 265 } else if (b.id === "sell") { | |
| 266 box(ctx, b, PAPER); | |
| 267 ctx.strokeStyle = "#c03030"; | |
| 268 cross(ctx, cx, b.y + 22, 20); | |
| 269 price(ctx, b, "+" + MM.tower.refund(game.selected, game.pauseNumber())); | |
| 270 } else if (b.id === "play") { | |
| 271 box(ctx, b, PAPER); | |
| 272 ctx.fillStyle = INK; | |
| 273 triangle(ctx, cx + 4, cy, 60); | |
| 274 } else if (b.id === "next") { | |
| 275 box(ctx, b, PAPER); | |
| 276 ctx.fillStyle = INK; | |
| 277 arrowRight(ctx, cx, cy, 52); | |
| 278 } else if (b.id === "retry" || b.id === "restart") { | |
| 192 box(ctx, b, PAPER); | 279 box(ctx, b, PAPER); |
| 193 restartIcon(ctx, cx, cy, 26); | 280 restartIcon(ctx, cx, cy, 26); |
| 194 } | 281 } |
| 195 } | 282 } |
| 196 | 283 |
| 210 heart(ctx, 126, TOP / 2 + 2, 30, "#e03a3a"); | 297 heart(ctx, 126, TOP / 2 + 2, 30, "#e03a3a"); |
| 211 text(ctx, game.hearts + "", 146, TOP / 2); | 298 text(ctx, game.hearts + "", 146, TOP / 2); |
| 212 icon(ctx, "m0", 206, TOP / 2, 30); | 299 icon(ctx, "m0", 206, TOP / 2, 30); |
| 213 text(ctx, game.waves.shown() + "/" + game.waves.list.length, 226, TOP / 2); | 300 text(ctx, game.waves.shown() + "/" + game.waves.list.length, 226, TOP / 2); |
| 214 | 301 |
| 215 if (!game.result) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); | 302 var screen = game.splash || game.result; |
| 216 | 303 if (!screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); |
| 217 if (game.paused && !game.result) { | 304 |
| 305 if (game.paused && !screen) { | |
| 218 ctx.fillStyle = "rgba(255, 255, 255, 0.18)"; | 306 ctx.fillStyle = "rgba(255, 255, 255, 0.18)"; |
| 219 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale); | 307 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale); |
| 220 } | 308 } |
| 221 if (game.result) { | 309 if (game.splash) { |
| 310 // title and byline, placeholders until the drawings arrive | |
| 311 ctx.fillStyle = "rgba(246, 241, 230, 0.55)"; | |
| 312 ctx.fillRect(0, 0, L.w, L.h); | |
| 313 text(ctx, "minor monsters", L.w / 2, L.h / 2 - 90, "center", 60); | |
| 314 text(ctx, "By M.A. and J.A. Juszczyk", L.w / 2, L.h / 2 - 38, "center", 24); | |
| 315 } else if (game.result) { | |
| 222 ctx.fillStyle = "rgba(40, 35, 30, 0.45)"; | 316 ctx.fillStyle = "rgba(40, 35, 30, 0.45)"; |
| 223 ctx.fillRect(0, 0, L.w, L.h); | 317 ctx.fillRect(0, 0, L.w, L.h); |
| 224 if (game.result === "won") { | 318 if (game.result === "won" && game.lastMap()) { |
| 319 // game won: three stars | |
| 320 star(ctx, L.w / 2 - 125, L.h / 2 - 15, 50); | |
| 321 star(ctx, L.w / 2 + 125, L.h / 2 - 15, 50); | |
| 322 star(ctx, L.w / 2, L.h / 2 - 45, 70); | |
| 323 } else if (game.result === "won") { | |
| 225 star(ctx, L.w / 2, L.h / 2 - 40, 70); | 324 star(ctx, L.w / 2, L.h / 2 - 40, 70); |
| 226 } else { | 325 } else { |
| 227 heart(ctx, L.w / 2, L.h / 2 - 30, 140, "#8a8a8a"); | 326 heart(ctx, L.w / 2, L.h / 2 - 30, 140, "#8a8a8a"); |
| 228 ctx.strokeStyle = INK; | 327 ctx.strokeStyle = INK; |
| 229 ctx.lineWidth = 5; | 328 ctx.lineWidth = 5; |
| 232 ctx.lineTo(L.w / 2 + 10, L.h / 2 - 40); | 331 ctx.lineTo(L.w / 2 + 10, L.h / 2 - 40); |
| 233 ctx.lineTo(L.w / 2 - 10, L.h / 2 - 20); | 332 ctx.lineTo(L.w / 2 - 10, L.h / 2 - 20); |
| 234 ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22); | 333 ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22); |
| 235 ctx.stroke(); | 334 ctx.stroke(); |
| 236 } | 335 } |
| 237 buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); | 336 } |
| 238 } | 337 if (screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); |
| 239 | 338 |
| 240 if (cfg.debug.show_stats && view.stats) { | 339 if (cfg.debug.show_stats && view.stats) { |
| 241 var st = view.stats; | 340 var st = view.stats; |
| 242 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " + | 341 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " + |
| 243 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed, | 342 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed, |
