wobblyhead hg > minor_monsters
view 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 |
line wrap: on
line source
// drawing of the map and everything on it. reads the game state, never // changes it. the top bar, shop and screens are drawn by ui.js. window.MM = window.MM || {}; MM.render = (function () { "use strict"; var cfg = MM.config; var A = MM.assets; function circle(ctx, x, y, r, fill, stroke, width) { ctx.beginPath(); ctx.arc(x, y, r, 0, Math.PI * 2); if (fill) { ctx.fillStyle = fill; ctx.fill(); } if (stroke) { ctx.strokeStyle = stroke; ctx.lineWidth = width; ctx.stroke(); } } // t0 spikes: one p0 per side that faces the path. during an attack each // spike stretches from its base under the tower until its tip reaches // the range, and back function drawSpikes(ctx, t, scale) { var half = A.size(MM.tower.sprite(t), scale)[1] / 2; var spike = A.size("p0", scale)[1]; var k = t.burst > 0 ? Math.sin(Math.PI * (1 - t.burst / MM.tower.BURST_TIME)) : 0; // at rest a quarter of the spike shows beyond the tower's edge var base = half - spike * 0.75; var tip = MM.util.lerp(half + spike * 0.25, MM.tower.range(t), k); var mid = (base + tip) / 2, stretch = (tip - base) / spike; for (var side = 0; side < 4; side++) { if (!t.sides[side]) continue; var a = -Math.PI / 2 + side * Math.PI / 2; A.draw(ctx, "p0", t.x + Math.cos(a) * mid, t.y + Math.sin(a) * mid, scale, a, 1, stretch); } } // k scales the drawing, for the pulsing preview function drawTower(ctx, t, alpha, k) { var scale = cfg.scale.sprite * (k || 1); ctx.globalAlpha = alpha; if (t.def.projectile_speed === 0) drawSpikes(ctx, t, scale); A.draw(ctx, MM.tower.sprite(t), t.x, t.y, scale, t.angle); ctx.globalAlpha = 1; } function drawRange(ctx, x, y, r, mapScale) { circle(ctx, x, y, r, "rgba(255, 255, 255, 0.25)", "rgba(40, 40, 40, 0.7)", 2 / mapScale); } function drawMonster(ctx, m, time) { var pose = MM.monster.pose(m, time); var img = A.img(m.kind); ctx.save(); // feet on the ground point, so squash and stretch keep them down ctx.translate(m.x, m.y + m.h / 2 - pose.lift); ctx.scale(pose.sx, pose.sy); ctx.drawImage(img, -m.w / 2, -m.h, m.w, m.h); ctx.restore(); // hp bar, cut to the share of hp left over its darkened copy. the // boss has its own bar; a slowed monster's bar is blue var name = (m.boss ? "hpb" : "hpm") + (MM.monster.slowFactor(m) < 1 ? "_slow" : ""); var bar = A.size(name, cfg.scale.hp_bar); var bx = m.x - bar[0] / 2, by = m.y - m.h / 2 - pose.lift - bar[1] - 2; var share = Math.max(0, m.hp / m.maxHp); var full = A.img(name), empty = A.img(name + "_empty"); ctx.drawImage(empty, bx, by, bar[0], bar[1]); if (share > 0) { ctx.drawImage(full, 0, 0, full.naturalWidth * share, full.naturalHeight, bx, by, bar[0] * share, bar[1]); } } // death puff: grows from 60 % to full size, and fades out over the second // half of its time function drawPuff(ctx, p) { var u = p.t / cfg.animation.death_puff; var k = MM.util.lerp(0.6, 1, MM.util.easeOut(Math.min(1, u * 2))); var name = p.boss ? "boss_death" : "monster_death"; ctx.globalAlpha = u < 0.5 ? 1 : 2 - 2 * u; A.draw(ctx, name, p.x, p.y, cfg.scale.sprite, null, k, k); ctx.globalAlpha = 1; } // freeze splash: drawn across the splash diameter. it grows from 60 % to // full size over the first half of its time and fades out over the // second half function drawFreeze(ctx, f) { var u = f.t / cfg.animation.freeze_splash; var s = cfg.sprites.freeze; var k = MM.util.lerp(0.6, 1, MM.util.easeOut(Math.min(1, u * 2))); ctx.globalAlpha = 0.85 * (u < 0.5 ? 1 : 2 - 2 * u); A.draw(ctx, "freeze", f.x, f.y, 2 * f.r / Math.max(s[0], s[1]), null, k, k); ctx.globalAlpha = 1; } // draw the map area. L is the layout from ui.js, view holds the canvas // pixel ratio, wall clock and tap feedback. js/debug.js, when it is not // empty, draws over the map image and over the monsters. function draw(ctx, game, L, view) { var map = game.map, s = L.scale, dpr = view.dpr; ctx.setTransform(dpr * s, 0, 0, dpr * s, dpr * L.ox, dpr * L.oy); ctx.drawImage(A.img(map.name), 0, 0, map.width, map.height); if (MM.debug) MM.debug.drawGround(ctx, game, s); game.towers.slice().sort(function (a, b) { return a.y - b.y; }) .forEach(function (t) { drawTower(ctx, t, 1); }); if (game.selected) { var t = game.selected; drawRange(ctx, t.x, t.y, MM.tower.range(t), s); drawTower(ctx, t, 1); } if (game.preview) { var pv = MM.tower.create(game.shop, game.preview.x, game.preview.y, game.preview.sides); drawRange(ctx, pv.x, pv.y, MM.tower.range(pv), s); // the preview pulses with the chosen shop item: it grows while // the item shows one drawing and shrinks while it shows the other var k = 0.5 - 0.5 * Math.cos(Math.PI * view.now / MM.ui.BUBBLE_FLIP); drawTower(ctx, pv, MM.util.lerp(0.45, 0.8, k), MM.util.lerp(1, 1.08, k)); } // the splashes lie on the ground, under the monsters game.freezes.forEach(function (f) { drawFreeze(ctx, f); }); game.monsters.slice().sort(function (a, b) { return a.y - b.y; }) .forEach(function (m) { drawMonster(ctx, m, game.time); }); game.puffs.forEach(function (p) { drawPuff(ctx, p); }); if (MM.debug) MM.debug.drawTop(ctx, game, s); game.projectiles.forEach(function (p) { A.draw(ctx, p.kind, p.x, p.y, cfg.scale.sprite, p.angle); }); // refused placement: a red cross where the tap was if (view.refused && view.now < view.refused.until) { var f = view.refused, r = 10 / s; ctx.strokeStyle = "#d02020"; ctx.lineWidth = 4 / s; ctx.beginPath(); ctx.moveTo(f.x - r, f.y - r); ctx.lineTo(f.x + r, f.y + r); ctx.moveTo(f.x + r, f.y - r); ctx.lineTo(f.x - r, f.y + r); ctx.stroke(); } } // coins flying from a monster or an exit to the coin counter, in screen // pixels function drawFlights(ctx, game, L, view) { var dpr = view.dpr, ft = cfg.coins.flight_time; ctx.setTransform(dpr, 0, 0, dpr, 0, 0); game.flights.forEach(function (f) { var u = MM.util.clamp(f.t / ft, 0, 1); var e = MM.util.easeIn(u); var sx = L.ox + f.x * L.scale, sy = L.oy + f.y * L.scale; var x = MM.util.lerp(sx, L.coinIcon.x, e); var y = MM.util.lerp(sy, L.coinIcon.y, e) - 40 * Math.sin(Math.PI * u); var start = A.size(f.sprite, cfg.scale.coin)[0] * L.scale; var size = MM.util.lerp(start, L.coinIcon.size, e); var img = A.img(f.sprite); var h = size * img.naturalHeight / img.naturalWidth; ctx.drawImage(img, x - size / 2, y - h / 2, size, h); }); } return { draw: draw, drawFlights: drawFlights, circle: circle, drawRange: drawRange }; })();
