view game/js/ui.js @ 2:3dd5b745fa08

updated art with inked drawings
author ark
date 2026-10-18 15:08 +0200
parents 5492efd60e2c
children 910b26feca76
line wrap: on
line source

// screen layout, top bar, tower shop and end screens. icons are placeholder
// shapes until the drawings arrive (art.md); numbers use a system font.

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_BUTTON = 80;
    var INK = "#3a3530";
    var PAPER = "#f6f1e6";
    var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
    var TOWERS = ["t0", "t1", "t2", "t3"];

    // 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: 26, y: TOP / 2, size: 28 }
        };
        return L;
    }

    // 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.result) {
            out.push({ id: "restart", 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 });
        out.push({ id: "speed", x: L.w - 2 * BUTTON - 16, y: y, w: BUTTON, h: BUTTON });
        if (game.waves.phase === "break") {
            out.push({ id: "early", x: L.w - 2 * BUTTON - 150, y: y, w: 126, h: BUTTON });
        }
        TOWERS.forEach(function (t, i) {
            var pos = 10 + i * (SHOP_BUTTON + 10);
            var b = L.land ? { x: L.shop.x + (SHOP - SHOP_BUTTON) / 2, y: L.shop.y + pos }
                           : { x: L.shop.x + pos, y: L.shop.y + (SHOP - SHOP_BUTTON) / 2 };
            b.id = "shop";
            b.tower = t;
            b.w = b.h = SHOP_BUTTON;
            out.push(b);
        });
        return out;
    }

    function hit(game, L, x, y) {
        var list = buttons(game, L);
        for (var i = 0; i < list.length; i++) {
            var b = list[i];
            if (x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h) return b;
        }
        return null;
    }

    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 size s, centred on x, y
    function icon(ctx, name, x, y, s) {
        var img = A.img(name);
        var k = s / Math.max(img.naturalWidth, img.naturalHeight);
        var w = img.naturalWidth * k, h = img.naturalHeight * k;
        ctx.drawImage(img, x - w / 2, y - h / 2, w, h);
    }

    function heart(ctx, x, y, s, fill) {
        ctx.beginPath();
        ctx.moveTo(x, y + s * 0.4);
        ctx.bezierCurveTo(x - s * 0.6, y, x - s * 0.5, y - s * 0.5, x, y - s * 0.2);
        ctx.bezierCurveTo(x + s * 0.5, y - s * 0.5, x + s * 0.6, y, x, y + s * 0.4);
        ctx.fillStyle = fill;
        ctx.fill();
        ctx.lineWidth = 2;
        ctx.strokeStyle = INK;
        ctx.stroke();
    }

    function triangle(ctx, x, y, s) {
        ctx.beginPath();
        ctx.moveTo(x - s * 0.35, y - s * 0.45);
        ctx.lineTo(x + s * 0.45, y);
        ctx.lineTo(x - s * 0.35, y + s * 0.45);
        ctx.closePath();
        ctx.fill();
    }

    function star(ctx, x, y, r) {
        ctx.beginPath();
        for (var i = 0; i < 10; i++) {
            var a = -Math.PI / 2 + i * Math.PI / 5, rr = i % 2 ? r * 0.45 : r;
            ctx.lineTo(x + Math.cos(a) * rr, y + Math.sin(a) * rr);
        }
        ctx.closePath();
        ctx.fillStyle = "#f2c230";
        ctx.fill();
        ctx.lineWidth = 3;
        ctx.strokeStyle = INK;
        ctx.stroke();
    }

    function restartIcon(ctx, x, y, r) {
        ctx.lineWidth = 7;
        ctx.strokeStyle = INK;
        ctx.beginPath();
        ctx.arc(x, y, r, -Math.PI * 0.35, Math.PI * 1.45);
        ctx.stroke();
        var a = -Math.PI * 0.35, tx = x + Math.cos(a) * r, ty = y + Math.sin(a) * r;
        ctx.fillStyle = INK;
        ctx.beginPath();
        ctx.moveTo(tx + 12, ty - 4);
        ctx.lineTo(tx - 8, ty - 12);
        ctx.lineTo(tx - 2, ty + 10);
        ctx.closePath();
        ctx.fill();
    }

    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();
    }

    function drawButton(ctx, game, b) {
        var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
        if (b.id === "pause") {
            box(ctx, b, game.paused ? "#ffe9a8" : PAPER);
            ctx.fillStyle = INK;
            if (game.paused) {
                triangle(ctx, cx, cy, 26);
            } else {
                ctx.fillRect(cx - 10, cy - 12, 7, 24);
                ctx.fillRect(cx + 3, cy - 12, 7, 24);
            }
        } else if (b.id === "speed") {
            // one triangle per speed step; numbers for the debug speeds
            var sp = game.speed();
            box(ctx, b, sp > 1 ? "#ffe9a8" : PAPER);
            ctx.fillStyle = INK;
            if (sp <= 3) {
                for (var i = 0; i < sp; i++) triangle(ctx, cx + (i - (sp - 1) / 2) * 12, cy, 20);
            } else {
                text(ctx, sp + "", cx, cy, "center");
            }
        } else if (b.id === "early") {
            box(ctx, b, PAPER);
            ctx.fillStyle = INK;
            triangle(ctx, b.x + 20, cy, 22);
            ctx.fillRect(b.x + 29, cy - 10, 4, 20);
            text(ctx, Math.ceil(game.waves.left) + "", b.x + 58, cy, "center");
            var bonus = game.waves.bonus();
            if (bonus > 0) text(ctx, "+" + bonus, b.x + b.w - 6, cy, "right", 18);
        } else if (b.id === "shop") {
            var def = cfg.tower[b.tower];
            var ok = game.canAfford(def.cost);
            box(ctx, b, game.shop === b.tower ? "#ffe9a8" : PAPER,
                game.shop === b.tower ? "#c08000" : INK);
            ctx.globalAlpha = ok ? 1 : 0.35;
            icon(ctx, b.tower, cx, b.y + 30, 46);
            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") {
            box(ctx, b, PAPER);
            restartIcon(ctx, cx, cy, 26);
        }
    }

    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);
        icon(ctx, "c0", L.coinIcon.x, L.coinIcon.y, L.coinIcon.size);
        text(ctx, game.coins + "", 46, TOP / 2);
        heart(ctx, 126, TOP / 2 + 2, 30, "#e03a3a");
        text(ctx, game.hearts + "", 146, TOP / 2);
        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); });

        if (game.paused && !game.result) {
            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) {
            ctx.fillStyle = "rgba(40, 35, 30, 0.45)";
            ctx.fillRect(0, 0, L.w, L.h);
            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");
                ctx.strokeStyle = INK;
                ctx.lineWidth = 5;
                ctx.beginPath();
                ctx.moveTo(L.w / 2 - 6, L.h / 2 - 72);
                ctx.lineTo(L.w / 2 + 10, L.h / 2 - 40);
                ctx.lineTo(L.w / 2 - 10, L.h / 2 - 20);
                ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22);
                ctx.stroke();
            }
            buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); });
        }

        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 };
})();