wobblyhead hg > minor_monsters
comparison game/js/render.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 | 3f2811aebb3d |
| children |
comparison
equal
deleted
inserted
replaced
| 26:28e1fc486be5 | 27:75deda08db90 |
|---|---|
| 5 | 5 |
| 6 MM.render = (function () { | 6 MM.render = (function () { |
| 7 "use strict"; | 7 "use strict"; |
| 8 | 8 |
| 9 var cfg = MM.config; | 9 var cfg = MM.config; |
| 10 var dbg = cfg.debug; | |
| 11 var A = MM.assets; | 10 var A = MM.assets; |
| 12 // debug placement grid, drawn once per map into a canvas of its own | |
| 13 var gridCache = { map: null, canvas: null }; | |
| 14 | |
| 15 function gridCanvas(map) { | |
| 16 if (gridCache.map === map.index) return gridCache.canvas; | |
| 17 var d = map.data; | |
| 18 var c = document.createElement("canvas"); | |
| 19 c.width = d.width; | |
| 20 c.height = d.height; | |
| 21 var g = c.getContext("2d"); | |
| 22 g.fillStyle = "rgba(40, 160, 40, 0.55)"; | |
| 23 for (var i = 0; i < d.grid.length; i++) { | |
| 24 if (d.grid.charCodeAt(i) !== 49) continue; | |
| 25 g.fillRect((i % d.cols) * d.cell, Math.floor(i / d.cols) * d.cell, d.cell - 1, d.cell - 1); | |
| 26 } | |
| 27 gridCache.map = map.index; | |
| 28 gridCache.canvas = c; | |
| 29 return c; | |
| 30 } | |
| 31 | 11 |
| 32 function circle(ctx, x, y, r, fill, stroke, width) { | 12 function circle(ctx, x, y, r, fill, stroke, width) { |
| 33 ctx.beginPath(); | 13 ctx.beginPath(); |
| 34 ctx.arc(x, y, r, 0, Math.PI * 2); | 14 ctx.arc(x, y, r, 0, Math.PI * 2); |
| 35 if (fill) { | 15 if (fill) { |
| 39 if (stroke) { | 19 if (stroke) { |
| 40 ctx.strokeStyle = stroke; | 20 ctx.strokeStyle = stroke; |
| 41 ctx.lineWidth = width; | 21 ctx.lineWidth = width; |
| 42 ctx.stroke(); | 22 ctx.stroke(); |
| 43 } | 23 } |
| 44 } | |
| 45 | |
| 46 function drawRoutes(ctx, map) { | |
| 47 var colours = ["#d0009f", "#005aff", "#ff7800", "#00a03c", "#8c00ff", "#00b4c8", "#c82828", "#5a5a5a"]; | |
| 48 map.routes.forEach(function (r, k) { | |
| 49 ctx.beginPath(); | |
| 50 for (var i = 0; i < r.xs.length; i++) ctx.lineTo(r.xs[i], r.ys[i]); | |
| 51 ctx.strokeStyle = colours[k % colours.length]; | |
| 52 ctx.lineWidth = 2; | |
| 53 ctx.stroke(); | |
| 54 }); | |
| 55 } | 24 } |
| 56 | 25 |
| 57 // t0 spikes: one p0 per side that faces the path. during an attack each | 26 // t0 spikes: one p0 per side that faces the path. during an attack each |
| 58 // spike stretches from its base under the tower until its tip reaches | 27 // spike stretches from its base under the tower until its tip reaches |
| 59 // the range, and back | 28 // the range, and back |
| 131 A.draw(ctx, "freeze", f.x, f.y, 2 * f.r / Math.max(s[0], s[1]), null, k, k); | 100 A.draw(ctx, "freeze", f.x, f.y, 2 * f.r / Math.max(s[0], s[1]), null, k, k); |
| 132 ctx.globalAlpha = 1; | 101 ctx.globalAlpha = 1; |
| 133 } | 102 } |
| 134 | 103 |
| 135 // draw the map area. L is the layout from ui.js, view holds the canvas | 104 // draw the map area. L is the layout from ui.js, view holds the canvas |
| 136 // pixel ratio, wall clock and tap feedback. | 105 // pixel ratio, wall clock and tap feedback. js/debug.js, when it is not |
| 106 // empty, draws over the map image and over the monsters. | |
| 137 function draw(ctx, game, L, view) { | 107 function draw(ctx, game, L, view) { |
| 138 var map = game.map, s = L.scale, dpr = view.dpr; | 108 var map = game.map, s = L.scale, dpr = view.dpr; |
| 139 ctx.setTransform(dpr * s, 0, 0, dpr * s, dpr * L.ox, dpr * L.oy); | 109 ctx.setTransform(dpr * s, 0, 0, dpr * s, dpr * L.ox, dpr * L.oy); |
| 140 ctx.drawImage(A.img(map.name), 0, 0, map.width, map.height); | 110 ctx.drawImage(A.img(map.name), 0, 0, map.width, map.height); |
| 141 | 111 |
| 142 if (dbg.show_grid) ctx.drawImage(gridCanvas(map), 0, 0); | 112 if (MM.debug) MM.debug.drawGround(ctx, game, s); |
| 143 if (dbg.show_routes) drawRoutes(ctx, map); | |
| 144 | 113 |
| 145 game.towers.slice().sort(function (a, b) { return a.y - b.y; }) | 114 game.towers.slice().sort(function (a, b) { return a.y - b.y; }) |
| 146 .forEach(function (t) { drawTower(ctx, t, 1); }); | 115 .forEach(function (t) { drawTower(ctx, t, 1); }); |
| 147 if (dbg.show_ranges) { | |
| 148 game.towers.forEach(function (t) { drawRange(ctx, t.x, t.y, MM.tower.range(t), s); }); | |
| 149 } | |
| 150 if (game.selected) { | 116 if (game.selected) { |
| 151 var t = game.selected; | 117 var t = game.selected; |
| 152 drawRange(ctx, t.x, t.y, MM.tower.range(t), s); | 118 drawRange(ctx, t.x, t.y, MM.tower.range(t), s); |
| 153 drawTower(ctx, t, 1); | 119 drawTower(ctx, t, 1); |
| 154 } | 120 } |
| 164 // the splashes lie on the ground, under the monsters | 130 // the splashes lie on the ground, under the monsters |
| 165 game.freezes.forEach(function (f) { drawFreeze(ctx, f); }); | 131 game.freezes.forEach(function (f) { drawFreeze(ctx, f); }); |
| 166 game.monsters.slice().sort(function (a, b) { return a.y - b.y; }) | 132 game.monsters.slice().sort(function (a, b) { return a.y - b.y; }) |
| 167 .forEach(function (m) { drawMonster(ctx, m, game.time); }); | 133 .forEach(function (m) { drawMonster(ctx, m, game.time); }); |
| 168 game.puffs.forEach(function (p) { drawPuff(ctx, p); }); | 134 game.puffs.forEach(function (p) { drawPuff(ctx, p); }); |
| 169 if (dbg.show_hit_areas) { | 135 if (MM.debug) MM.debug.drawTop(ctx, game, s); |
| 170 game.monsters.forEach(function (m) { | |
| 171 circle(ctx, m.x, m.y, m.hitRadius, null, "rgba(255, 0, 0, 0.8)", 1 / s); | |
| 172 }); | |
| 173 game.freezes.forEach(function (f) { | |
| 174 circle(ctx, f.x, f.y, f.r, null, "rgba(0, 90, 255, 0.8)", 1 / s); | |
| 175 }); | |
| 176 } | |
| 177 | 136 |
| 178 game.projectiles.forEach(function (p) { | 137 game.projectiles.forEach(function (p) { |
| 179 A.draw(ctx, p.kind, p.x, p.y, cfg.scale.sprite, p.angle); | 138 A.draw(ctx, p.kind, p.x, p.y, cfg.scale.sprite, p.angle); |
| 180 }); | 139 }); |
| 181 | 140 |
| 210 var h = size * img.naturalHeight / img.naturalWidth; | 169 var h = size * img.naturalHeight / img.naturalWidth; |
| 211 ctx.drawImage(img, x - size / 2, y - h / 2, size, h); | 170 ctx.drawImage(img, x - size / 2, y - h / 2, size, h); |
| 212 }); | 171 }); |
| 213 } | 172 } |
| 214 | 173 |
| 215 return { draw: draw, drawFlights: drawFlights }; | 174 return { draw: draw, drawFlights: drawFlights, circle: circle, drawRange: drawRange }; |
| 216 })(); | 175 })(); |
