@energy8platform/game-engine 0.34.2 → 0.35.0

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.
Files changed (45) hide show
  1. package/dist/audio.cjs.js +114 -59
  2. package/dist/audio.cjs.js.map +1 -1
  3. package/dist/audio.d.ts +25 -0
  4. package/dist/audio.esm.js +114 -59
  5. package/dist/audio.esm.js.map +1 -1
  6. package/dist/core.cjs.js +222 -66
  7. package/dist/core.cjs.js.map +1 -1
  8. package/dist/core.d.ts +25 -0
  9. package/dist/core.esm.js +223 -67
  10. package/dist/core.esm.js.map +1 -1
  11. package/dist/flow.cjs.js +246 -0
  12. package/dist/flow.cjs.js.map +1 -1
  13. package/dist/flow.d.ts +192 -33
  14. package/dist/flow.esm.js +238 -1
  15. package/dist/flow.esm.js.map +1 -1
  16. package/dist/host.cjs.js +343 -82
  17. package/dist/host.cjs.js.map +1 -1
  18. package/dist/host.d.ts +82 -2
  19. package/dist/host.esm.js +344 -83
  20. package/dist/host.esm.js.map +1 -1
  21. package/dist/index.cjs.js +222 -66
  22. package/dist/index.cjs.js.map +1 -1
  23. package/dist/index.d.ts +72 -0
  24. package/dist/index.esm.js +223 -67
  25. package/dist/index.esm.js.map +1 -1
  26. package/dist/scene-devtools.cjs.js +529 -115
  27. package/dist/scene-devtools.cjs.js.map +1 -1
  28. package/dist/scene-devtools.d.ts +187 -34
  29. package/dist/scene-devtools.esm.js +529 -115
  30. package/dist/scene-devtools.esm.js.map +1 -1
  31. package/dist/scene.cjs.js +704 -46
  32. package/dist/scene.cjs.js.map +1 -1
  33. package/dist/scene.d.ts +228 -41
  34. package/dist/scene.esm.js +698 -47
  35. package/dist/scene.esm.js.map +1 -1
  36. package/package.json +2 -2
  37. package/src/audio/AudioManager.ts +111 -53
  38. package/src/core/GameApplication.ts +47 -5
  39. package/src/host/buildConfig.ts +17 -4
  40. package/src/host/createSlotGame.ts +114 -12
  41. package/src/host/index.ts +3 -0
  42. package/src/host/types.ts +58 -0
  43. package/src/loading/LoadingScene.ts +76 -2
  44. package/src/loading/index.ts +6 -0
  45. package/src/types.ts +2 -0
package/dist/index.cjs.js CHANGED
@@ -753,6 +753,12 @@ class AudioManager {
753
753
  _categories;
754
754
  _masterGain = 1.0;
755
755
  _currentMusic = null;
756
+ /** Duck factor (0..1) from duckMusic/unduckMusic. A presentation state, not a player setting. */
757
+ _musicDuck = 1;
758
+ /** Crossfade ramp (0..1) for the track that is fading IN. 1 whenever no fade is running. */
759
+ _musicFade = 1;
760
+ /** Generation counter so a superseded crossfade ramp stops writing over the new track's. */
761
+ _musicFadeToken = 0;
756
762
  _unlocked = false;
757
763
  _unlockHandler = null;
758
764
  constructor(config) {
@@ -811,7 +817,10 @@ class AudioManager {
811
817
  if (this._globalMuted || this._categories[category].muted)
812
818
  return;
813
819
  const { sound } = this._soundModule;
814
- const vol = (options?.volume ?? 1) * this._categories[category].volume * this._masterGain;
820
+ // The master gain lives on the GLOBAL bus (`sound.volumeAll`, see applyVolumes) and @pixi/sound
821
+ // already multiplies it in — folding it in here as well squared it, so a master of 0.5 played
822
+ // sfx at 0.25.
823
+ const vol = (options?.volume ?? 1) * this._categories[category].volume;
815
824
  try {
816
825
  sound.play(alias, {
817
826
  volume: vol,
@@ -833,52 +842,50 @@ class AudioManager {
833
842
  if (!this._initialized || !this._soundModule)
834
843
  return;
835
844
  const { sound } = this._soundModule;
836
- // Stop current music with fade-out, start new music with fade-in
837
- if (this._currentMusic && fadeDuration > 0) {
838
- const prevAlias = this._currentMusic;
839
- this._currentMusic = alias;
840
- if (this._globalMuted || this._categories.music.muted)
841
- return;
842
- // Fade out the previous track
843
- this.fadeVolume(prevAlias, this._categories.music.volume * this._masterGain, 0, fadeDuration, () => {
844
- try {
845
- sound.stop(prevAlias);
846
- }
847
- catch { /* ignore */ }
848
- });
849
- // Start new track at zero volume, fade in
850
- try {
851
- sound.play(alias, {
852
- volume: 0,
853
- loop: true,
845
+ const prevAlias = this._currentMusic;
846
+ const crossfade = !!prevAlias && prevAlias !== alias && fadeDuration > 0;
847
+ // Retire the outgoing track. Its own SOUND-level volume is the only thing still pointing at it,
848
+ // so fading that to 0 is safe — nothing else writes it once `_currentMusic` has moved on.
849
+ if (prevAlias) {
850
+ if (crossfade) {
851
+ const from = this.soundVolumeOf(prevAlias);
852
+ this.fadeVolume(prevAlias, from, 0, fadeDuration, () => {
853
+ try {
854
+ sound.stop(prevAlias);
855
+ }
856
+ catch { /* ignore */ }
854
857
  });
855
- this.fadeVolume(alias, 0, this._categories.music.volume * this._masterGain, fadeDuration);
856
- }
857
- catch (e) {
858
- console.warn(`[AudioManager] Failed to play music "${alias}":`, e);
859
858
  }
860
- }
861
- else {
862
- // No crossfade — instant switch
863
- if (this._currentMusic) {
859
+ else {
864
860
  try {
865
- sound.stop(this._currentMusic);
861
+ sound.stop(prevAlias);
866
862
  }
867
863
  catch { /* ignore */ }
868
864
  }
869
- this._currentMusic = alias;
870
- if (this._globalMuted || this._categories.music.muted)
871
- return;
872
- try {
873
- sound.play(alias, {
874
- volume: this._categories.music.volume * this._masterGain,
875
- loop: true,
876
- });
877
- }
878
- catch (e) {
879
- console.warn(`[AudioManager] Failed to play music "${alias}":`, e);
880
- }
881
865
  }
866
+ this._currentMusic = alias;
867
+ this._musicFadeToken++; // any ramp still running belongs to a track we just replaced
868
+ // Deliberately started even while muted. Global mute is the @pixi/sound CONTEXT mute and a
869
+ // muted music category is a 0 term in `musicGain()` — both already make this inaudible, and
870
+ // both undo themselves the moment the player flips them back. Returning early here instead
871
+ // meant a track begun while muted never existed, so unmuting restored silence until some
872
+ // later mode change happened to switch tracks.
873
+ // The incoming track plays at INSTANCE volume 1 and carries its whole gain on the SOUND layer
874
+ // (`musicGain()`), which is the layer the slider, the duck and this fade all write. Splitting
875
+ // them across layers is what silenced every crossfade: the track was started at instance volume
876
+ // 0 and the ramp then moved the sound layer, whose product with 0 is 0 for the track's life.
877
+ // The gain is written BEFORE play() so the first frame is never at full volume.
878
+ this._musicFade = crossfade ? 0 : 1;
879
+ this.applyMusicGain();
880
+ try {
881
+ sound.play(alias, { volume: 1, loop: true });
882
+ }
883
+ catch (e) {
884
+ console.warn(`[AudioManager] Failed to play music "${alias}":`, e);
885
+ return;
886
+ }
887
+ if (crossfade)
888
+ this.rampMusicFade(fadeDuration);
882
889
  }
883
890
  /**
884
891
  * Stop current music.
@@ -894,6 +901,10 @@ class AudioManager {
894
901
  // ignore
895
902
  }
896
903
  this._currentMusic = null;
904
+ // Retire any running ramp and clear the fade term, so the next track does not inherit a
905
+ // half-finished crossfade and start silent.
906
+ this._musicFadeToken++;
907
+ this._musicFade = 1;
897
908
  }
898
909
  /**
899
910
  * Stop all sounds.
@@ -919,6 +930,9 @@ class AudioManager {
919
930
  */
920
931
  setVolume(category, volume) {
921
932
  this._categories[category].volume = Math.max(0, Math.min(1, volume));
933
+ // applyVolumes() re-pushes the music gain, so moving the Music slider is heard on the track
934
+ // that is ALREADY playing — it used to take effect only at the next playMusic (a mode change).
935
+ // SFX need no push: play() reads the category volume fresh on every call.
922
936
  this.applyVolumes();
923
937
  this.saveState();
924
938
  }
@@ -991,30 +1005,19 @@ class AudioManager {
991
1005
  * @param factor - Volume multiplier (0..1), e.g. 0.3 = 30% of normal
992
1006
  */
993
1007
  duckMusic(factor) {
994
- if (!this._initialized || !this._soundModule || !this._currentMusic)
995
- return;
996
- const { sound } = this._soundModule;
997
- const vol = this._categories.music.volume * factor;
998
- try {
999
- sound.volume(this._currentMusic, vol);
1000
- }
1001
- catch {
1002
- // ignore
1003
- }
1008
+ // Held as a FACTOR rather than written as a finished volume: the duck used to write
1009
+ // `category × factor` onto a track whose instance already carried the category volume, so it
1010
+ // ducked to category², and unducking restored category² instead of category. Keeping it as one
1011
+ // term of `musicGain()` also keeps the slider live while ducked.
1012
+ this._musicDuck = Math.max(0, Math.min(1, factor));
1013
+ this.applyMusicGain();
1004
1014
  }
1005
1015
  /**
1006
1016
  * Restore music to normal volume after ducking.
1007
1017
  */
1008
1018
  unduckMusic() {
1009
- if (!this._initialized || !this._soundModule || !this._currentMusic)
1010
- return;
1011
- const { sound } = this._soundModule;
1012
- try {
1013
- sound.volume(this._currentMusic, this._categories.music.volume);
1014
- }
1015
- catch {
1016
- // ignore
1017
- }
1019
+ this._musicDuck = 1;
1020
+ this.applyMusicGain();
1018
1021
  }
1019
1022
  /**
1020
1023
  * Destroy the audio manager and free resources.
@@ -1053,6 +1056,57 @@ class AudioManager {
1053
1056
  };
1054
1057
  requestAnimationFrame(tick);
1055
1058
  }
1059
+ /**
1060
+ * The SOUND-layer gain for the running music track.
1061
+ *
1062
+ * @pixi/sound resolves a playing instance as `instance × sound × global` (WebAudioInstance.
1063
+ * refresh). Each of those three has exactly ONE owner here, which is what keeps the mixer honest:
1064
+ * global — the master gain (`applyVolumes`)
1065
+ * sound — music: this function. sfx: untouched, left at 1.
1066
+ * instance — sfx: the per-call volume × the sfx category. music: always 1.
1067
+ * Everything that can move music volume — the player's slider, the category mute, a big-win duck,
1068
+ * a crossfade — is a term below, so they compose instead of overwriting each other.
1069
+ */
1070
+ musicGain() {
1071
+ const c = this._categories.music;
1072
+ return (c.muted ? 0 : 1) * c.volume * this._musicDuck * this._musicFade;
1073
+ }
1074
+ /** Push `musicGain()` at the current track. Safe before it starts playing and with none playing. */
1075
+ applyMusicGain() {
1076
+ if (!this._soundModule || !this._currentMusic)
1077
+ return;
1078
+ try {
1079
+ this._soundModule.sound.volume(this._currentMusic, this.musicGain());
1080
+ }
1081
+ catch {
1082
+ // ignore — alias not registered yet
1083
+ }
1084
+ }
1085
+ /** Current SOUND-layer volume of `alias`, or 0 when it cannot be read. */
1086
+ soundVolumeOf(alias) {
1087
+ try {
1088
+ return Number(this._soundModule.sound.volume(alias)) || 0;
1089
+ }
1090
+ catch {
1091
+ return 0;
1092
+ }
1093
+ }
1094
+ /** Ramp the crossfade term 0 → 1 over `durationMs`, recomposing the gain each frame so a slider
1095
+ * drag or a duck landing mid-fade is honoured rather than overwritten when the fade ends. */
1096
+ rampMusicFade(durationMs) {
1097
+ const token = this._musicFadeToken;
1098
+ const start = Date.now();
1099
+ const tick = () => {
1100
+ if (token !== this._musicFadeToken)
1101
+ return; // a newer track owns the music now
1102
+ const t = Math.min((Date.now() - start) / durationMs, 1);
1103
+ this._musicFade = t;
1104
+ this.applyMusicGain();
1105
+ if (t < 1)
1106
+ requestAnimationFrame(tick);
1107
+ };
1108
+ requestAnimationFrame(tick);
1109
+ }
1056
1110
  applyVolumes() {
1057
1111
  if (!this._soundModule)
1058
1112
  return;
@@ -1060,6 +1114,7 @@ class AudioManager {
1060
1114
  // Global mute is owned by sound.muteAll()/unmuteAll() (context.muted),
1061
1115
  // not by volumeAll — mixing both leaves mute un-undoable after reload.
1062
1116
  sound.volumeAll = this._masterGain; // master multiplies the global bus
1117
+ this.applyMusicGain(); // category volume/mute reach the RUNNING track
1063
1118
  }
1064
1119
  setupMobileUnlock() {
1065
1120
  if (this._unlocked)
@@ -1541,6 +1596,14 @@ class Scene {
1541
1596
  * tap-to-start → `waitCSSPreloaderTap`, then fades it out via
1542
1597
  * `removeCSSPreloader` before entering the game. One continuous overlay from
1543
1598
  * boot to gameplay — no second logo, no mid-load flash.
1599
+ *
1600
+ * When the game supplied its own overlay (`loading.externalOverlay`, e.g.
1601
+ * Artube's `LoaderViewController`), this scene is also the HAND-OVER point: that
1602
+ * overlay covered the gap this scene's existence ends — the bundle download,
1603
+ * Pixi init and the SDK handshake, none of which the engine can paint over. The
1604
+ * first thing `onEnter` does is mount the preloader, wait for it to be painted,
1605
+ * and dismiss the game's overlay. Everything after that line is identical on
1606
+ * every platform.
1544
1607
  */
1545
1608
  class LoadingScene extends Scene {
1546
1609
  _engine;
@@ -1558,6 +1621,10 @@ class LoadingScene extends Scene {
1558
1621
  this._targetScene = targetScene;
1559
1622
  this._targetData = targetData;
1560
1623
  this._config = engine.config.loading ?? {};
1624
+ // Take the screen from a game-supplied loading overlay, if there is one. Before any awaited
1625
+ // work: from here on the player is looking at OUR loading screen, and `_startTime` (which
1626
+ // `minDisplayTime` is measured from) must start when that becomes true.
1627
+ await this.takeOverFromExternalOverlay();
1561
1628
  this._startTime = Date.now();
1562
1629
  // Initialize asset manager
1563
1630
  await this._engine.assets.init();
@@ -1604,8 +1671,10 @@ class LoadingScene extends Scene {
1604
1671
  // Final snap to 100%
1605
1672
  this._displayedProgress = 1;
1606
1673
  this.updateLoaderBar(1);
1607
- // Wait for the player's tap — resolves immediately when tapToStart is
1608
- // false (the preloader honours that flag) then enter the game.
1674
+ // Wait for the player's tap — resolves immediately when tapToStart is false — then enter the
1675
+ // game. This is the preloader's gate and it reads the preloader's config, so it means the same
1676
+ // thing on every target: a game-supplied overlay has no say in it, and by now no part in the
1677
+ // screen either. It was dismissed at the hand-over above; the player is looking at ours.
1609
1678
  await loading.waitCSSPreloaderTap();
1610
1679
  await this.transitionToGame();
1611
1680
  }
@@ -1624,6 +1693,59 @@ class LoadingScene extends Scene {
1624
1693
  // (e.g. the scene was popped externally). Idempotent.
1625
1694
  void loading.removeCSSPreloader(this.hostElement());
1626
1695
  }
1696
+ // ─── Hand-over from a game-supplied overlay ────────────
1697
+ /**
1698
+ * Swap a game-supplied loading overlay for the engine's own loading screen.
1699
+ *
1700
+ * The overlay (Artube's) has been on screen since before this bundle was fetched, covering a gap
1701
+ * nothing of ours could. Its job ends here, at the first frame the engine paints; the player then
1702
+ * gets the game's own brand, progress bar and tap-to-start, exactly as on every other target.
1703
+ *
1704
+ * The order of the four steps is the whole design, and each is wrong on its own:
1705
+ *
1706
+ * 0. Wait out whatever the overlay is still owed on screen (`externalOverlayMinDisplayTime`,
1707
+ * default 1.5s, plus room for a phase crossfade already in flight). The gap this overlay
1708
+ * covers can be under half a second, which is not long enough for a partner's brand to
1709
+ * register. Waiting here — BEFORE mounting ours — rather than after is what keeps the two
1710
+ * screens' timelines from overlapping: our splash and brand floor start when the player can
1711
+ * actually see them, not behind someone else's overlay. On any boot slower than the floor
1712
+ * this step costs nothing, and on a non-Artube target it is not reached at all.
1713
+ * 1. Mount the preloader, opaque and full-bleed, while theirs is still up. Both are on screen
1714
+ * together for a few frames, so there is never a moment with neither, whatever happens next.
1715
+ * 2. Wait for that frame to actually be PAINTED — mounting only queues it. Dismissing theirs
1716
+ * before the paint is precisely the flash of bare background this ordering exists to avoid.
1717
+ * Two `requestAnimationFrame`s: the first callback runs before the frame it belongs to is
1718
+ * composited, the second after. Two frames is also enough for Pixi's own rAF-driven ticker
1719
+ * to have rendered this scene at least once, so "the loading scene has painted" is literally
1720
+ * true by the time step 3 runs.
1721
+ * 3. Only then dismiss theirs. Their `hideLoader()` plays a 0.3s fade and removes the element.
1722
+ * Not waiting for that fade is deliberate — it is an animation on someone else's element,
1723
+ * and blocking a boot on it would be a hang waiting to happen.
1724
+ *
1725
+ * Which of the two is visually on top is the host page's business, not ours, and it does NOT
1726
+ * change the guarantee. On a typical game page (`#game { position: fixed; inset: 0 }`) the fixed
1727
+ * container establishes a stacking context, so the preloader's z-index is scoped inside it and
1728
+ * Artube's `position: fixed; z-index: 9999` sits above — their fade then crossfades onto our
1729
+ * loading screen, which is what was observed live and looks right. On a page where ours wins
1730
+ * instead, their fade simply plays underneath, unseen. Either way the seam is covered, because
1731
+ * what step 2 buys is that OUR screen is already painted before theirs starts going away.
1732
+ */
1733
+ async takeOverFromExternalOverlay() {
1734
+ if (!loading.hasExternalOverlay())
1735
+ return;
1736
+ await loading.externalOverlayHold();
1737
+ loading.createCSSPreloader(this.hostElement(), this._config);
1738
+ await this.nextPaint();
1739
+ loading.releaseExternalOverlay();
1740
+ }
1741
+ /** Resolves after the browser has composited at least one frame (see step 2 above). */
1742
+ nextPaint() {
1743
+ if (typeof requestAnimationFrame !== 'function')
1744
+ return Promise.resolve();
1745
+ return new Promise((resolve) => {
1746
+ requestAnimationFrame(() => requestAnimationFrame(() => resolve()));
1747
+ });
1748
+ }
1627
1749
  // ─── Progress ──────────────────────────────────────────
1628
1750
  updateLoaderBar(progress) {
1629
1751
  loading.setCSSPreloaderProgress(Math.max(0, Math.min(1, progress)));
@@ -1878,14 +2000,37 @@ class GameApplication extends EventEmitter {
1878
2000
  return;
1879
2001
  }
1880
2002
  try {
2003
+ // 0. Adopt a game-supplied loading overlay (`loading.externalOverlay`) BEFORE anything that
2004
+ // can throw. Such an overlay is already on screen — Artube's is injected into index.html,
2005
+ // so it paints before this bundle is even fetched — and until the engine has adopted it,
2006
+ // the catch below has no way to take it down. A bad `container` selector (step 1) would
2007
+ // otherwise strand it on screen forever. It needs no container of ours.
2008
+ // Adoption is also where its minimum display time starts counting, which is why the
2009
+ // config value is handed over here rather than read at the hand-over: this is the
2010
+ // earliest moment the engine runs, and the overlay has been on screen since before it.
2011
+ const external = this.config.loading?.externalOverlay;
2012
+ if (external)
2013
+ loading.adoptExternalOverlay(external, this.config.loading?.externalOverlayMinDisplayTime);
1881
2014
  // 1. Resolve container element
1882
2015
  this._container = this.resolveContainer();
1883
- // 2. Show CSS preloader immediately (before PixiJS)
1884
- loading.createCSSPreloader(this._container, this.config.loading);
2016
+ // 2. Show the CSS preloader immediately (before PixiJS) — UNLESS a game-supplied overlay is
2017
+ // already covering the screen. In that case the preloader is mounted later, by LoadingScene
2018
+ // at its first frame, which is where the hand-over happens. Mounting it here instead would
2019
+ // put our brand over theirs for the whole of Pixi init and the SDK handshake, i.e. hand
2020
+ // over long before the gap the external overlay exists to cover has closed.
2021
+ if (!loading.hasExternalOverlay())
2022
+ loading.createCSSPreloader(this._container, this.config.loading);
1885
2023
  // 3. Initialize PixiJS
1886
2024
  await this.initPixi();
2025
+ // Milestones through the pre-first-frame gap, for a game-supplied overlay only (no-ops
2026
+ // otherwise, so the built-in preloader's behaviour is untouched). They are also what makes
2027
+ // Artube's loader crossfade from its dark partner phase to its branded one: that transition
2028
+ // fires on the first progress above zero, and without it the player would never see the
2029
+ // brand the loader exists to show. Values are honest weights of what remains, not a timer.
2030
+ loading.advanceExternalOverlay(0.35);
1887
2031
  // 4. Initialize SDK (if enabled)
1888
2032
  await this.initSDK();
2033
+ loading.advanceExternalOverlay(0.7);
1889
2034
  // 4b. Mount the branded game shell after the SDK handshake (optional)
1890
2035
  if (this.config.shell) {
1891
2036
  const { createGameShell } = await import('@energy8platform/shell/html');
@@ -1895,10 +2040,17 @@ class GameApplication extends EventEmitter {
1895
2040
  this.applySDKConfig();
1896
2041
  // 6. Initialize sub-systems
1897
2042
  this.initSubSystems();
2043
+ loading.advanceExternalOverlay(0.85);
1898
2044
  this.emit('initialized');
1899
2045
  // 7. Load assets. The CSS preloader stays on screen — LoadingScene drives
1900
2046
  // its progress/tap and removes it before entering the game, so there's
1901
2047
  // a single continuous overlay from boot to gameplay (no logo flash).
2048
+ //
2049
+ // With a game-supplied overlay the sequence has one extra step at the
2050
+ // front: LoadingScene MOUNTS the preloader, waits for its first painted
2051
+ // frame, and only then dismisses the external overlay. From that frame
2052
+ // on this path and every other are identical — same brand, same bar,
2053
+ // same tap-to-start.
1902
2054
  await this.loadAssets(firstScene, sceneData);
1903
2055
  this.emit('loaded');
1904
2056
  // 8. Start the game loop
@@ -1907,9 +2059,13 @@ class GameApplication extends EventEmitter {
1907
2059
  }
1908
2060
  catch (err) {
1909
2061
  console.error('[GameEngine] Failed to start:', err);
1910
- // Tear down the preloader so a failure doesn't strand the brand frame.
1911
- if (this._container)
1912
- loading.removeCSSPreloader(this._container);
2062
+ // Tear down both possible overlays so a failure strands neither brand frame. BOTH calls run:
2063
+ // a throw during the hand-over window can leave the preloader mounted AND the external
2064
+ // overlay still adopted, and each call is a no-op when there is nothing to remove. The
2065
+ // container may never have resolved (step 1 is inside this try), hence the `document.body`
2066
+ // fallback — the external overlay ignores the element entirely.
2067
+ loading.releaseExternalOverlay();
2068
+ void loading.removeCSSPreloader(this._container ?? document.body);
1913
2069
  this.emit('error', err instanceof Error ? err : new Error(String(err)));
1914
2070
  throw err;
1915
2071
  }