comparison game/js/debug.js @ 27:75deda08db90 0.9.1

game debug functions separated into own file; release build option, distribution script
author ark
date 2026-10-20 17:19 +0200
parents
children
comparison
equal deleted inserted replaced
26:28e1fc486be5 27:75deda08db90
1 // debug switches from [debug] in config/game.toml (architecture.md). this
2 // file replaces functions of MM.Game and MM.ui, and draws over the map
3 // for render.js. a release build writes it empty, and the game runs as if
4 // every switch were off.
5
6 window.MM = window.MM || {};
7
8 MM.debug = (function () {
9 "use strict";
10
11 var cfg = MM.config;
12 var dbg = cfg.debug;
13 var INK = "#3a3530";
14 var PAPER = "#f6f1e6";
15
16 // rules: start coins, unlimited coins, no heart loss
17 var Game = MM.Game;
18 MM.Game = function (mapIndex, seed, firstWave) {
19 Game.call(this, mapIndex, seed, firstWave);
20 if (dbg.start_coins >= 0) this.coins = dbg.start_coins;
21 };
22 MM.Game.prototype = Game.prototype;
23
24 if (dbg.unlimited_coins) {
25 Game.prototype.canAfford = function () { return true; };
26 Game.prototype.pay = function () {};
27 }
28
29 if (dbg.no_heart_loss) {
30 var escape = Game.prototype.escape;
31 Game.prototype.escape = function (m) {
32 var hearts = this.hearts;
33 escape.call(this, m);
34 this.hearts = hearts;
35 };
36 }
37
38 // placement grid, drawn once per map into a canvas of its own
39 var gridCache = { map: null, canvas: null };
40
41 function gridCanvas(map) {
42 if (gridCache.map === map.index) return gridCache.canvas;
43 var d = map.data;
44 var c = document.createElement("canvas");
45 c.width = d.width;
46 c.height = d.height;
47 var g = c.getContext("2d");
48 g.fillStyle = "rgba(40, 160, 40, 0.55)";
49 for (var i = 0; i < d.grid.length; i++) {
50 if (d.grid.charCodeAt(i) !== 49) continue;
51 g.fillRect((i % d.cols) * d.cell, Math.floor(i / d.cols) * d.cell, d.cell - 1, d.cell - 1);
52 }
53 gridCache.map = map.index;
54 gridCache.canvas = c;
55 return c;
56 }
57
58 function drawRoutes(ctx, map) {
59 var colours = ["#d0009f", "#005aff", "#ff7800", "#00a03c", "#8c00ff", "#00b4c8", "#c82828", "#5a5a5a"];
60 map.routes.forEach(function (r, k) {
61 ctx.beginPath();
62 for (var i = 0; i < r.xs.length; i++) ctx.lineTo(r.xs[i], r.ys[i]);
63 ctx.strokeStyle = colours[k % colours.length];
64 ctx.lineWidth = 2;
65 ctx.stroke();
66 });
67 }
68
69 // grid, routes and tower ranges, over the map image and under the
70 // towers. s is the map scale
71 function drawGround(ctx, game, s) {
72 var map = game.map;
73 if (dbg.show_grid) ctx.drawImage(gridCanvas(map), 0, 0);
74 if (dbg.show_routes) drawRoutes(ctx, map);
75 if (dbg.show_ranges) {
76 game.towers.forEach(function (t) { MM.render.drawRange(ctx, t.x, t.y, MM.tower.range(t), s); });
77 }
78 }
79
80 // hit areas, over the monsters
81 function drawTop(ctx, game, s) {
82 if (!dbg.show_hit_areas) return;
83 game.monsters.forEach(function (m) {
84 MM.render.circle(ctx, m.x, m.y, m.hitRadius, null, "rgba(255, 0, 0, 0.8)", 1 / s);
85 });
86 game.freezes.forEach(function (f) {
87 MM.render.circle(ctx, f.x, f.y, f.r, null, "rgba(0, 90, 255, 0.8)", 1 / s);
88 });
89 }
90
91 // stats: frame time in ms, smoothed, and entity counts, drawn after the
92 // top bar, shop and screens
93 var frameMs = 0;
94
95 function frameTime(ms) {
96 frameMs = frameMs * 0.9 + ms * 0.1;
97 }
98
99 if (dbg.show_stats) {
100 var uiDraw = MM.ui.draw;
101 MM.ui.draw = function (ctx, game, L, view) {
102 uiDraw(ctx, game, L, view);
103 var s = frameMs.toFixed(1) + " ms " + game.monsters.length + " m " +
104 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed;
105 ctx.setTransform(view.dpr, 0, 0, view.dpr, 0, 0);
106 ctx.font = "bold 14px system-ui, -apple-system, Segoe UI, sans-serif";
107 ctx.textAlign = "left";
108 ctx.textBaseline = "middle";
109 ctx.lineWidth = 4;
110 ctx.strokeStyle = PAPER;
111 ctx.strokeText(s, 8, L.h - 14);
112 ctx.fillStyle = INK;
113 ctx.fillText(s, 8, L.h - 14);
114 };
115 }
116
117 return {
118 start: { map: dbg.start_map, wave: dbg.start_wave, seed: dbg.seed },
119 drawGround: drawGround,
120 drawTop: drawTop,
121 frameTime: frameTime
122 };
123 })();