mwg API
    Preparing search index...

    Class Orchestrator

    The data a game writes once instead of scattering Music.play/Sound.play calls across every place a fight starts or a menu opens: name the states ("exploring", "combat", "boss", "menu") and the cues ("hit", "levelUp"), and call enter/trigger by name from then on.

    enter is idempotent by track, not by state name - two state names that happen to map to the same track will not refade into each other either, since there is nothing to refade to. Re-entering "combat" mid-fight (the same fight, the same track) must not reset the music, which is the one behaviour a direct Music.play call at every call site cannot give you for free: the orchestrator remembers the current track and treats entering an already-current one as a no-op.

    Cues are separate from states because they are fire-and-forget one-shots (Sound, not Music) - folding them into enter would conflate "this is where we are" with "this just happened".

    import { Orchestrator, Music, Sound } from '@datamoc/mw_games/audio';

    const orchestrator = new Orchestrator(new Music());
    orchestrator.define('exploring', { track: 'music/town.mp3' });
    orchestrator.define('combat', { track: 'music/battle.mp3', fadeDuration: 0.5 });
    orchestrator.on('levelUp', new Sound('sounds/level-up.mp3'));

    orchestrator.enter('combat'); // crossfades in
    orchestrator.enter('combat'); // already playing - a no-op, does not refade
    orchestrator.trigger('levelUp'); // fires the one-shot cue
    Index
    • switches to state's track, crossfading; a no-op if that track is already playing

      Parameters

      • state: string

      Returns void

    • plays the sound registered for event, if any; silently does nothing otherwise

      Parameters

      • event: string

      Returns void