@almadar/ui 5.76.2 → 5.76.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -12100,14 +12100,318 @@ var init_useCanvasEffects = __esm({
12100
12100
  "use client";
12101
12101
  }
12102
12102
  });
12103
+ function pickPath(entry) {
12104
+ if (Array.isArray(entry.path)) {
12105
+ return entry.path[Math.floor(Math.random() * entry.path.length)];
12106
+ }
12107
+ return entry.path;
12108
+ }
12109
+ function useGameAudio({
12110
+ manifest,
12111
+ baseUrl = "",
12112
+ initialMuted = false,
12113
+ initialVolume = 1
12114
+ }) {
12115
+ const [muted, setMutedState] = useState(initialMuted);
12116
+ const [masterVolume, setMasterVolumeState] = useState(initialVolume);
12117
+ const mutedRef = useRef(muted);
12118
+ const volumeRef = useRef(masterVolume);
12119
+ const manifestRef = useRef(manifest);
12120
+ mutedRef.current = muted;
12121
+ volumeRef.current = masterVolume;
12122
+ manifestRef.current = manifest;
12123
+ const poolsRef = useRef(/* @__PURE__ */ new Map());
12124
+ const getOrCreateElement = useCallback((key) => {
12125
+ const entry = manifestRef.current[key];
12126
+ if (!entry) return null;
12127
+ let pool = poolsRef.current.get(key);
12128
+ if (!pool) {
12129
+ pool = [];
12130
+ poolsRef.current.set(key, pool);
12131
+ }
12132
+ const maxSize = entry.poolSize ?? 1;
12133
+ for (const audio of pool) {
12134
+ if (audio.paused && (audio.ended || audio.currentTime === 0)) {
12135
+ return audio;
12136
+ }
12137
+ }
12138
+ if (pool.length < maxSize) {
12139
+ const src = baseUrl + pickPath(entry);
12140
+ const audio = new Audio(src);
12141
+ audio.loop = entry.loop ?? false;
12142
+ pool.push(audio);
12143
+ return audio;
12144
+ }
12145
+ if (!entry.loop) {
12146
+ let oldest = pool[0];
12147
+ for (const audio of pool) {
12148
+ if (audio.currentTime > oldest.currentTime) {
12149
+ oldest = audio;
12150
+ }
12151
+ }
12152
+ oldest.pause();
12153
+ oldest.currentTime = 0;
12154
+ return oldest;
12155
+ }
12156
+ return null;
12157
+ }, [baseUrl]);
12158
+ const play = useCallback((key) => {
12159
+ if (mutedRef.current) return;
12160
+ const entry = manifestRef.current[key];
12161
+ if (!entry) return;
12162
+ const audio = getOrCreateElement(key);
12163
+ if (!audio) return;
12164
+ audio.volume = Math.min(1, (entry.volume ?? 1) * volumeRef.current);
12165
+ if (!entry.loop) {
12166
+ audio.currentTime = 0;
12167
+ }
12168
+ const promise = audio.play();
12169
+ if (promise) {
12170
+ promise.catch(() => {
12171
+ });
12172
+ }
12173
+ }, [getOrCreateElement]);
12174
+ const stop = useCallback((key) => {
12175
+ const pool = poolsRef.current.get(key);
12176
+ if (!pool) return;
12177
+ for (const audio of pool) {
12178
+ audio.pause();
12179
+ audio.currentTime = 0;
12180
+ }
12181
+ }, []);
12182
+ const currentMusicKeyRef = useRef(null);
12183
+ const currentMusicElRef = useRef(null);
12184
+ const musicFadeRef = useRef(null);
12185
+ const pendingMusicKeyRef = useRef(null);
12186
+ const clearMusicFade = useCallback(() => {
12187
+ if (musicFadeRef.current) {
12188
+ clearInterval(musicFadeRef.current);
12189
+ musicFadeRef.current = null;
12190
+ }
12191
+ }, []);
12192
+ const playMusic = useCallback((key) => {
12193
+ if (key === currentMusicKeyRef.current) return;
12194
+ pendingMusicKeyRef.current = key;
12195
+ const entry = manifestRef.current[key];
12196
+ if (!entry) return;
12197
+ const fadeDurationMs = entry.crossfadeDurationMs ?? 1500;
12198
+ const stepMs = 50;
12199
+ const totalSteps = Math.max(1, fadeDurationMs / stepMs);
12200
+ const targetVolume = Math.min(1, (entry.volume ?? 1) * volumeRef.current);
12201
+ clearMusicFade();
12202
+ const src = baseUrl + (Array.isArray(entry.path) ? entry.path[0] : entry.path);
12203
+ const incoming = new Audio(src);
12204
+ incoming.loop = true;
12205
+ incoming.volume = 0;
12206
+ const outgoing = currentMusicElRef.current;
12207
+ const outgoingStartVol = outgoing?.volume ?? 0;
12208
+ currentMusicKeyRef.current = key;
12209
+ currentMusicElRef.current = incoming;
12210
+ if (!mutedRef.current) {
12211
+ incoming.play().catch(() => {
12212
+ currentMusicKeyRef.current = null;
12213
+ currentMusicElRef.current = outgoing;
12214
+ });
12215
+ }
12216
+ let step = 0;
12217
+ musicFadeRef.current = setInterval(() => {
12218
+ step++;
12219
+ const progress = Math.min(step / totalSteps, 1);
12220
+ incoming.volume = Math.min(1, targetVolume * progress);
12221
+ if (outgoing) {
12222
+ outgoing.volume = Math.max(0, outgoingStartVol * (1 - progress));
12223
+ }
12224
+ if (progress >= 1) {
12225
+ clearMusicFade();
12226
+ if (outgoing) {
12227
+ outgoing.pause();
12228
+ outgoing.src = "";
12229
+ }
12230
+ }
12231
+ }, stepMs);
12232
+ }, [baseUrl, clearMusicFade]);
12233
+ const stopMusic = useCallback((fadeDurationMs = 1e3) => {
12234
+ const outgoing = currentMusicElRef.current;
12235
+ if (!outgoing) return;
12236
+ currentMusicKeyRef.current = null;
12237
+ currentMusicElRef.current = null;
12238
+ pendingMusicKeyRef.current = null;
12239
+ clearMusicFade();
12240
+ const startVolume = outgoing.volume;
12241
+ const stepMs = 50;
12242
+ const totalSteps = Math.max(1, fadeDurationMs / stepMs);
12243
+ let step = 0;
12244
+ musicFadeRef.current = setInterval(() => {
12245
+ step++;
12246
+ const progress = step / totalSteps;
12247
+ outgoing.volume = Math.max(0, startVolume * (1 - progress));
12248
+ if (progress >= 1) {
12249
+ clearMusicFade();
12250
+ outgoing.pause();
12251
+ outgoing.src = "";
12252
+ }
12253
+ }, stepMs);
12254
+ }, [clearMusicFade]);
12255
+ const stopAll = useCallback(() => {
12256
+ for (const pool of poolsRef.current.values()) {
12257
+ for (const audio of pool) {
12258
+ audio.pause();
12259
+ audio.currentTime = 0;
12260
+ }
12261
+ }
12262
+ stopMusic(0);
12263
+ }, [stopMusic]);
12264
+ const setMuted = useCallback((value) => {
12265
+ setMutedState(value);
12266
+ if (value) {
12267
+ for (const [key, pool] of poolsRef.current.entries()) {
12268
+ if (manifestRef.current[key]?.loop) {
12269
+ for (const audio of pool) {
12270
+ if (!audio.paused) audio.pause();
12271
+ }
12272
+ }
12273
+ }
12274
+ currentMusicElRef.current?.pause();
12275
+ } else {
12276
+ for (const [key, pool] of poolsRef.current.entries()) {
12277
+ const entry = manifestRef.current[key];
12278
+ if (entry?.loop && entry?.autostart) {
12279
+ for (const audio of pool) {
12280
+ if (audio.paused) audio.play().catch(() => {
12281
+ });
12282
+ }
12283
+ }
12284
+ }
12285
+ const musicEl = currentMusicElRef.current;
12286
+ if (musicEl) {
12287
+ musicEl.play().catch(() => {
12288
+ });
12289
+ }
12290
+ }
12291
+ }, []);
12292
+ const setMasterVolume = useCallback((volume) => {
12293
+ const clamped = Math.max(0, Math.min(1, volume));
12294
+ setMasterVolumeState(clamped);
12295
+ for (const [key, pool] of poolsRef.current.entries()) {
12296
+ const entryVol = manifestRef.current[key]?.volume ?? 1;
12297
+ for (const audio of pool) {
12298
+ audio.volume = Math.min(1, entryVol * clamped);
12299
+ }
12300
+ }
12301
+ if (!musicFadeRef.current && currentMusicElRef.current) {
12302
+ const key = currentMusicKeyRef.current;
12303
+ const entryVol = key ? manifestRef.current[key]?.volume ?? 1 : 1;
12304
+ currentMusicElRef.current.volume = Math.min(1, entryVol * clamped);
12305
+ }
12306
+ }, []);
12307
+ const unlockedRef = useRef(false);
12308
+ useEffect(() => {
12309
+ const autoKeys = Object.keys(manifest).filter((k) => manifest[k].autostart);
12310
+ const hasPendingMusic = () => pendingMusicKeyRef.current !== null;
12311
+ const hasAutoStart = autoKeys.length > 0;
12312
+ if (!hasAutoStart && !hasPendingMusic()) return;
12313
+ const unlock = () => {
12314
+ if (unlockedRef.current) return;
12315
+ unlockedRef.current = true;
12316
+ if (!mutedRef.current) {
12317
+ for (const key of autoKeys) {
12318
+ play(key);
12319
+ }
12320
+ const pending = pendingMusicKeyRef.current;
12321
+ if (pending && pending !== currentMusicKeyRef.current) {
12322
+ playMusic(pending);
12323
+ }
12324
+ }
12325
+ };
12326
+ document.addEventListener("click", unlock, { once: true });
12327
+ document.addEventListener("keydown", unlock, { once: true });
12328
+ document.addEventListener("touchstart", unlock, { once: true });
12329
+ return () => {
12330
+ document.removeEventListener("click", unlock);
12331
+ document.removeEventListener("keydown", unlock);
12332
+ document.removeEventListener("touchstart", unlock);
12333
+ };
12334
+ }, [manifest, play, playMusic]);
12335
+ useEffect(() => {
12336
+ return () => {
12337
+ clearMusicFade();
12338
+ for (const pool of poolsRef.current.values()) {
12339
+ for (const audio of pool) {
12340
+ audio.pause();
12341
+ audio.src = "";
12342
+ }
12343
+ }
12344
+ poolsRef.current.clear();
12345
+ if (currentMusicElRef.current) {
12346
+ currentMusicElRef.current.pause();
12347
+ currentMusicElRef.current.src = "";
12348
+ currentMusicElRef.current = null;
12349
+ }
12350
+ };
12351
+ }, [clearMusicFade]);
12352
+ return {
12353
+ play,
12354
+ stop,
12355
+ stopAll,
12356
+ playMusic,
12357
+ stopMusic,
12358
+ muted,
12359
+ setMuted,
12360
+ masterVolume,
12361
+ setMasterVolume
12362
+ };
12363
+ }
12103
12364
  var init_useGameAudio = __esm({
12104
12365
  "components/game/shared/hooks/useGameAudio.ts"() {
12105
12366
  "use client";
12367
+ useGameAudio.displayName = "useGameAudio";
12106
12368
  }
12107
12369
  });
12108
12370
  function useGameAudioContextOptional() {
12109
12371
  return useContext(GameAudioContext);
12110
12372
  }
12373
+ function GameAudioProvider({
12374
+ manifest,
12375
+ baseUrl = "",
12376
+ children,
12377
+ initialMuted = false
12378
+ }) {
12379
+ const eventBus = useEventBus();
12380
+ const { play, stop, stopAll, playMusic, stopMusic, muted, setMuted, masterVolume, setMasterVolume } = useGameAudio({ manifest, baseUrl, initialMuted });
12381
+ useEffect(() => {
12382
+ const unsubPlay = eventBus.on("UI:PLAY_SOUND", (event) => {
12383
+ const key = event.payload?.key;
12384
+ if (key) play(key);
12385
+ });
12386
+ const unsubStop = eventBus.on("UI:STOP_SOUND", (event) => {
12387
+ const key = event.payload?.key;
12388
+ if (key) {
12389
+ stop(key);
12390
+ } else {
12391
+ stopAll();
12392
+ }
12393
+ });
12394
+ const unsubChangeMusic = eventBus.on("UI:CHANGE_MUSIC", (event) => {
12395
+ const key = event.payload?.key;
12396
+ if (key) {
12397
+ playMusic(key);
12398
+ } else {
12399
+ stopMusic();
12400
+ }
12401
+ });
12402
+ const unsubStopMusic = eventBus.on("UI:STOP_MUSIC", () => {
12403
+ stopMusic();
12404
+ });
12405
+ return () => {
12406
+ unsubPlay();
12407
+ unsubStop();
12408
+ unsubChangeMusic();
12409
+ unsubStopMusic();
12410
+ };
12411
+ }, [eventBus, play, stop, stopAll, playMusic, stopMusic]);
12412
+ const value = { muted, setMuted, masterVolume, setMasterVolume, play, playMusic, stopMusic };
12413
+ return /* @__PURE__ */ jsx(GameAudioContext.Provider, { value, children });
12414
+ }
12111
12415
  var GameAudioContext;
12112
12416
  var init_GameAudioProvider = __esm({
12113
12417
  "components/game/shared/providers/GameAudioProvider.tsx"() {
@@ -12116,6 +12420,7 @@ var init_GameAudioProvider = __esm({
12116
12420
  init_useGameAudio();
12117
12421
  GameAudioContext = createContext(null);
12118
12422
  GameAudioContext.displayName = "GameAudioContext";
12423
+ GameAudioProvider.displayName = "GameAudioProvider";
12119
12424
  }
12120
12425
  });
12121
12426
  function GameAudioToggle({
@@ -50756,9 +51061,33 @@ var init_ToastSlot = __esm({
50756
51061
  ToastSlot.displayName = "ToastSlot";
50757
51062
  }
50758
51063
  });
50759
-
50760
- // components/core/organisms/component-registry.generated.ts
50761
- var COMPONENT_REGISTRY;
51064
+ function lazyThree(name, loader) {
51065
+ const Lazy = React107__default.lazy(
51066
+ () => loader().then((m) => {
51067
+ const Resolved = m[name];
51068
+ if (!Resolved) {
51069
+ throw new Error(
51070
+ `[@almadar/ui] 3D component "${name}" was not found in the three subpath bundle.`
51071
+ );
51072
+ }
51073
+ return { default: Resolved };
51074
+ })
51075
+ );
51076
+ function ThreeWrapper(props) {
51077
+ return React107__default.createElement(
51078
+ ThreeBoundary,
51079
+ { name },
51080
+ React107__default.createElement(
51081
+ React107__default.Suspense,
51082
+ { fallback: null },
51083
+ React107__default.createElement(Lazy, props)
51084
+ )
51085
+ );
51086
+ }
51087
+ ThreeWrapper.displayName = `Lazy(${name})`;
51088
+ return ThreeWrapper;
51089
+ }
51090
+ var ThreeBoundary, GameBoard3D, GameCanvas3D, GameCanvas3DBattleTemplate, GameCanvas3DCastleTemplate, GameCanvas3DWorldMapTemplate, COMPONENT_REGISTRY;
50762
51091
  var init_component_registry_generated = __esm({
50763
51092
  "components/core/organisms/component-registry.generated.ts"() {
50764
51093
  init_AboutPageTemplate();
@@ -50869,9 +51198,11 @@ var init_component_registry_generated = __esm({
50869
51198
  init_Form();
50870
51199
  init_FormField();
50871
51200
  init_FormSectionHeader();
51201
+ init_GameAudioProvider();
50872
51202
  init_GameAudioToggle();
50873
51203
  init_GameCard();
50874
51204
  init_GameHud();
51205
+ init_GameIcon();
50875
51206
  init_GameMenu();
50876
51207
  init_GameShell();
50877
51208
  init_GameTemplate();
@@ -51056,6 +51387,33 @@ var init_component_registry_generated = __esm({
51056
51387
  init_WizardProgress();
51057
51388
  init_WorldMapBoard();
51058
51389
  init_WorldMapTemplate();
51390
+ ThreeBoundary = class extends React107__default.Component {
51391
+ constructor() {
51392
+ super(...arguments);
51393
+ __publicField(this, "state", { failed: false });
51394
+ }
51395
+ static getDerivedStateFromError() {
51396
+ return { failed: true };
51397
+ }
51398
+ render() {
51399
+ if (this.state.failed) {
51400
+ return React107__default.createElement(
51401
+ "div",
51402
+ {
51403
+ "data-testid": "three-unavailable",
51404
+ style: { padding: 16, fontSize: 13, lineHeight: 1.5, opacity: 0.7 }
51405
+ },
51406
+ `3D pattern "${this.props.name}" requires three.js. Install the optional peers three + @react-three/fiber + @react-three/drei (matching the host React major) to render it.`
51407
+ );
51408
+ }
51409
+ return this.props.children;
51410
+ }
51411
+ };
51412
+ GameBoard3D = lazyThree("GameBoard3D", () => import('@almadar/ui/components/molecules/game/three'));
51413
+ GameCanvas3D = lazyThree("GameCanvas3D", () => import('@almadar/ui/components/molecules/game/three'));
51414
+ GameCanvas3DBattleTemplate = lazyThree("GameCanvas3DBattleTemplate", () => import('@almadar/ui/components/molecules/game/three'));
51415
+ GameCanvas3DCastleTemplate = lazyThree("GameCanvas3DCastleTemplate", () => import('@almadar/ui/components/molecules/game/three'));
51416
+ GameCanvas3DWorldMapTemplate = lazyThree("GameCanvas3DWorldMapTemplate", () => import('@almadar/ui/components/molecules/game/three'));
51059
51417
  COMPONENT_REGISTRY = {
51060
51418
  "AboutPageTemplate": AboutPageTemplate,
51061
51419
  "Accordion": Accordion,
@@ -51171,9 +51529,16 @@ var init_component_registry_generated = __esm({
51171
51529
  "Form": Form,
51172
51530
  "FormField": FormField,
51173
51531
  "FormSectionHeader": FormSectionHeader,
51532
+ "GameAudioProvider": GameAudioProvider,
51174
51533
  "GameAudioToggle": GameAudioToggle,
51534
+ "GameBoard3D": GameBoard3D,
51535
+ "GameCanvas3D": GameCanvas3D,
51536
+ "GameCanvas3DBattleTemplate": GameCanvas3DBattleTemplate,
51537
+ "GameCanvas3DCastleTemplate": GameCanvas3DCastleTemplate,
51538
+ "GameCanvas3DWorldMapTemplate": GameCanvas3DWorldMapTemplate,
51175
51539
  "GameCard": GameCard,
51176
51540
  "GameHud": GameHud,
51541
+ "GameIcon": GameIcon,
51177
51542
  "GameMenu": GameMenu,
51178
51543
  "GameShell": GameShell,
51179
51544
  "GameTemplate": GameTemplate,