@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.
package/dist/avl/index.js CHANGED
@@ -15854,14 +15854,318 @@ var init_useCanvasEffects = __esm({
15854
15854
  "use client";
15855
15855
  }
15856
15856
  });
15857
+ function pickPath(entry) {
15858
+ if (Array.isArray(entry.path)) {
15859
+ return entry.path[Math.floor(Math.random() * entry.path.length)];
15860
+ }
15861
+ return entry.path;
15862
+ }
15863
+ function useGameAudio({
15864
+ manifest,
15865
+ baseUrl = "",
15866
+ initialMuted = false,
15867
+ initialVolume = 1
15868
+ }) {
15869
+ const [muted, setMutedState] = useState(initialMuted);
15870
+ const [masterVolume, setMasterVolumeState] = useState(initialVolume);
15871
+ const mutedRef = useRef(muted);
15872
+ const volumeRef = useRef(masterVolume);
15873
+ const manifestRef = useRef(manifest);
15874
+ mutedRef.current = muted;
15875
+ volumeRef.current = masterVolume;
15876
+ manifestRef.current = manifest;
15877
+ const poolsRef = useRef(/* @__PURE__ */ new Map());
15878
+ const getOrCreateElement = useCallback((key) => {
15879
+ const entry = manifestRef.current[key];
15880
+ if (!entry) return null;
15881
+ let pool = poolsRef.current.get(key);
15882
+ if (!pool) {
15883
+ pool = [];
15884
+ poolsRef.current.set(key, pool);
15885
+ }
15886
+ const maxSize = entry.poolSize ?? 1;
15887
+ for (const audio of pool) {
15888
+ if (audio.paused && (audio.ended || audio.currentTime === 0)) {
15889
+ return audio;
15890
+ }
15891
+ }
15892
+ if (pool.length < maxSize) {
15893
+ const src = baseUrl + pickPath(entry);
15894
+ const audio = new Audio(src);
15895
+ audio.loop = entry.loop ?? false;
15896
+ pool.push(audio);
15897
+ return audio;
15898
+ }
15899
+ if (!entry.loop) {
15900
+ let oldest = pool[0];
15901
+ for (const audio of pool) {
15902
+ if (audio.currentTime > oldest.currentTime) {
15903
+ oldest = audio;
15904
+ }
15905
+ }
15906
+ oldest.pause();
15907
+ oldest.currentTime = 0;
15908
+ return oldest;
15909
+ }
15910
+ return null;
15911
+ }, [baseUrl]);
15912
+ const play = useCallback((key) => {
15913
+ if (mutedRef.current) return;
15914
+ const entry = manifestRef.current[key];
15915
+ if (!entry) return;
15916
+ const audio = getOrCreateElement(key);
15917
+ if (!audio) return;
15918
+ audio.volume = Math.min(1, (entry.volume ?? 1) * volumeRef.current);
15919
+ if (!entry.loop) {
15920
+ audio.currentTime = 0;
15921
+ }
15922
+ const promise = audio.play();
15923
+ if (promise) {
15924
+ promise.catch(() => {
15925
+ });
15926
+ }
15927
+ }, [getOrCreateElement]);
15928
+ const stop = useCallback((key) => {
15929
+ const pool = poolsRef.current.get(key);
15930
+ if (!pool) return;
15931
+ for (const audio of pool) {
15932
+ audio.pause();
15933
+ audio.currentTime = 0;
15934
+ }
15935
+ }, []);
15936
+ const currentMusicKeyRef = useRef(null);
15937
+ const currentMusicElRef = useRef(null);
15938
+ const musicFadeRef = useRef(null);
15939
+ const pendingMusicKeyRef = useRef(null);
15940
+ const clearMusicFade = useCallback(() => {
15941
+ if (musicFadeRef.current) {
15942
+ clearInterval(musicFadeRef.current);
15943
+ musicFadeRef.current = null;
15944
+ }
15945
+ }, []);
15946
+ const playMusic = useCallback((key) => {
15947
+ if (key === currentMusicKeyRef.current) return;
15948
+ pendingMusicKeyRef.current = key;
15949
+ const entry = manifestRef.current[key];
15950
+ if (!entry) return;
15951
+ const fadeDurationMs = entry.crossfadeDurationMs ?? 1500;
15952
+ const stepMs = 50;
15953
+ const totalSteps = Math.max(1, fadeDurationMs / stepMs);
15954
+ const targetVolume = Math.min(1, (entry.volume ?? 1) * volumeRef.current);
15955
+ clearMusicFade();
15956
+ const src = baseUrl + (Array.isArray(entry.path) ? entry.path[0] : entry.path);
15957
+ const incoming = new Audio(src);
15958
+ incoming.loop = true;
15959
+ incoming.volume = 0;
15960
+ const outgoing = currentMusicElRef.current;
15961
+ const outgoingStartVol = outgoing?.volume ?? 0;
15962
+ currentMusicKeyRef.current = key;
15963
+ currentMusicElRef.current = incoming;
15964
+ if (!mutedRef.current) {
15965
+ incoming.play().catch(() => {
15966
+ currentMusicKeyRef.current = null;
15967
+ currentMusicElRef.current = outgoing;
15968
+ });
15969
+ }
15970
+ let step = 0;
15971
+ musicFadeRef.current = setInterval(() => {
15972
+ step++;
15973
+ const progress = Math.min(step / totalSteps, 1);
15974
+ incoming.volume = Math.min(1, targetVolume * progress);
15975
+ if (outgoing) {
15976
+ outgoing.volume = Math.max(0, outgoingStartVol * (1 - progress));
15977
+ }
15978
+ if (progress >= 1) {
15979
+ clearMusicFade();
15980
+ if (outgoing) {
15981
+ outgoing.pause();
15982
+ outgoing.src = "";
15983
+ }
15984
+ }
15985
+ }, stepMs);
15986
+ }, [baseUrl, clearMusicFade]);
15987
+ const stopMusic = useCallback((fadeDurationMs = 1e3) => {
15988
+ const outgoing = currentMusicElRef.current;
15989
+ if (!outgoing) return;
15990
+ currentMusicKeyRef.current = null;
15991
+ currentMusicElRef.current = null;
15992
+ pendingMusicKeyRef.current = null;
15993
+ clearMusicFade();
15994
+ const startVolume = outgoing.volume;
15995
+ const stepMs = 50;
15996
+ const totalSteps = Math.max(1, fadeDurationMs / stepMs);
15997
+ let step = 0;
15998
+ musicFadeRef.current = setInterval(() => {
15999
+ step++;
16000
+ const progress = step / totalSteps;
16001
+ outgoing.volume = Math.max(0, startVolume * (1 - progress));
16002
+ if (progress >= 1) {
16003
+ clearMusicFade();
16004
+ outgoing.pause();
16005
+ outgoing.src = "";
16006
+ }
16007
+ }, stepMs);
16008
+ }, [clearMusicFade]);
16009
+ const stopAll = useCallback(() => {
16010
+ for (const pool of poolsRef.current.values()) {
16011
+ for (const audio of pool) {
16012
+ audio.pause();
16013
+ audio.currentTime = 0;
16014
+ }
16015
+ }
16016
+ stopMusic(0);
16017
+ }, [stopMusic]);
16018
+ const setMuted = useCallback((value) => {
16019
+ setMutedState(value);
16020
+ if (value) {
16021
+ for (const [key, pool] of poolsRef.current.entries()) {
16022
+ if (manifestRef.current[key]?.loop) {
16023
+ for (const audio of pool) {
16024
+ if (!audio.paused) audio.pause();
16025
+ }
16026
+ }
16027
+ }
16028
+ currentMusicElRef.current?.pause();
16029
+ } else {
16030
+ for (const [key, pool] of poolsRef.current.entries()) {
16031
+ const entry = manifestRef.current[key];
16032
+ if (entry?.loop && entry?.autostart) {
16033
+ for (const audio of pool) {
16034
+ if (audio.paused) audio.play().catch(() => {
16035
+ });
16036
+ }
16037
+ }
16038
+ }
16039
+ const musicEl = currentMusicElRef.current;
16040
+ if (musicEl) {
16041
+ musicEl.play().catch(() => {
16042
+ });
16043
+ }
16044
+ }
16045
+ }, []);
16046
+ const setMasterVolume = useCallback((volume) => {
16047
+ const clamped = Math.max(0, Math.min(1, volume));
16048
+ setMasterVolumeState(clamped);
16049
+ for (const [key, pool] of poolsRef.current.entries()) {
16050
+ const entryVol = manifestRef.current[key]?.volume ?? 1;
16051
+ for (const audio of pool) {
16052
+ audio.volume = Math.min(1, entryVol * clamped);
16053
+ }
16054
+ }
16055
+ if (!musicFadeRef.current && currentMusicElRef.current) {
16056
+ const key = currentMusicKeyRef.current;
16057
+ const entryVol = key ? manifestRef.current[key]?.volume ?? 1 : 1;
16058
+ currentMusicElRef.current.volume = Math.min(1, entryVol * clamped);
16059
+ }
16060
+ }, []);
16061
+ const unlockedRef = useRef(false);
16062
+ useEffect(() => {
16063
+ const autoKeys = Object.keys(manifest).filter((k) => manifest[k].autostart);
16064
+ const hasPendingMusic = () => pendingMusicKeyRef.current !== null;
16065
+ const hasAutoStart = autoKeys.length > 0;
16066
+ if (!hasAutoStart && !hasPendingMusic()) return;
16067
+ const unlock = () => {
16068
+ if (unlockedRef.current) return;
16069
+ unlockedRef.current = true;
16070
+ if (!mutedRef.current) {
16071
+ for (const key of autoKeys) {
16072
+ play(key);
16073
+ }
16074
+ const pending = pendingMusicKeyRef.current;
16075
+ if (pending && pending !== currentMusicKeyRef.current) {
16076
+ playMusic(pending);
16077
+ }
16078
+ }
16079
+ };
16080
+ document.addEventListener("click", unlock, { once: true });
16081
+ document.addEventListener("keydown", unlock, { once: true });
16082
+ document.addEventListener("touchstart", unlock, { once: true });
16083
+ return () => {
16084
+ document.removeEventListener("click", unlock);
16085
+ document.removeEventListener("keydown", unlock);
16086
+ document.removeEventListener("touchstart", unlock);
16087
+ };
16088
+ }, [manifest, play, playMusic]);
16089
+ useEffect(() => {
16090
+ return () => {
16091
+ clearMusicFade();
16092
+ for (const pool of poolsRef.current.values()) {
16093
+ for (const audio of pool) {
16094
+ audio.pause();
16095
+ audio.src = "";
16096
+ }
16097
+ }
16098
+ poolsRef.current.clear();
16099
+ if (currentMusicElRef.current) {
16100
+ currentMusicElRef.current.pause();
16101
+ currentMusicElRef.current.src = "";
16102
+ currentMusicElRef.current = null;
16103
+ }
16104
+ };
16105
+ }, [clearMusicFade]);
16106
+ return {
16107
+ play,
16108
+ stop,
16109
+ stopAll,
16110
+ playMusic,
16111
+ stopMusic,
16112
+ muted,
16113
+ setMuted,
16114
+ masterVolume,
16115
+ setMasterVolume
16116
+ };
16117
+ }
15857
16118
  var init_useGameAudio = __esm({
15858
16119
  "components/game/shared/hooks/useGameAudio.ts"() {
15859
16120
  "use client";
16121
+ useGameAudio.displayName = "useGameAudio";
15860
16122
  }
15861
16123
  });
15862
16124
  function useGameAudioContextOptional() {
15863
16125
  return useContext(GameAudioContext);
15864
16126
  }
16127
+ function GameAudioProvider({
16128
+ manifest,
16129
+ baseUrl = "",
16130
+ children,
16131
+ initialMuted = false
16132
+ }) {
16133
+ const eventBus = useEventBus();
16134
+ const { play, stop, stopAll, playMusic, stopMusic, muted, setMuted, masterVolume, setMasterVolume } = useGameAudio({ manifest, baseUrl, initialMuted });
16135
+ useEffect(() => {
16136
+ const unsubPlay = eventBus.on("UI:PLAY_SOUND", (event) => {
16137
+ const key = event.payload?.key;
16138
+ if (key) play(key);
16139
+ });
16140
+ const unsubStop = eventBus.on("UI:STOP_SOUND", (event) => {
16141
+ const key = event.payload?.key;
16142
+ if (key) {
16143
+ stop(key);
16144
+ } else {
16145
+ stopAll();
16146
+ }
16147
+ });
16148
+ const unsubChangeMusic = eventBus.on("UI:CHANGE_MUSIC", (event) => {
16149
+ const key = event.payload?.key;
16150
+ if (key) {
16151
+ playMusic(key);
16152
+ } else {
16153
+ stopMusic();
16154
+ }
16155
+ });
16156
+ const unsubStopMusic = eventBus.on("UI:STOP_MUSIC", () => {
16157
+ stopMusic();
16158
+ });
16159
+ return () => {
16160
+ unsubPlay();
16161
+ unsubStop();
16162
+ unsubChangeMusic();
16163
+ unsubStopMusic();
16164
+ };
16165
+ }, [eventBus, play, stop, stopAll, playMusic, stopMusic]);
16166
+ const value = { muted, setMuted, masterVolume, setMasterVolume, play, playMusic, stopMusic };
16167
+ return /* @__PURE__ */ jsx(GameAudioContext.Provider, { value, children });
16168
+ }
15865
16169
  var GameAudioContext;
15866
16170
  var init_GameAudioProvider = __esm({
15867
16171
  "components/game/shared/providers/GameAudioProvider.tsx"() {
@@ -15870,6 +16174,7 @@ var init_GameAudioProvider = __esm({
15870
16174
  init_useGameAudio();
15871
16175
  GameAudioContext = createContext(null);
15872
16176
  GameAudioContext.displayName = "GameAudioContext";
16177
+ GameAudioProvider.displayName = "GameAudioProvider";
15873
16178
  }
15874
16179
  });
15875
16180
  function GameAudioToggle({
@@ -52910,9 +53215,33 @@ var init_ToastSlot = __esm({
52910
53215
  ToastSlot.displayName = "ToastSlot";
52911
53216
  }
52912
53217
  });
52913
-
52914
- // components/core/organisms/component-registry.generated.ts
52915
- var COMPONENT_REGISTRY;
53218
+ function lazyThree(name, loader) {
53219
+ const Lazy = React114__default.lazy(
53220
+ () => loader().then((m) => {
53221
+ const Resolved = m[name];
53222
+ if (!Resolved) {
53223
+ throw new Error(
53224
+ `[@almadar/ui] 3D component "${name}" was not found in the three subpath bundle.`
53225
+ );
53226
+ }
53227
+ return { default: Resolved };
53228
+ })
53229
+ );
53230
+ function ThreeWrapper(props) {
53231
+ return React114__default.createElement(
53232
+ ThreeBoundary,
53233
+ { name },
53234
+ React114__default.createElement(
53235
+ React114__default.Suspense,
53236
+ { fallback: null },
53237
+ React114__default.createElement(Lazy, props)
53238
+ )
53239
+ );
53240
+ }
53241
+ ThreeWrapper.displayName = `Lazy(${name})`;
53242
+ return ThreeWrapper;
53243
+ }
53244
+ var ThreeBoundary, GameBoard3D, GameCanvas3D, GameCanvas3DBattleTemplate, GameCanvas3DCastleTemplate, GameCanvas3DWorldMapTemplate, COMPONENT_REGISTRY;
52916
53245
  var init_component_registry_generated = __esm({
52917
53246
  "components/core/organisms/component-registry.generated.ts"() {
52918
53247
  init_AboutPageTemplate();
@@ -53023,9 +53352,11 @@ var init_component_registry_generated = __esm({
53023
53352
  init_Form();
53024
53353
  init_FormField();
53025
53354
  init_FormSectionHeader();
53355
+ init_GameAudioProvider();
53026
53356
  init_GameAudioToggle();
53027
53357
  init_GameCard();
53028
53358
  init_GameHud();
53359
+ init_GameIcon();
53029
53360
  init_GameMenu();
53030
53361
  init_GameShell();
53031
53362
  init_GameTemplate();
@@ -53210,6 +53541,33 @@ var init_component_registry_generated = __esm({
53210
53541
  init_WizardProgress();
53211
53542
  init_WorldMapBoard();
53212
53543
  init_WorldMapTemplate();
53544
+ ThreeBoundary = class extends React114__default.Component {
53545
+ constructor() {
53546
+ super(...arguments);
53547
+ __publicField(this, "state", { failed: false });
53548
+ }
53549
+ static getDerivedStateFromError() {
53550
+ return { failed: true };
53551
+ }
53552
+ render() {
53553
+ if (this.state.failed) {
53554
+ return React114__default.createElement(
53555
+ "div",
53556
+ {
53557
+ "data-testid": "three-unavailable",
53558
+ style: { padding: 16, fontSize: 13, lineHeight: 1.5, opacity: 0.7 }
53559
+ },
53560
+ `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.`
53561
+ );
53562
+ }
53563
+ return this.props.children;
53564
+ }
53565
+ };
53566
+ GameBoard3D = lazyThree("GameBoard3D", () => import('@almadar/ui/components/molecules/game/three'));
53567
+ GameCanvas3D = lazyThree("GameCanvas3D", () => import('@almadar/ui/components/molecules/game/three'));
53568
+ GameCanvas3DBattleTemplate = lazyThree("GameCanvas3DBattleTemplate", () => import('@almadar/ui/components/molecules/game/three'));
53569
+ GameCanvas3DCastleTemplate = lazyThree("GameCanvas3DCastleTemplate", () => import('@almadar/ui/components/molecules/game/three'));
53570
+ GameCanvas3DWorldMapTemplate = lazyThree("GameCanvas3DWorldMapTemplate", () => import('@almadar/ui/components/molecules/game/three'));
53213
53571
  COMPONENT_REGISTRY = {
53214
53572
  "AboutPageTemplate": AboutPageTemplate,
53215
53573
  "Accordion": Accordion,
@@ -53325,9 +53683,16 @@ var init_component_registry_generated = __esm({
53325
53683
  "Form": Form,
53326
53684
  "FormField": FormField,
53327
53685
  "FormSectionHeader": FormSectionHeader,
53686
+ "GameAudioProvider": GameAudioProvider,
53328
53687
  "GameAudioToggle": GameAudioToggle,
53688
+ "GameBoard3D": GameBoard3D,
53689
+ "GameCanvas3D": GameCanvas3D,
53690
+ "GameCanvas3DBattleTemplate": GameCanvas3DBattleTemplate,
53691
+ "GameCanvas3DCastleTemplate": GameCanvas3DCastleTemplate,
53692
+ "GameCanvas3DWorldMapTemplate": GameCanvas3DWorldMapTemplate,
53329
53693
  "GameCard": GameCard,
53330
53694
  "GameHud": GameHud,
53695
+ "GameIcon": GameIcon,
53331
53696
  "GameMenu": GameMenu,
53332
53697
  "GameShell": GameShell,
53333
53698
  "GameTemplate": GameTemplate,