comparison game/js/ui.js @ 32:85d23328a215 0.10.0

added intro music, logic for browser audio triggering on splash screen
author ark
date 2026-10-20 18:39 +0200
parents 75deda08db90
children
comparison
equal deleted inserted replaced
31:d1ae50439686 32:85d23328a215
167 167
168 // the buttons for the current state, each {id, x, y, w, h} 168 // the buttons for the current state, each {id, x, y, w, h}
169 function buttons(game, L) { 169 function buttons(game, L) {
170 var out = []; 170 var out = [];
171 var name = screenName(game); 171 var name = screenName(game);
172 if (name) return screenRects(name, L).buttons; 172 if (name) {
173 out = screenRects(name, L).buttons;
174 // the sound button in place of the play button, until sound starts
175 if (name === "splash" && game.askSound) {
176 var p = out[0];
177 out[0] = { id: "start_sound", x: p.x, y: p.y, w: p.w, h: p.h };
178 }
179 return out;
180 }
173 // top bar, right to left 181 // top bar, right to left
174 var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8; 182 var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8;
175 ["help", "pause", "speed", "sound"].forEach(function (id) { 183 ["help", "pause", "speed", "sound"].forEach(function (id) {
176 x -= BUTTON; 184 x -= BUTTON;
177 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON }); 185 out.push({ id: id, x: x, y: y, w: BUTTON, h: BUTTON });
270 } 278 }
271 ctx.drawImage(A.img(b.image), cx - w / 2, cy - h / 2, w, h); 279 ctx.drawImage(A.img(b.image), cx - w / 2, cy - h / 2, w, h);
272 ctx.restore(); 280 ctx.restore();
273 } 281 }
274 282
283 // an armed button pulses fast with a red glow. sets the glow on ctx and
284 // returns the size factor of the pulse
285 function armed(ctx, view) {
286 ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
287 ctx.shadowBlur = 14;
288 return 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
289 }
290
275 function drawButton(ctx, game, b, view) { 291 function drawButton(ctx, game, b, view) {
276 var cx = b.x + b.w / 2, cy = b.y + b.h / 2; 292 var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
277 if (b.screen) { 293 if (b.screen) {
278 screenButton(ctx, b, view); 294 screenButton(ctx, b, view);
279 } else if (b.id === "help") { 295 } else if (b.id === "help") {
281 } else if (b.id === "pause") { 297 } else if (b.id === "pause") {
282 // shows what a tap does: pause while running, play while paused 298 // shows what a tap does: pause while running, play while paused
283 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h); 299 fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
284 } else if (b.id === "speed") { 300 } else if (b.id === "speed") {
285 fit(ctx, "speed_x" + game.speed(), cx, cy, b.w, b.h); 301 fit(ctx, "speed_x" + game.speed(), cx, cy, b.w, b.h);
302 } else if (b.id === "start_sound") {
303 // covers the play button of the splash drawing with paper
304 ctx.fillStyle = "#fff";
305 ctx.fillRect(b.x, b.y, b.w, b.h);
306 ctx.save();
307 var a = armed(ctx, view);
308 fit(ctx, "sound_on", cx, cy, b.w * a, b.h * a);
309 ctx.restore();
286 } else if (b.id === "sound") { 310 } else if (b.id === "sound") {
287 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h); 311 fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h);
288 } else if (b.id === "early") { 312 } else if (b.id === "early") {
289 // the countdown fills the empty right part of the drawing 313 // the countdown fills the empty right part of the drawing
290 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h); 314 ctx.drawImage(A.img("next_wave"), b.x, b.y, b.w, b.h);
306 price(ctx, cost, b.cx, b.bubble.y + b.bubble.h * 0.7); 330 price(ctx, cost, b.cx, b.bubble.y + b.bubble.h * 0.7);
307 ctx.globalAlpha = 1; 331 ctx.globalAlpha = 1;
308 } else if (b.id === "sell") { 332 } else if (b.id === "sell") {
309 // armed by a first tap: a fast pulse with a red glow until the 333 // armed by a first tap: a fast pulse with a red glow until the
310 // second tap sells 334 // second tap sells
311 var k = 1;
312 ctx.save(); 335 ctx.save();
313 if (game.sellArmed) { 336 var k = game.sellArmed ? armed(ctx, view) : 1;
314 k = 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
315 ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
316 ctx.shadowBlur = 14;
317 }
318 fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k); 337 fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k);
319 ctx.restore(); 338 ctx.restore();
320 price(ctx, MM.tower.refund(game.selected), b.cx, 339 price(ctx, MM.tower.refund(game.selected), b.cx,
321 b.bubble.y + b.bubble.h * 0.7); 340 b.bubble.y + b.bubble.h * 0.7);
322 } 341 }