comparison game/js/ui.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 85d23328a215
comparison
equal deleted inserted replaced
26:28e1fc486be5 27:75deda08db90
19 var PRICE = 18; // digit height of prices 19 var PRICE = 18; // digit height of prices
20 var DIGIT_GAP = 0.08; // space between digits, a share of their height 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 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 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 23 var WARN_PULSE = 0.5; // seconds of one pulse of an armed sell button
24 var INK = "#3a3530";
25 var PAPER = "#f6f1e6";
26 var FONT = "bold 22px system-ui, -apple-system, Segoe UI, sans-serif";
27 var TOWERS = ["t0", "t1", "t2", "t3"]; 24 var TOWERS = ["t0", "t1", "t2", "t3"];
28 // widest usual values of the coin, heart and wave counters 25 // widest usual values of the coin, heart and wave counters
29 var USUAL = ["000", "00", "00/00"]; 26 var USUAL = ["000", "00", "00/00"];
30 // each screen drawing, its separate button image and the button's action 27 // each screen drawing, its separate button image and the button's action
31 var SCREENS = { 28 var SCREENS = {
207 if (inside(list[i], x, y)) return list[i]; 204 if (inside(list[i], x, y)) return list[i];
208 } 205 }
209 return null; 206 return null;
210 } 207 }
211 208
212 // system font text, for the debug stats only
213 function text(ctx, s, x, y, align, size) {
214 ctx.font = size ? FONT.replace("22px", size + "px") : FONT;
215 ctx.textAlign = align || "left";
216 ctx.textBaseline = "middle";
217 ctx.lineWidth = 4;
218 ctx.strokeStyle = PAPER;
219 ctx.strokeText(s, x, y);
220 ctx.fillStyle = INK;
221 ctx.fillText(s, x, y);
222 }
223
224 // a sprite fitted into a box of w x h centred on x, y, keeping its shape 209 // a sprite fitted into a box of w x h centred on x, y, keeping its shape
225 function fit(ctx, name, x, y, w, h) { 210 function fit(ctx, name, x, y, w, h) {
226 var s = cfg.sprites[name], k = Math.min(w / s[0], h / s[1]); 211 var s = cfg.sprites[name], k = Math.min(w / s[0], h / s[1]);
227 ctx.drawImage(A.img(name), x - s[0] * k / 2, y - s[1] * k / 2, s[0] * k, s[1] * k); 212 ctx.drawImage(A.img(name), x - s[0] * k / 2, y - s[1] * k / 2, s[0] * k, s[1] * k);
228 } 213 }
265 function price(ctx, amount, x, y) { 250 function price(ctx, amount, x, y) {
266 var s = String(amount), coin = PRICE * 0.8, gap = 3; 251 var s = String(amount), coin = PRICE * 0.8, gap = 3;
267 var left = x - (coin + gap + numberWidth(s, PRICE)) / 2; 252 var left = x - (coin + gap + numberWidth(s, PRICE)) / 2;
268 fit(ctx, "coin_icon", left + coin / 2, y, coin, coin); 253 fit(ctx, "coin_icon", left + coin / 2, y, coin, coin);
269 number(ctx, s, left + coin + gap, y, PRICE, "left"); 254 number(ctx, s, left + coin + gap, y, PRICE, "left");
270 }
271
272 function box(ctx, b, fill, line) {
273 ctx.fillStyle = fill;
274 ctx.strokeStyle = line || INK;
275 ctx.lineWidth = 2;
276 ctx.beginPath();
277 if (ctx.roundRect) ctx.roundRect(b.x, b.y, b.w, b.h, 10);
278 else ctx.rect(b.x, b.y, b.w, b.h);
279 ctx.fill();
280 ctx.stroke();
281 } 255 }
282 256
283 // a screen's button image over the same button in the drawing. it pulses 257 // a screen's button image over the same button in the drawing. it pulses
284 // slowly as a cue to tap; under the mouse it is drawn larger, lifted by 258 // slowly as a cue to tap; under the mouse it is drawn larger, lifted by
285 // a shadow. 259 // a shadow.
306 fit(ctx, "help_button", cx, cy, b.w, b.h); 280 fit(ctx, "help_button", cx, cy, b.w, b.h);
307 } else if (b.id === "pause") { 281 } else if (b.id === "pause") {
308 // shows what a tap does: pause while running, play while paused 282 // shows what a tap does: pause while running, play while paused
309 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h); 283 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
310 } else if (b.id === "speed") { 284 } else if (b.id === "speed") {
311 // debug speeds have no drawing and show their number 285 fit(ctx, "speed_x" + game.speed(), cx, cy, b.w, b.h);
312 var sp = game.speed(), name = "speed_x" + sp;
313 if (cfg.sprites[name]) {
314 fit(ctx, name, cx, cy, b.w, b.h);
315 } else {
316 box(ctx, b, PAPER);
317 number(ctx, sp, cx, cy, DIGIT, "center");
318 }
319 } else if (b.id === "sound") { 286 } else if (b.id === "sound") {
320 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h); 287 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h);
321 } else if (b.id === "early") { 288 } else if (b.id === "early") {
322 // the countdown fills the empty right part of the drawing 289 // the countdown fills the empty right part of the drawing
323 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h); 290 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h);
395 } else if (game.selected) { 362 } else if (game.selected) {
396 var bub = list[list.length - 1].bubble; 363 var bub = list[list.length - 1].bubble;
397 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h); 364 ctx.drawImage(A.img("tower_popup"), bub.x, bub.y, bub.w, bub.h);
398 } 365 }
399 list.forEach(function (b) { drawButton(ctx, game, b, view); }); 366 list.forEach(function (b) { drawButton(ctx, game, b, view); });
400
401 if (cfg.debug.show_stats && view.stats) {
402 var st = view.stats;
403 text(ctx, st.ms.toFixed(1) + " ms " + game.monsters.length + " m " +
404 game.towers.length + " t " + game.projectiles.length + " p seed " + game.seed,
405 8, L.h - 14, "left", 14);
406 }
407 } 367 }
408 368
409 return { layout: layout, hit: hit, draw: draw, BUBBLE_FLIP: BUBBLE_FLIP }; 369 return { layout: layout, hit: hit, draw: draw, BUBBLE_FLIP: BUBBLE_FLIP };
410 })(); 370 })();