wobblyhead hg > minor_monsters
diff game/js/ui.js @ 6:910b26feca76
full game flow
| author | ark |
|---|---|
| date | 2026-10-19 01:05 +0200 |
| parents | 5492efd60e2c |
| children | 2c4fed8baed9 |
line wrap: on
line diff
--- a/game/js/ui.js Sun Oct 18 22:50:24 2026 +0200 +++ b/game/js/ui.js Mon Oct 19 01:05:29 2026 +0200 @@ -1,4 +1,4 @@ -// screen layout, top bar, tower shop and end screens. icons are placeholder +// screen layout, top bar, tower shop, tower menu, splash and end screens. icons are placeholder // shapes until the drawings arrive (art.md); numbers use a system font. window.MM = window.MM || {}; @@ -12,6 +12,7 @@ var SHOP = 96; // shop width (landscape) or height (portrait) var BUTTON = 52; // top bar buttons, at least 48 for touch var SHOP_BUTTON = 80; + var MENU_BUTTON = 64; // upgrade and sell, next to the selected tower var INK = "#3a3530"; var PAPER = "#f6f1e6"; var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif"; @@ -34,12 +35,37 @@ return L; } + // upgrade and sell buttons above the selected tower, or below it when + // there is no room above, kept inside the map area. no upgrade button + // at the top level. + function towerMenu(game, L) { + var t = game.selected, a = L.area, gap = 8, out = []; + 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 w = 2 * MENU_BUTTON + gap; + var top = y - half - MENU_BUTTON; + 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); + if (MM.tower.upgradeCost(t)) { + out.push({ id: "upgrade", x: left, y: top, w: MENU_BUTTON, h: MENU_BUTTON }); + } + out.push({ id: "sell", x: left + MENU_BUTTON + gap, y: top, w: MENU_BUTTON, h: MENU_BUTTON }); + return out; + } + // the buttons for the current state, each {id, x, y, w, h} function buttons(game, L) { var out = []; var y = (TOP - BUTTON) / 2; + if (game.splash) { + out.push({ id: "play", x: L.w / 2 - 56, y: L.h / 2 + 10, w: 112, h: 112 }); + return out; + } + // map lost: back to the first map. map won: on to the next map, or + // after the last map back to the splash screen if (game.result) { - out.push({ id: "restart", x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 }); + var id = game.result === "lost" ? "retry" : game.lastMap() ? "restart" : "next"; + out.push({ id: id, x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 }); return out; } out.push({ id: "pause", x: L.w - BUTTON - 8, y: y, w: BUTTON, h: BUTTON }); @@ -56,6 +82,7 @@ b.w = b.h = SHOP_BUTTON; out.push(b); }); + if (game.selected) out = out.concat(towerMenu(game, L)); return out; } @@ -138,6 +165,45 @@ ctx.fill(); } + function arrowRight(ctx, x, y, s) { + ctx.save(); + ctx.translate(x, y); + ctx.rotate(Math.PI / 2); + arrowUp(ctx, 0, 0, s); + ctx.restore(); + } + + function arrowUp(ctx, x, y, s) { + ctx.beginPath(); + ctx.moveTo(x, y - s * 0.5); + ctx.lineTo(x + s * 0.45, y); + ctx.lineTo(x + s * 0.17, y); + ctx.lineTo(x + s * 0.17, y + s * 0.5); + ctx.lineTo(x - s * 0.17, y + s * 0.5); + ctx.lineTo(x - s * 0.17, y); + ctx.lineTo(x - s * 0.45, y); + ctx.closePath(); + ctx.fill(); + } + + function cross(ctx, x, y, s) { + ctx.lineWidth = 6; + ctx.lineCap = "round"; + ctx.beginPath(); + ctx.moveTo(x - s / 2, y - s / 2); + ctx.lineTo(x + s / 2, y + s / 2); + ctx.moveTo(x + s / 2, y - s / 2); + ctx.lineTo(x - s / 2, y + s / 2); + ctx.stroke(); + ctx.lineCap = "butt"; + } + + // a coin and an amount along the bottom of button b + function price(ctx, b, s) { + icon(ctx, "c0", b.x + 16, b.y + b.h - 13, 16); + text(ctx, s, b.x + 27, b.y + b.h - 12, "left", 18); + } + function box(ctx, b, fill, line) { ctx.fillStyle = fill; ctx.strokeStyle = line || INK; @@ -188,7 +254,28 @@ icon(ctx, "c0", b.x + 22, b.y + b.h - 14, 16); text(ctx, def.cost + "", b.x + 34, b.y + b.h - 13, "left", 18); ctx.globalAlpha = 1; - } else if (b.id === "restart") { + } else if (b.id === "upgrade") { + var cost = MM.tower.upgradeCost(game.selected); + box(ctx, b, PAPER); + ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35; + ctx.fillStyle = INK; + arrowUp(ctx, cx, b.y + 22, 28); + price(ctx, b, cost + ""); + ctx.globalAlpha = 1; + } else if (b.id === "sell") { + box(ctx, b, PAPER); + ctx.strokeStyle = "#c03030"; + cross(ctx, cx, b.y + 22, 20); + price(ctx, b, "+" + MM.tower.refund(game.selected, game.pauseNumber())); + } else if (b.id === "play") { + box(ctx, b, PAPER); + ctx.fillStyle = INK; + triangle(ctx, cx + 4, cy, 60); + } else if (b.id === "next") { + box(ctx, b, PAPER); + ctx.fillStyle = INK; + arrowRight(ctx, cx, cy, 52); + } else if (b.id === "retry" || b.id === "restart") { box(ctx, b, PAPER); restartIcon(ctx, cx, cy, 26); } @@ -212,16 +299,28 @@ icon(ctx, "m0", 206, TOP / 2, 30); text(ctx, game.waves.shown() + "/" + game.waves.list.length, 226, TOP / 2); - if (!game.result) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); + var screen = game.splash || game.result; + if (!screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); - if (game.paused && !game.result) { + 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 (game.result) { + if (game.splash) { + // title and byline, placeholders until the drawings arrive + ctx.fillStyle = "rgba(246, 241, 230, 0.55)"; + ctx.fillRect(0, 0, L.w, L.h); + text(ctx, "minor monsters", L.w / 2, L.h / 2 - 90, "center", 60); + text(ctx, "By M.A. and J.A. Juszczyk", L.w / 2, L.h / 2 - 38, "center", 24); + } else if (game.result) { ctx.fillStyle = "rgba(40, 35, 30, 0.45)"; ctx.fillRect(0, 0, L.w, L.h); - if (game.result === "won") { + if (game.result === "won" && game.lastMap()) { + // game won: three stars + star(ctx, L.w / 2 - 125, L.h / 2 - 15, 50); + star(ctx, L.w / 2 + 125, L.h / 2 - 15, 50); + star(ctx, L.w / 2, L.h / 2 - 45, 70); + } else if (game.result === "won") { star(ctx, L.w / 2, L.h / 2 - 40, 70); } else { heart(ctx, L.w / 2, L.h / 2 - 30, 140, "#8a8a8a"); @@ -234,8 +333,8 @@ ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22); ctx.stroke(); } - buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); } + if (screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); if (cfg.debug.show_stats && view.stats) { var st = view.stats;
