comparison game/js/ui.js @ 8:2c4fed8baed9

art update, persistent sound setting, tower selling as double action
author ark
date 2026-10-19 13:37 +0200
parents 910b26feca76
children a77857594150
comparison
equal deleted inserted replaced
7:4fd2da999ba4 8:2c4fed8baed9
1 // screen layout, top bar, tower shop, tower menu, splash and end screens. icons are placeholder 1 // screen layout, top bar, tower shop, tower menu, splash and end screens,
2 // shapes until the drawings arrive (art.md); numbers use a system font. 2 // drawn from the supplied icons and drawings (art.md). numbers use the
3 // hand-drawn digits.
3 4
4 window.MM = window.MM || {}; 5 window.MM = window.MM || {};
5 6
6 MM.ui = (function () { 7 MM.ui = (function () {
7 "use strict"; 8 "use strict";
9 var cfg = MM.config; 10 var cfg = MM.config;
10 var A = MM.assets; 11 var A = MM.assets;
11 var TOP = 60; // top bar height, css pixels 12 var TOP = 60; // top bar height, css pixels
12 var SHOP = 96; // shop width (landscape) or height (portrait) 13 var SHOP = 96; // shop width (landscape) or height (portrait)
13 var BUTTON = 52; // top bar buttons, at least 48 for touch 14 var BUTTON = 52; // top bar buttons, at least 48 for touch
14 var SHOP_BUTTON = 80; 15 var SHOP_W = 84; // shop items
15 var MENU_BUTTON = 64; // upgrade and sell, next to the selected tower 16 var SHOP_H = 72;
17 var POPUP_W = 168; // tower menu bubble; its height follows the drawing
18 var DIGIT = 28; // digit height in the top bar
19 var PRICE = 18; // digit height of prices
20 var DIGIT_GAP = 0.08; // space between digits, a share of their height
21 var BUBBLE_FLIP = 400; // ms between the two drawings of the chosen shop item
22 var PULSE = 1.2; // seconds of one pulse of a screen button
23 var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button
16 var INK = "#3a3530"; 24 var INK = "#3a3530";
17 var PAPER = "#f6f1e6"; 25 var PAPER = "#f6f1e6";
18 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif"; 26 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
19 var TOWERS = ["t0", "t1", "t2", "t3"]; 27 var TOWERS = ["t0", "t1", "t2", "t3"];
28 // each screen drawing, its separate button image and the button's action
29 var SCREENS = {
30 splash: { image: "splash_play", id: "play" },
31 map_won: { image: "map_won_button", id: "next" },
32 game_lost: { image: "game_lost_button", id: "retry" },
33 game_won: { image: "game_won_button", id: "restart" }
34 };
35
36 // digit variants from the sprite table: character -> sprite names
37 var DIGITS = {};
38 Object.keys(cfg.sprites).forEach(function (n) {
39 var m = /^digit_(\d|slash)_\d+$/.exec(n);
40 if (!m) return;
41 var ch = m[1] === "slash" ? "/" : m[1];
42 (DIGITS[ch] = DIGITS[ch] || []).push(n);
43 });
44
45 // all screens share one scale, set by the largest drawing, so they keep
46 // the sizes of the drawings relative to each other
47 function screenScale(w, h) {
48 var mw = 0, mh = 0;
49 Object.keys(SCREENS).forEach(function (n) {
50 mw = Math.max(mw, cfg.sprites[n][0]);
51 mh = Math.max(mh, cfg.sprites[n][1]);
52 });
53 return Math.min(1, 0.92 * w / mw, 0.9 * h / mh);
54 }
20 55
21 // positions of everything on screen for a window of w x h css pixels 56 // positions of everything on screen for a window of w x h css pixels
22 function layout(w, h, map) { 57 function layout(w, h, map) {
23 var land = w >= h; 58 var land = w >= h;
24 var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP } 59 var area = land ? { x: 0, y: TOP, w: w - SHOP, h: h - TOP }
28 w: w, h: h, land: land, area: area, scale: scale, 63 w: w, h: h, land: land, area: area, scale: scale,
29 ox: area.x + (area.w - map.width * scale) / 2, 64 ox: area.x + (area.w - map.width * scale) / 2,
30 oy: area.y + (area.h - map.height * scale) / 2, 65 oy: area.y + (area.h - map.height * scale) / 2,
31 shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP } 66 shop: land ? { x: w - SHOP, y: TOP, w: SHOP, h: h - TOP }
32 : { x: 0, y: h - SHOP, w: w, h: SHOP }, 67 : { x: 0, y: h - SHOP, w: w, h: SHOP },
33 coinIcon: { x: 26, y: TOP / 2, size: 28 } 68 coinIcon: { x: 24, y: TOP / 2, size: 32 },
69 screen: screenScale(w, h)
34 }; 70 };
35 return L; 71 return L;
36 } 72 }
37 73
38 // upgrade and sell buttons above the selected tower, or below it when 74 // the screen shown over the map, or null during play
39 // there is no room above, kept inside the map area. no upgrade button 75 function screenName(game) {
40 // at the top level. 76 if (game.splash) return "splash";
77 if (game.result === "lost") return "game_lost";
78 if (game.result) return game.lastMap() ? "game_won" : "map_won";
79 return null;
80 }
81
82 // the drawing of a screen centred in the window, and its button
83 function screenRects(name, L) {
84 var k = L.screen, s = cfg.sprites[name];
85 var bs = cfg.sprites[SCREENS[name].image], at = cfg.art.screen[name];
86 var p = { x: (L.w - s[0] * k) / 2, y: (L.h - s[1] * k) / 2, w: s[0] * k, h: s[1] * k };
87 return { panel: p, button: { x: p.x + at[0] * k, y: p.y + at[1] * k, w: bs[0] * k, h: bs[1] * k } };
88 }
89
90 // the tower menu: a bubble above the selected tower, or below it when
91 // there is no room above, kept inside the map area. upgrade and sell each
92 // take half of the bubble; at the top level there is no upgrade and sell
93 // takes all of it. cx is where the button's drawing is centred.
41 function towerMenu(game, L) { 94 function towerMenu(game, L) {
42 var t = game.selected, a = L.area, gap = 8, out = []; 95 var t = game.selected, a = L.area, gap = 8;
96 var s = cfg.sprites.tower_popup, w = POPUP_W, h = POPUP_W * s[1] / s[0];
43 var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale; 97 var x = L.ox + t.x * L.scale, y = L.oy + t.y * L.scale;
44 var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap; 98 var half = A.size(MM.tower.sprite(t), cfg.scale.sprite)[1] / 2 * L.scale + gap;
45 var w = 2 * MENU_BUTTON + gap; 99 var top = y - half - h;
46 var top = y - half - MENU_BUTTON;
47 if (top < a.y + gap) top = y + half; 100 if (top < a.y + gap) top = y + half;
48 var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap); 101 var left = MM.util.clamp(x - w / 2, a.x + gap, a.x + a.w - w - gap);
49 if (MM.tower.upgradeCost(t)) { 102 var bubble = { x: left, y: top, w: w, h: h };
50 out.push({ id: "upgrade", x: left, y: top, w: MENU_BUTTON, h: MENU_BUTTON }); 103 if (!MM.tower.upgradeCost(t)) {
51 } 104 return [{ id: "sell", x: left, y: top, w: w, h: h, cx: left + w / 2, bubble: bubble }];
52 out.push({ id: "sell", x: left + MENU_BUTTON + gap, y: top, w: MENU_BUTTON, h: MENU_BUTTON }); 105 }
53 return out; 106 return [
107 { id: "upgrade", x: left, y: top, w: w / 2, h: h, cx: left + w * 0.34, bubble: bubble },
108 { id: "sell", x: left + w / 2, y: top, w: w / 2, h: h, cx: left + w * 0.66, bubble: bubble }
109 ];
54 } 110 }
55 111
56 // the buttons for the current state, each {id, x, y, w, h} 112 // the buttons for the current state, each {id, x, y, w, h}
57 function buttons(game, L) { 113 function buttons(game, L) {
58 var out = []; 114 var out = [];
59 var y = (TOP - BUTTON) / 2; 115 var name = screenName(game);
60 if (game.splash) { 116 if (name) {
61 out.push({ id: "play", x: L.w / 2 - 56, y: L.h / 2 + 10, w: 112, h: 112 }); 117 var b = screenRects(name, L).button;
118 b.id = SCREENS[name].id;
119 b.screen = name;
120 out.push(b);
62 return out; 121 return out;
63 } 122 }
64 // map lost: back to the first map. map won: on to the next map, or 123 // top bar, right to left
65 // after the last map back to the splash screen 124 var y = (TOP - BUTTON) / 2, x = L.w - 8;
66 if (game.result) { 125 ["pause", "speed", "sound"].forEach(function (id) {
67 var id = game.result === "lost" ? "retry" : game.lastMap() ? "restart" : "next"; 126 x -= BUTTON;
68 out.push({ id: id, x: L.w / 2 - 48, y: L.h / 2 + 50, w: 96, h: 96 }); 127 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
69 return out; 128 x -= 8;
70 } 129 });
71 out.push({ id: "pause", x: L.w - BUTTON - 8, y: y, w: BUTTON, h: BUTTON });
72 out.push({ id: "speed", x: L.w - 2 * BUTTON - 16, y: y, w: BUTTON, h: BUTTON });
73 if (game.waves.phase === "break") { 130 if (game.waves.phase === "break") {
74 out.push({ id: "early", x: L.w - 2 * BUTTON - 150, y: y, w: 126, h: BUTTON }); 131 var s = cfg.sprites.next_wave, w = BUTTON * s[0] / s[1];
132 out.push({ id: "early", x: x - w, y: y, w: w, h: BUTTON });
75 } 133 }
76 TOWERS.forEach(function (t, i) { 134 TOWERS.forEach(function (t, i) {
77 var pos = 10 + i * (SHOP_BUTTON + 10); 135 var b = L.land ? { x: L.shop.x + (SHOP - SHOP_W) / 2, y: L.shop.y + 10 + i * (SHOP_H + 10) }
78 var b = L.land ? { x: L.shop.x + (SHOP - SHOP_BUTTON) / 2, y: L.shop.y + pos } 136 : { x: L.shop.x + 10 + i * (SHOP_W + 10), y: L.shop.y + (SHOP - SHOP_H) / 2 };
79 : { x: L.shop.x + pos, y: L.shop.y + (SHOP - SHOP_BUTTON) / 2 };
80 b.id = "shop"; 137 b.id = "shop";
81 b.tower = t; 138 b.tower = t;
82 b.w = b.h = SHOP_BUTTON; 139 b.w = SHOP_W;
140 b.h = SHOP_H;
83 out.push(b); 141 out.push(b);
84 }); 142 });
85 if (game.selected) out = out.concat(towerMenu(game, L)); 143 if (game.selected) out = out.concat(towerMenu(game, L));
86 return out; 144 return out;
145 }
146
147 function inside(b, x, y) {
148 return x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h;
87 } 149 }
88 150
89 function hit(game, L, x, y) { 151 function hit(game, L, x, y) {
90 var list = buttons(game, L); 152 var list = buttons(game, L);
91 for (var i = 0; i < list.length; i++) { 153 for (var i = 0; i < list.length; i++) {
92 var b = list[i]; 154 if (inside(list[i], x, y)) return list[i];
93 if (x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + b.h) return b;
94 } 155 }
95 return null; 156 return null;
96 } 157 }
97 158
159 // system font text, for the debug stats only
98 function text(ctx, s, x, y, align, size) { 160 function text(ctx, s, x, y, align, size) {
99 ctx.font = size ? FONT.replace("22px", size + "px") : FONT; 161 ctx.font = size ? FONT.replace("22px", size + "px") : FONT;
100 ctx.textAlign = align || "left"; 162 ctx.textAlign = align || "left";
101 ctx.textBaseline = "middle"; 163 ctx.textBaseline = "middle";
102 ctx.lineWidth = 4; 164 ctx.lineWidth = 4;
104 ctx.strokeText(s, x, y); 166 ctx.strokeText(s, x, y);
105 ctx.fillStyle = INK; 167 ctx.fillStyle = INK;
106 ctx.fillText(s, x, y); 168 ctx.fillText(s, x, y);
107 } 169 }
108 170
109 // a sprite fitted into a box of size s, centred on x, y 171 // a sprite fitted into a box of w x h centred on x, y, keeping its shape
110 function icon(ctx, name, x, y, s) { 172 function fit(ctx, name, x, y, w, h) {
111 var img = A.img(name); 173 var s = cfg.sprites[name], k = Math.min(w / s[0], h / s[1]);
112 var k = s / Math.max(img.naturalWidth, img.naturalHeight); 174 ctx.drawImage(A.img(name), x - s[0] * k / 2, y - s[1] * k / 2, s[0] * k, s[1] * k);
113 var w = img.naturalWidth * k, h = img.naturalHeight * k; 175 }
114 ctx.drawImage(img, x - w / 2, y - h / 2, w, h); 176
115 } 177 // the digit sprites for string s. the variant of each digit depends on
116 178 // the digit and its place counted from the right, so a number keeps its
117 function heart(ctx, x, y, s, fill) { 179 // look while other places change.
118 ctx.beginPath(); 180 function glyphs(s) {
119 ctx.moveTo(x, y + s * 0.4); 181 var out = [];
120 ctx.bezierCurveTo(x - s * 0.6, y, x - s * 0.5, y - s * 0.5, x, y - s * 0.2); 182 for (var i = 0; i < s.length; i++) {
121 ctx.bezierCurveTo(x + s * 0.5, y - s * 0.5, x + s * 0.6, y, x, y + s * 0.4); 183 var list = DIGITS[s[i]];
122 ctx.fillStyle = fill; 184 if (!list) continue;
123 ctx.fill(); 185 var place = s.length - 1 - i;
124 ctx.lineWidth = 2; 186 out.push(list[(place * 5 + s.charCodeAt(i)) % list.length]);
125 ctx.strokeStyle = INK; 187 }
126 ctx.stroke(); 188 return out;
127 } 189 }
128 190
129 function triangle(ctx, x, y, s) { 191 function numberWidth(s, h) {
130 ctx.beginPath(); 192 var g = glyphs(s), w = 0;
131 ctx.moveTo(x - s * 0.35, y - s * 0.45); 193 g.forEach(function (n) { w += cfg.sprites[n][0] * h / cfg.sprites[n][1]; });
132 ctx.lineTo(x + s * 0.45, y); 194 return w + Math.max(0, g.length - 1) * h * DIGIT_GAP;
133 ctx.lineTo(x - s * 0.35, y + s * 0.45); 195 }
134 ctx.closePath(); 196
135 ctx.fill(); 197 // number s in digits of height h, vertically centred on y. align is
136 } 198 // "left", "center" or "right" of x.
137 199 function number(ctx, s, x, y, h, align) {
138 function star(ctx, x, y, r) { 200 s = String(s);
139 ctx.beginPath(); 201 var w = numberWidth(s, h);
140 for (var i = 0; i < 10; i++) { 202 if (align === "center") x -= w / 2;
141 var a = -Math.PI / 2 + i * Math.PI / 5, rr = i % 2 ? r * 0.45 : r; 203 else if (align === "right") x -= w;
142 ctx.lineTo(x + Math.cos(a) * rr, y + Math.sin(a) * rr); 204 glyphs(s).forEach(function (n) {
143 } 205 var gw = cfg.sprites[n][0] * h / cfg.sprites[n][1];
144 ctx.closePath(); 206 ctx.drawImage(A.img(n), x, y - h / 2, gw, h);
145 ctx.fillStyle = "#f2c230"; 207 x += gw + h * DIGIT_GAP;
146 ctx.fill(); 208 });
147 ctx.lineWidth = 3; 209 }
148 ctx.strokeStyle = INK; 210
149 ctx.stroke(); 211 // a price, the coin icon followed by the amount, centred on x, y
150 } 212 function price(ctx, amount, x, y) {
151 213 var s = String(amount), coin = PRICE * 0.8, gap = 3;
152 function restartIcon(ctx, x, y, r) { 214 var left = x - (coin + gap + numberWidth(s, PRICE)) / 2;
153 ctx.lineWidth = 7; 215 fit(ctx, "coin_icon", left + coin / 2, y, coin, coin);
154 ctx.strokeStyle = INK; 216 number(ctx, s, left + coin + gap, y, PRICE, "left");
155 ctx.beginPath();
156 ctx.arc(x, y, r, -Math.PI * 0.35, Math.PI * 1.45);
157 ctx.stroke();
158 var a = -Math.PI * 0.35, tx = x + Math.cos(a) * r, ty = y + Math.sin(a) * r;
159 ctx.fillStyle = INK;
160 ctx.beginPath();
161 ctx.moveTo(tx + 12, ty - 4);
162 ctx.lineTo(tx - 8, ty - 12);
163 ctx.lineTo(tx - 2, ty + 10);
164 ctx.closePath();
165 ctx.fill();
166 }
167
168 function arrowRight(ctx, x, y, s) {
169 ctx.save();
170 ctx.translate(x, y);
171 ctx.rotate(Math.PI / 2);
172 arrowUp(ctx, 0, 0, s);
173 ctx.restore();
174 }
175
176 function arrowUp(ctx, x, y, s) {
177 ctx.beginPath();
178 ctx.moveTo(x, y - s * 0.5);
179 ctx.lineTo(x + s * 0.45, y);
180 ctx.lineTo(x + s * 0.17, y);
181 ctx.lineTo(x + s * 0.17, y + s * 0.5);
182 ctx.lineTo(x - s * 0.17, y + s * 0.5);
183 ctx.lineTo(x - s * 0.17, y);
184 ctx.lineTo(x - s * 0.45, y);
185 ctx.closePath();
186 ctx.fill();
187 }
188
189 function cross(ctx, x, y, s) {
190 ctx.lineWidth = 6;
191 ctx.lineCap = "round";
192 ctx.beginPath();
193 ctx.moveTo(x - s / 2, y - s / 2);
194 ctx.lineTo(x + s / 2, y + s / 2);
195 ctx.moveTo(x + s / 2, y - s / 2);
196 ctx.lineTo(x - s / 2, y + s / 2);
197 ctx.stroke();
198 ctx.lineCap = "butt";
199 }
200
201 // a coin and an amount along the bottom of button b
202 function price(ctx, b, s) {
203 icon(ctx, "c0", b.x + 16, b.y + b.h - 13, 16);
204 text(ctx, s, b.x + 27, b.y + b.h - 12, "left", 18);
205 } 217 }
206 218
207 function box(ctx, b, fill, line) { 219 function box(ctx, b, fill, line) {
208 ctx.fillStyle = fill; 220 ctx.fillStyle = fill;
209 ctx.strokeStyle = line || INK; 221 ctx.strokeStyle = line || INK;
213 else ctx.rect(b.x, b.y, b.w, b.h); 225 else ctx.rect(b.x, b.y, b.w, b.h);
214 ctx.fill(); 226 ctx.fill();
215 ctx.stroke(); 227 ctx.stroke();
216 } 228 }
217 229
218 function drawButton(ctx, game, b) { 230 // a screen's button image over the same button in the drawing. it pulses
231 // slowly as a cue to tap; under the mouse it is drawn larger, lifted by
232 // a shadow.
233 function screenButton(ctx, b, view) {
219 var cx = b.x + b.w / 2, cy = b.y + b.h / 2; 234 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
220 if (b.id === "pause") { 235 var hover = view.hover && inside(b, view.hover.x, view.hover.y);
221 box(ctx, b, game.paused ? "#ffe9a8" : PAPER); 236 var k = hover ? 1.08 : 1 + 0.04 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / PULSE));
222 ctx.fillStyle = INK; 237 var w = b.w * k, h = b.h * k;
223 if (game.paused) { 238 ctx.save();
224 triangle(ctx, cx, cy, 26); 239 if (hover) {
240 ctx.shadowColor = "rgba(0, 0, 0, 0.35)";
241 ctx.shadowBlur = 12;
242 ctx.shadowOffsetY = 3;
243 }
244 ctx.drawImage(A.img(SCREENS[b.screen].image), cx - w / 2, cy - h / 2, w, h);
245 ctx.restore();
246 }
247
248 function drawButton(ctx, game, b, view) {
249 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
250 if (b.screen) {
251 screenButton(ctx, b, view);
252 } else if (b.id === "pause") {
253 // shows what a tap does: pause while running, play while paused
254 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
255 } else if (b.id === "speed") {
256 // debug speeds have no drawing and show their number
257 var sp = game.speed(), name = "speed_x" + sp;
258 if (cfg.sprites[name]) {
259 fit(ctx, name, cx, cy, b.w, b.h);
225 } else { 260 } else {
226 ctx.fillRect(cx - 10, cy - 12, 7, 24); 261 box(ctx, b, PAPER);
227 ctx.fillRect(cx + 3, cy - 12, 7, 24); 262 number(ctx, sp, cx, cy, DIGIT, "center");
228 } 263 }
229 } else if (b.id === "speed") { 264 } else if (b.id === "sound") {
230 // one triangle per speed step; numbers for the debug speeds 265 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h);
231 var sp = game.speed();
232 box(ctx, b, sp > 1 ? "#ffe9a8" : PAPER);
233 ctx.fillStyle = INK;
234 if (sp <= 3) {
235 for (var i = 0; i < sp; i++) triangle(ctx, cx + (i - (sp - 1) / 2) * 12, cy, 20);
236 } else {
237 text(ctx, sp + "", cx, cy, "center");
238 }
239 } else if (b.id === "early") { 266 } else if (b.id === "early") {
240 box(ctx, b, PAPER); 267 // the countdown fills the empty right part of the drawing
241 ctx.fillStyle = INK; 268 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h);
242 triangle(ctx, b.x + 20, cy, 22); 269 number(ctx, Math.ceil(game.waves.left), b.x + b.w * 0.66, cy, DIGIT, "center");
243 ctx.fillRect(b.x + 29, cy - 10, 4, 20);
244 text(ctx, Math.ceil(game.waves.left) + "", b.x + 58, cy, "center");
245 var bonus = game.waves.bonus();
246 if (bonus > 0) text(ctx, "+" + bonus, b.x + b.w - 6, cy, "right", 18);
247 } else if (b.id === "shop") { 270 } else if (b.id === "shop") {
248 var def = cfg.tower[b.tower]; 271 var def = cfg.tower[b.tower];
249 var ok = game.canAfford(def.cost); 272 // the chosen item's bubble switches between its two drawings
250 box(ctx, b, game.shop === b.tower ? "#ffe9a8" : PAPER, 273 var bubble = game.shop !== b.tower ? "shop_bubble"
251 game.shop === b.tower ? "#c08000" : INK); 274 : Math.floor(view.now / BUBBLE_FLIP) % 2 ? "shop_bubble_dark2" : "shop_bubble_dark";
252 ctx.globalAlpha = ok ? 1 : 0.35; 275 ctx.drawImage(A.img(bubble), b.x, b.y, b.w, b.h);
253 icon(ctx, b.tower, cx, b.y + 30, 46); 276 ctx.globalAlpha = game.canAfford(def.cost) ? 1 : 0.35;
254 icon(ctx, "c0", b.x + 22, b.y + b.h - 14, 16); 277 fit(ctx, b.tower, cx, b.y + 28, 44, 38);
255 text(ctx, def.cost + "", b.x + 34, b.y + b.h - 13, "left", 18); 278 price(ctx, def.cost, cx, b.y + b.h - 15);
256 ctx.globalAlpha = 1; 279 ctx.globalAlpha = 1;
257 } else if (b.id === "upgrade") { 280 } else if (b.id === "upgrade") {
258 var cost = MM.tower.upgradeCost(game.selected); 281 var cost = MM.tower.upgradeCost(game.selected);
259 box(ctx, b, PAPER);
260 ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35; 282 ctx.globalAlpha = game.canAfford(cost) ? 1 : 0.35;
261 ctx.fillStyle = INK; 283 fit(ctx, "upgrade_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46, 44);
262 arrowUp(ctx, cx, b.y + 22, 28); 284 price(ctx, cost, b.cx, b.bubble.y + b.bubble.h * 0.7);
263 price(ctx, b, cost + "");
264 ctx.globalAlpha = 1; 285 ctx.globalAlpha = 1;
265 } else if (b.id === "sell") { 286 } else if (b.id === "sell") {
266 box(ctx, b, PAPER); 287 // armed by a first tap: a fast pulse with a red glow until the
267 ctx.strokeStyle = "#c03030"; 288 // second tap sells
268 cross(ctx, cx, b.y + 22, 20); 289 var k = 1;
269 price(ctx, b, "+" + MM.tower.refund(game.selected, game.pauseNumber())); 290 ctx.save();
270 } else if (b.id === "play") { 291 if (game.sellArmed) {
271 box(ctx, b, PAPER); 292 k = 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
272 ctx.fillStyle = INK; 293 ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
273 triangle(ctx, cx + 4, cy, 60); 294 ctx.shadowBlur = 14;
274 } else if (b.id === "next") { 295 }
275 box(ctx, b, PAPER); 296 fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k);
276 ctx.fillStyle = INK; 297 ctx.restore();
277 arrowRight(ctx, cx, cy, 52); 298 price(ctx, MM.tower.refund(game.selected, game.pauseNumber()), b.cx,
278 } else if (b.id === "retry" || b.id === "restart") { 299 b.bubble.y + b.bubble.h * 0.7);
279 box(ctx, b, PAPER);
280 restartIcon(ctx, cx, cy, 26);
281 } 300 }
282 } 301 }
283 302
284 function draw(ctx, game, L, view) { 303 function draw(ctx, game, L, view) {
285 var dpr = view.dpr; 304 var dpr = view.dpr;
290 ctx.fillRect(0, 0, L.w, TOP); 309 ctx.fillRect(0, 0, L.w, TOP);
291 ctx.fillStyle = "rgba(246, 241, 230, 0.92)"; 310 ctx.fillStyle = "rgba(246, 241, 230, 0.92)";
292 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h); 311 ctx.fillRect(L.shop.x, L.shop.y, L.shop.w, L.shop.h);
293 ctx.fillStyle = "rgba(58, 53, 48, 0.25)"; 312 ctx.fillStyle = "rgba(58, 53, 48, 0.25)";
294 ctx.fillRect(0, TOP - 2, L.w, 2); 313 ctx.fillRect(0, TOP - 2, L.w, 2);
295 icon(ctx, "c0", L.coinIcon.x, L.coinIcon.y, L.coinIcon.size); 314 // each counter takes at least the width of its widest usual value, so
296 text(ctx, game.coins + "", 46, TOP / 2); 315 // the next one does not move while the numbers change
297 heart(ctx, 126, TOP / 2 + 2, 30, "#e03a3a"); 316 var ic = L.coinIcon, x = ic.x - ic.size / 2;
298 text(ctx, game.hearts + "", 146, TOP / 2); 317 [["coin_icon", game.coins, "000"], ["heart", game.hearts, "00"],
299 icon(ctx, "m0", 206, TOP / 2, 30); 318 ["monster_icon", game.waves.shown() + "/" + game.waves.list.length, "00/00"]
300 text(ctx, game.waves.shown() + "/" + game.waves.list.length, 226, TOP / 2); 319 ].forEach(function (c) {
301 320 var s = String(c[1]);
302 var screen = game.splash || game.result; 321 fit(ctx, c[0], x + ic.size / 2, TOP / 2, ic.size, ic.size);
303 if (!screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); }); 322 number(ctx, s, x + ic.size + 6, TOP / 2, DIGIT, "left");
304 323 x += ic.size + 6 + Math.max(numberWidth(s, DIGIT), numberWidth(c[2], DIGIT)) + 18;
324 });
325
326 var screen = screenName(game);
327 var list = buttons(game, L);
305 if (game.paused && !screen) { 328 if (game.paused && !screen) {
306 ctx.fillStyle = "rgba(255, 255, 255, 0.18)"; 329 ctx.fillStyle = "rgba(255, 255, 255, 0.18)";
307 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale); 330 ctx.fillRect(L.ox, L.oy, game.map.width * L.scale, game.map.height * L.scale);
308 } 331 }
309 if (game.splash) { 332 if (screen) {
310 // title and byline, placeholders until the drawings arrive 333 ctx.fillStyle = game.splash ? "rgba(246, 241, 230, 0.55)" : "rgba(40, 35, 30, 0.45)";
311 ctx.fillStyle = "rgba(246, 241, 230, 0.55)";
312 ctx.fillRect(0, 0, L.w, L.h); 334 ctx.fillRect(0, 0, L.w, L.h);
313 text(ctx, "minor monsters", L.w / 2, L.h / 2 - 90, "center", 60); 335 var p = screenRects(screen, L).panel;
314 text(ctx, "By M.A. and J.A. Juszczyk", L.w / 2, L.h / 2 - 38, "center", 24); 336 ctx.drawImage(A.img(screen), p.x, p.y, p.w, p.h);
315 } else if (game.result) { 337 } else if (game.selected) {
316 ctx.fillStyle = "rgba(40, 35, 30, 0.45)"; 338 var bub = list[list.length - 1].bubble;
317 ctx.fillRect(0, 0, L.w, L.h); 339 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h);
318 if (game.result === "won" && game.lastMap()) { 340 }
319 // game won: three stars 341 list.forEach(function (b) { drawButton(ctx, game, b, view); });
320 star(ctx, L.w / 2 - 125, L.h / 2 - 15, 50);
321 star(ctx, L.w / 2 + 125, L.h / 2 - 15, 50);
322 star(ctx, L.w / 2, L.h / 2 - 45, 70);
323 } else if (game.result === "won") {
324 star(ctx, L.w / 2, L.h / 2 - 40, 70);
325 } else {
326 heart(ctx, L.w / 2, L.h / 2 - 30, 140, "#8a8a8a");
327 ctx.strokeStyle = INK;
328 ctx.lineWidth = 5;
329 ctx.beginPath();
330 ctx.moveTo(L.w / 2 - 6, L.h / 2 - 72);
331 ctx.lineTo(L.w / 2 + 10, L.h / 2 - 40);
332 ctx.lineTo(L.w / 2 - 10, L.h / 2 - 20);
333 ctx.lineTo(L.w / 2 + 4, L.h / 2 + 22);
334 ctx.stroke();
335 }
336 }
337 if (screen) buttons(game, L).forEach(function (b) { drawButton(ctx, game, b); });
338 342
339 if (cfg.debug.show_stats && view.stats) { 343 if (cfg.debug.show_stats && view.stats) {
340 var st = view.stats; 344 var st = view.stats;
341 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " + 345 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " +
342 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed, 346 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed,