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