diff 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
line wrap: on
line diff
--- a/game/js/ui.js	Tue Oct 20 17:58:10 2026 +0200
+++ b/game/js/ui.js	Tue Oct 20 18:39:22 2026 +0200
@@ -169,7 +169,15 @@
     function buttons(game, L) {
         var out = [];
         var name = screenName(game);
-        if (name) return screenRects(name, L).buttons;
+        if (name) {
+            out = screenRects(name, L).buttons;
+            // the sound button in place of the play button, until sound starts
+            if (name === "splash" && game.askSound) {
+                var p = out[0];
+                out[0] = { id: "start_sound", x: p.x, y: p.y, w: p.w, h: p.h };
+            }
+            return out;
+        }
         // top bar, right to left
         var y = L.buttonRow + (TOP - BUTTON) / 2, x = L.w - 8;
         ["help", "pause", "speed", "sound"].forEach(function (id) {
@@ -272,6 +280,14 @@
         ctx.restore();
     }
 
+    // an armed button pulses fast with a red glow. sets the glow on ctx and
+    // returns the size factor of the pulse
+    function armed(ctx, view) {
+        ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
+        ctx.shadowBlur = 14;
+        return 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
+    }
+
     function drawButton(ctx, game, b, view) {
         var cx = b.x + b.w / 2, cy = b.y + b.h / 2;
         if (b.screen) {
@@ -283,6 +299,14 @@
             fit(ctx, game.paused ? "play_button" : "pause_button", cx, cy, b.w, b.h);
         } else if (b.id === "speed") {
             fit(ctx, "speed_x" + game.speed(), cx, cy, b.w, b.h);
+        } else if (b.id === "start_sound") {
+            // covers the play button of the splash drawing with paper
+            ctx.fillStyle = "#fff";
+            ctx.fillRect(b.x, b.y, b.w, b.h);
+            ctx.save();
+            var a = armed(ctx, view);
+            fit(ctx, "sound_on", cx, cy, b.w * a, b.h * a);
+            ctx.restore();
         } else if (b.id === "sound") {
             fit(ctx, MM.audio.enabled() ? "sound_on" : "sound_off", cx, cy, b.w, b.h);
         } else if (b.id === "early") {
@@ -308,13 +332,8 @@
         } else if (b.id === "sell") {
             // armed by a first tap: a fast pulse with a red glow until the
             // second tap sells
-            var k = 1;
             ctx.save();
-            if (game.sellArmed) {
-                k = 1 + 0.15 * (0.5 - 0.5 * Math.cos(2 * Math.PI * view.now / 1000 / WARN_PULSE));
-                ctx.shadowColor = "rgba(220, 30, 20, 0.9)";
-                ctx.shadowBlur = 14;
-            }
+            var k = game.sellArmed ? armed(ctx, view) : 1;
             fit(ctx, "sell_button", b.cx, b.bubble.y + b.bubble.h * 0.4, 46 * k, 44 * k);
             ctx.restore();
             price(ctx, MM.tower.refund(game.selected), b.cx,