Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

test(examples): the audio demo authors a clip, instead of naming one in code

esengine248d6c9b4fb7
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 511d11e5d..6f4b1f4ed 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -36,6 +36,12 @@ published separately; it ships inside the editor.
### Changed
+- **The audio demo authors its beat's clip on an `AudioSource`.** Which sound the
+ beat is now comes from the scene rather than a path in code, so the asset system
+ brings the clip in with the scene the way a game's own audio arrives. No example
+ or template scene used `AudioSource` at all — the engine's own audio component had
+ no coverage in the shipped corpus, which is the door #59's silence came in by.
+
- **The hot-update demo now ships a real 分包.** Its two groups take the two modes
their flows need: `cdn` stays remote, because a hot update has to replace it after
the game shipped, and `pack` — the DLC tile strip the *下载资源包* button pulls on
diff --git a/examples/audio-demo/assets/scenes/main.esscene b/examples/audio-demo/assets/scenes/main.esscene
index dd753e7ba..fac8b16e1 100644
--- a/examples/audio-demo/assets/scenes/main.esscene
+++ b/examples/audio-demo/assets/scenes/main.esscene
@@ -108,7 +108,8 @@
{ "type": "UINode", "data": { "position": 1, "width": { "value": 170, "unit": 0 }, "height": { "value": 44, "unit": 0 }, "insetLeft": { "value": 50, "unit": 1 }, "marginLeft": { "value": -85, "unit": 0 }, "insetTop": { "value": 50, "unit": 1 }, "marginTop": { "value": -74, "unit": 0 } } },
{ "type": "UIVisual", "data": { "visualType": 1, "color": { "r": 0.55, "g": 0.35, "b": 0.85, "a": 1 } } },
{ "type": "Interactable", "data": {} },
- { "type": "BeatToggle", "data": {} }
+ { "type": "BeatToggle", "data": {} },
+ { "type": "AudioSource", "data": { "clip": "@uuid:a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d", "bus": "music", "volume": 0.9, "loop": true, "playOnAwake": false } }
]
},
{
diff --git a/examples/audio-demo/src/config.ts b/examples/audio-demo/src/config.ts
index 3883f89bf..cd783f446 100644
--- a/examples/audio-demo/src/config.ts
+++ b/examples/audio-demo/src/config.ts
@@ -6,13 +6,10 @@ export const PADS = [
{ name: 'Clap', url: 'assets/audio/clap.wav' },
];
-// The looping beat runs on the music bus. There's no dedicated music track in
-// this example, so a kick loop stands in — enough to show playBGM's loop + fade
-// + music-bus routing.
-export const BEAT_URL = 'assets/audio/kick.wav';
-
-// Everything to preload up front so the first hit is latency-free.
-export const ALL_URLS = [...PADS.map((p) => p.url), BEAT_URL];
+// Everything to preload up front so the first hit is latency-free. The beat's
+// clip is not here: it is authored on the BeatBtn's AudioSource, so the scene
+// brings it in and `beatSystem` reads which sound it is off the component.
+export const ALL_URLS = PADS.map((p) => p.url);
// Bus volumes cycle through these steps on each click.
export const VOLUME_STEPS = [1, 0.66, 0.33, 0];
diff --git a/examples/audio-demo/src/systems/beat.ts b/examples/audio-demo/src/systems/beat.ts
index 946f438ca..31bfba16d 100644
--- a/examples/audio-demo/src/systems/beat.ts
+++ b/examples/audio-demo/src/systems/beat.ts
@@ -1,24 +1,26 @@
import {
- defineSystem, Query, Mut, Res, UIEvents, Audio, Text,
+ defineSystem, Query, Mut, Res, UIEvents, Audio, Text, AudioSource,
} from 'esengine';
-import type { UIEventQueue, AudioAPI } from 'esengine';
+import type { UIEventQueue, AudioAPI, AudioSourceData } from 'esengine';
import { BeatToggle, BeatLabel } from '../components';
-import { BEAT_URL } from '../config';
let beatOn = false;
// Toggle the looping beat on the music bus, fading in/out, and reflect the state
-// in the label.
+// in the label. Which sound it is comes from the button's AudioSource, not from
+// code: the scene names the clip, so the asset system brings it in with it.
export const beatSystem = defineSystem(
- [Res(UIEvents), Res(Audio), Query(BeatToggle), Query(Mut(Text), BeatLabel)],
+ [Res(UIEvents), Res(Audio), Query(AudioSource, BeatToggle), Query(Mut(Text), BeatLabel)],
(events: UIEventQueue, audio: AudioAPI, toggles, labels) => {
const clicks = events.query('click');
- let toggled = false;
- for (const [entity] of toggles) if (clicks.some((c) => c.target === entity)) toggled = true;
- if (!toggled) return;
+ let clip: string | null = null;
+ for (const [entity, source] of toggles) {
+ if (clicks.some((c) => c.target === entity)) clip = (source as AudioSourceData).clip;
+ }
+ if (clip === null) return;
beatOn = !beatOn;
- if (beatOn) audio.playBGM(BEAT_URL, { volume: 0.9, fadeIn: 0.3 });
+ if (beatOn) audio.playBGM(clip, { volume: 0.9, fadeIn: 0.3 });
else audio.stopBGM(0.3);
for (const [, text] of labels) text.content = beatOn ? 'Beat: On' : 'Beat: Off';
},