view game/js/debug.js @ 34:83e8b0e0f010 0.10.1

homepage link on splash does not open a new tab anymore
author ark
date 2026-10-20 19:54 +0200
parents 75deda08db90
children
line wrap: on
line source

// debug switches from [debug] in config/game.toml (architecture.md). this
// file replaces functions of MM.Game and MM.ui, and draws over the map
// for render.js. a release build writes it empty, and the game runs as if
// every switch were off.

window.MM = window.MM || {};

MM.debug = (function () {
    "use strict";

    var cfg = MM.config;
    var dbg = cfg.debug;
    var INK = "#3a3530";
    var PAPER = "#f6f1e6";

    // rules: start coins, unlimited coins, no heart loss
    var Game = MM.Game;
    MM.Game = function (mapIndex, seed, firstWave) {
        Game.call(this, mapIndex, seed, firstWave);
        if (dbg.start_coins >= 0) this.coins = dbg.start_coins;
    };
    MM.Game.prototype = Game.prototype;

    if (dbg.unlimited_coins) {
        Game.prototype.canAfford = function () { return true; };
        Game.prototype.pay = function () {};
    }

    if (dbg.no_heart_loss) {
        var escape = Game.prototype.escape;
        Game.prototype.escape = function (m) {
            var hearts = this.hearts;
            escape.call(this, m);
            this.hearts = hearts;
        };
    }

    // placement grid, drawn once per map into a canvas of its own
    var gridCache = { map: null, canvas: null };

    function gridCanvas(map) {
        if (gridCache.map === map.index) return gridCache.canvas;
        var d = map.data;
        var c = document.createElement("canvas");
        c.width = d.width;
        c.height = d.height;
        var g = c.getContext("2d");
        g.fillStyle = "rgba(40, 160, 40, 0.55)";
        for (var i = 0; i < d.grid.length; i++) {
            if (d.grid.charCodeAt(i) !== 49) continue;
            g.fillRect((i % d.cols) * d.cell, Math.floor(i / d.cols) * d.cell, d.cell - 1, d.cell - 1);
        }
        gridCache.map = map.index;
        gridCache.canvas = c;
        return c;
    }

    function drawRoutes(ctx, map) {
        var colours = ["#d0009f", "#005aff", "#ff7800", "#00a03c", "#8c00ff", "#00b4c8", "#c82828", "#5a5a5a"];
        map.routes.forEach(function (r, k) {
            ctx.beginPath();
            for (var i = 0; i < r.xs.length; i++) ctx.lineTo(r.xs[i], r.ys[i]);
            ctx.strokeStyle = colours[k % colours.length];
            ctx.lineWidth = 2;
            ctx.stroke();
        });
    }

    // grid, routes and tower ranges, over the map image and under the
    // towers. s is the map scale
    function drawGround(ctx, game, s) {
        var map = game.map;
        if (dbg.show_grid) ctx.drawImage(gridCanvas(map), 0, 0);
        if (dbg.show_routes) drawRoutes(ctx, map);
        if (dbg.show_ranges) {
            game.towers.forEach(function (t) { MM.render.drawRange(ctx, t.x, t.y, MM.tower.range(t), s); });
        }
    }

    // hit areas, over the monsters
    function drawTop(ctx, game, s) {
        if (!dbg.show_hit_areas) return;
        game.monsters.forEach(function (m) {
            MM.render.circle(ctx, m.x, m.y, m.hitRadius, null, "rgba(255, 0, 0, 0.8)", 1 / s);
        });
        game.freezes.forEach(function (f) {
            MM.render.circle(ctx, f.x, f.y, f.r, null, "rgba(0, 90, 255, 0.8)", 1 / s);
        });
    }

    // stats: frame time in ms, smoothed, and entity counts, drawn after the
    // top bar, shop and screens
    var frameMs = 0;

    function frameTime(ms) {
        frameMs = frameMs * 0.9 + ms * 0.1;
    }

    if (dbg.show_stats) {
        var uiDraw = MM.ui.draw;
        MM.ui.draw = function (ctx, game, L, view) {
            uiDraw(ctx, game, L, view);
            var s = frameMs.toFixed(1) + " ms  " + game.monsters.length + " m  " +
                    game.towers.length + " t  " + game.projectiles.length + " p  seed " + game.seed;
            ctx.setTransform(view.dpr, 0, 0, view.dpr, 0, 0);
            ctx.font = "bold 14px system-ui, -apple-system, Segoe UI, sans-serif";
            ctx.textAlign = "left";
            ctx.textBaseline = "middle";
            ctx.lineWidth = 4;
            ctx.strokeStyle = PAPER;
            ctx.strokeText(s, 8, L.h - 14);
            ctx.fillStyle = INK;
            ctx.fillText(s, 8, L.h - 14);
        };
    }

    return {
        start: { map: dbg.start_map, wave: dbg.start_wave, seed: dbg.seed },
        drawGround: drawGround,
        drawTop: drawTop,
        frameTime: frameTime
    };
})();