wobblyhead hg > minor_monsters
annotate 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 |
| rev | line source |
|---|---|
| 17 | 1 // drawing of the map and everything on it. reads the game state, never |
| 2 // changes it. the top bar, shop and screens are drawn by ui.js. | |
| 3 | |
| 4 window.MM = window.MM || {}; | |
| 5 | |
| 6 MM.render = (function () { | |
| 7 "use strict"; | |
| 8 | |
| 9 var cfg = MM.config; | |
| 10 var A = MM.assets; | |
| 11 | |
| 12 function circle(ctx, x, y, r, fill, stroke, width) { | |
| 13 ctx.beginPath(); | |
| 14 ctx.arc(x, y, r, 0, Math.PI * 2); | |
| 15 if (fill) { | |
| 16 ctx.fillStyle = fill; | |
| 17 ctx.fill(); | |
| 18 } | |
| 19 if (stroke) { | |
| 20 ctx.strokeStyle = stroke; | |
| 21 ctx.lineWidth = width; | |
| 22 ctx.stroke(); | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 // t0 spikes: one p0 per side that faces the path. during an attack each | |
| 27 // spike stretches from its base under the tower until its tip reaches | |
| 28 // the range, and back | |
| 29 function drawSpikes(ctx, t, scale) { | |
| 30 var half = A.size(MM.tower.sprite(t), scale)[1] / 2; | |
| 31 var spike = A.size("p0", scale)[1]; | |
| 32 var k = t.burst > 0 ? Math.sin(Math.PI * (1 - t.burst / MM.tower.BURST_TIME)) : 0; | |
| 33 // at rest a quarter of the spike shows beyond the tower's edge | |
| 34 var base = half - spike * 0.75; | |
| 35 var tip = MM.util.lerp(half + spike * 0.25, MM.tower.range(t), k); | |
| 36 var mid = (base + tip) / 2, stretch = (tip - base) / spike; | |
| 37 for (var side = 0; side < 4; side++) { | |
| 38 if (!t.sides[side]) continue; | |
| 39 var a = -Math.PI / 2 + side * Math.PI / 2; | |
| 40 A.draw(ctx, "p0", t.x + Math.cos(a) * mid, t.y + Math.sin(a) * mid, scale, a, 1, stretch); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 // k scales the drawing, for the pulsing preview | |
| 45 function drawTower(ctx, t, alpha, k) { | |
| 46 var scale = cfg.scale.sprite * (k || 1); | |
| 47 ctx.globalAlpha = alpha; | |
| 48 if (t.def.projectile_speed === 0) drawSpikes(ctx, t, scale); | |
| 49 A.draw(ctx, MM.tower.sprite(t), t.x, t.y, scale, t.angle); | |
| 50 ctx.globalAlpha = 1; | |
| 51 } | |
| 52 | |
| 53 function drawRange(ctx, x, y, r, mapScale) { | |
| 54 circle(ctx, x, y, r, "rgba(255, 255, 255, 0.25)", "rgba(40, 40, 40, 0.7)", 2 / mapScale); | |
| 55 } | |
| 56 | |
| 57 function drawMonster(ctx, m, time) { | |
| 58 var pose = MM.monster.pose(m, time); | |
| 59 var img = A.img(m.kind); | |
| 60 ctx.save(); | |
| 61 // feet on the ground point, so squash and stretch keep them down | |
| 62 ctx.translate(m.x, m.y + m.h / 2 - pose.lift); | |
| 63 ctx.scale(pose.sx, pose.sy); | |
| 64 ctx.drawImage(img, -m.w / 2, -m.h, m.w, m.h); | |
| 65 ctx.restore(); | |
| 66 | |
| 67 // hp bar, cut to the share of hp left over its darkened copy. the | |
| 68 // boss has its own bar; a slowed monster's bar is blue | |
| 69 var name = (m.boss ? "hpb" : "hpm") + (MM.monster.slowFactor(m) < 1 ? "_slow" : ""); | |
| 70 var bar = A.size(name, cfg.scale.hp_bar); | |
| 71 var bx = m.x - bar[0] / 2, by = m.y - m.h / 2 - pose.lift - bar[1] - 2; | |
| 72 var share = Math.max(0, m.hp / m.maxHp); | |
| 73 var full = A.img(name), empty = A.img(name + "_empty"); | |
| 74 ctx.drawImage(empty, bx, by, bar[0], bar[1]); | |
| 75 if (share > 0) { | |
| 76 ctx.drawImage(full, 0, 0, full.naturalWidth * share, full.naturalHeight, | |
| 77 bx, by, bar[0] * share, bar[1]); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 // death puff: grows from 60 % to full size, and fades out over the second | |
| 82 // half of its time | |
| 83 function drawPuff(ctx, p) { | |
| 84 var u = p.t / cfg.animation.death_puff; | |
| 85 var k = MM.util.lerp(0.6, 1, MM.util.easeOut(Math.min(1, u * 2))); | |
| 86 var name = p.boss ? "boss_death" : "monster_death"; | |
| 87 ctx.globalAlpha = u < 0.5 ? 1 : 2 - 2 * u; | |
| 88 A.draw(ctx, name, p.x, p.y, cfg.scale.sprite, null, k, k); | |
| 89 ctx.globalAlpha = 1; | |
| 90 } | |
| 91 | |
| 24 | 92 // freeze splash: drawn across the splash diameter. it grows from 60 % to |
| 93 // full size over the first half of its time and fades out over the | |
| 94 // second half | |
| 95 function drawFreeze(ctx, f) { | |
| 96 var u = f.t / cfg.animation.freeze_splash; | |
| 97 var s = cfg.sprites.freeze; | |
| 98 var k = MM.util.lerp(0.6, 1, MM.util.easeOut(Math.min(1, u * 2))); | |
| 99 ctx.globalAlpha = 0.85 * (u < 0.5 ? 1 : 2 - 2 * u); | |
| 100 A.draw(ctx, "freeze", f.x, f.y, 2 * f.r / Math.max(s[0], s[1]), null, k, k); | |
| 101 ctx.globalAlpha = 1; | |
| 102 } | |
| 103 | |
| 17 | 104 // draw the map area. L is the layout from ui.js, view holds the canvas |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
24
diff
changeset
|
105 // pixel ratio, wall clock and tap feedback. js/debug.js, when it is not |
|
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
24
diff
changeset
|
106 // empty, draws over the map image and over the monsters. |
| 17 | 107 function draw(ctx, game, L, view) { |
| 108 var map = game.map, s = L.scale, dpr = view.dpr; | |
| 109 ctx.setTransform(dpr * s, 0, 0, dpr * s, dpr * L.ox, dpr * L.oy); | |
| 110 ctx.drawImage(A.img(map.name), 0, 0, map.width, map.height); | |
| 111 | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
24
diff
changeset
|
112 if (MM.debug) MM.debug.drawGround(ctx, game, s); |
| 17 | 113 |
| 114 game.towers.slice().sort(function (a, b) { return a.y - b.y; }) | |
| 115 .forEach(function (t) { drawTower(ctx, t, 1); }); | |
| 116 if (game.selected) { | |
| 117 var t = game.selected; | |
| 118 drawRange(ctx, t.x, t.y, MM.tower.range(t), s); | |
| 119 drawTower(ctx, t, 1); | |
| 120 } | |
| 121 if (game.preview) { | |
| 122 var pv = MM.tower.create(game.shop, game.preview.x, game.preview.y, game.preview.sides); | |
| 123 drawRange(ctx, pv.x, pv.y, MM.tower.range(pv), s); | |
| 124 // the preview pulses with the chosen shop item: it grows while | |
| 125 // the item shows one drawing and shrinks while it shows the other | |
| 126 var k = 0.5 - 0.5 * Math.cos(Math.PI * view.now / MM.ui.BUBBLE_FLIP); | |
| 127 drawTower(ctx, pv, MM.util.lerp(0.45, 0.8, k), MM.util.lerp(1, 1.08, k)); | |
| 128 } | |
| 129 | |
| 24 | 130 // the splashes lie on the ground, under the monsters |
| 131 game.freezes.forEach(function (f) { drawFreeze(ctx, f); }); | |
| 17 | 132 game.monsters.slice().sort(function (a, b) { return a.y - b.y; }) |
| 133 .forEach(function (m) { drawMonster(ctx, m, game.time); }); | |
| 134 game.puffs.forEach(function (p) { drawPuff(ctx, p); }); | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
24
diff
changeset
|
135 if (MM.debug) MM.debug.drawTop(ctx, game, s); |
| 17 | 136 |
| 137 game.projectiles.forEach(function (p) { | |
| 138 A.draw(ctx, p.kind, p.x, p.y, cfg.scale.sprite, p.angle); | |
| 139 }); | |
| 140 | |
| 141 // refused placement: a red cross where the tap was | |
| 142 if (view.refused && view.now < view.refused.until) { | |
| 143 var f = view.refused, r = 10 / s; | |
| 144 ctx.strokeStyle = "#d02020"; | |
| 145 ctx.lineWidth = 4 / s; | |
| 146 ctx.beginPath(); | |
| 147 ctx.moveTo(f.x - r, f.y - r); | |
| 148 ctx.lineTo(f.x + r, f.y + r); | |
| 149 ctx.moveTo(f.x + r, f.y - r); | |
| 150 ctx.lineTo(f.x - r, f.y + r); | |
| 151 ctx.stroke(); | |
| 152 } | |
| 153 } | |
| 154 | |
| 23 | 155 // coins flying from a monster or an exit to the coin counter, in screen |
| 156 // pixels | |
| 17 | 157 function drawFlights(ctx, game, L, view) { |
| 158 var dpr = view.dpr, ft = cfg.coins.flight_time; | |
| 159 ctx.setTransform(dpr, 0, 0, dpr, 0, 0); | |
| 160 game.flights.forEach(function (f) { | |
| 161 var u = MM.util.clamp(f.t / ft, 0, 1); | |
| 162 var e = MM.util.easeIn(u); | |
| 163 var sx = L.ox + f.x * L.scale, sy = L.oy + f.y * L.scale; | |
| 164 var x = MM.util.lerp(sx, L.coinIcon.x, e); | |
| 165 var y = MM.util.lerp(sy, L.coinIcon.y, e) - 40 * Math.sin(Math.PI * u); | |
| 166 var start = A.size(f.sprite, cfg.scale.coin)[0] * L.scale; | |
| 167 var size = MM.util.lerp(start, L.coinIcon.size, e); | |
| 168 var img = A.img(f.sprite); | |
| 169 var h = size * img.naturalHeight / img.naturalWidth; | |
| 170 ctx.drawImage(img, x - size / 2, y - h / 2, size, h); | |
| 171 }); | |
| 172 } | |
| 173 | |
|
27
75deda08db90
game debug functions separated into own file; release build option, distribution script
ark
parents:
24
diff
changeset
|
174 return { draw: draw, drawFlights: drawFlights, circle: circle, drawRange: drawRange }; |
| 17 | 175 })(); |
