import { AudioController, Sound } from "dsg-engine"; import { SoundProps } from './types'; export class SoundController { public static readonly SOUND_TYPE = "slot_machine"; constructor( private _props: SoundProps ) { AudioController.SetSoundOptions(_props.spinningSound.name, _props.spinningSound.url, { loop: true, type: SoundController.SOUND_TYPE, fadeOutDuration: 0, }); AudioController.SetSoundOptions(_props.reelStopSound.name, _props.reelStopSound.url, { loop: false, type: SoundController.SOUND_TYPE, }); if (_props.anticipationSound) { AudioController.SetSoundOptions(_props.anticipationSound.name, _props.anticipationSound.url, { loop: false, type: SoundController.SOUND_TYPE, }); } } private _spinningSoundInstance: Sound | undefined = undefined; // private _stopSoundTypes: number[] = []; private _visibilityEventListenerActive = false; // private calculateStopSoundTypes(symbols: GameSymbol[][]): number[] { // let scatterCounter = 0; // let bonusCounter = 0; // const numbers: number[] = []; // symbols.forEach((reel, reelIndex) => { // let bonusSymbolFound = false; // let scatterSymbolFound = false; // reel.forEach((symbol, symbolIndex) => { // if (symbol === GameSymbol.Scatter) { // scatterCounter++; // scatterSymbolFound = true; // } else if (symbol === GameSymbol.Bonus) { // bonusCounter++; // bonusSymbolFound = true; // } // }); // numbers[ reelIndex ] = number.Normal; // if (scatterSymbolFound) { // if (scatterCounter === 1) { // numbers[ reelIndex ] = number.Bonus; // } else if (scatterCounter >= 2) { // numbers[ reelIndex ] = number.Bonus2; // } // } // if (bonusSymbolFound) { // if (bonusCounter === 1) { // numbers[ reelIndex ] = number.Bonus; // } else if (bonusCounter >= 2) { // numbers[ reelIndex ] = number.Bonus2; // } // } // }); // return numbers; // } // public updateSymbols(symbols: GameSymbol[][]) { // this._stopSoundTypes = this.calculateStopSoundTypes(symbols); // } public playStopSound(reelIndex: number) { AudioController.PlaySound(this._props.reelStopSound.name); // switch (this._stopSoundTypes[ reelIndex ]) { // case number.Normal: // AudioController.PlaySound('stop_reel'); // break; // case number.Bonus: // AudioController.PlaySound('stop_reel_bonus_1'); // break; // case number.Bonus2: // AudioController.PlaySound('stop_reel_bonus_2'); // break; // }; } public playStopSoundOnForceStop() { AudioController.PlaySound(this._props.reelStopSound.name); } public playAnticipationSound() { if (this._props.anticipationSound) { AudioController.PlaySound(this._props.anticipationSound.name); } } /** * Start playing spinning sfx on infinite loop. * Mute if window becomes inactive. */ public playSpinningSfx() { this._spinningSoundInstance ??= AudioController.PlaySound(this._props.spinningSound.name); if (!this._visibilityEventListenerActive) { this._visibilityEventListenerActive = true; document.addEventListener("visibilitychange", event => { if (this._spinningSoundInstance) { if (document.visibilityState == "visible") { AudioController.MuteSound(this._spinningSoundInstance.name, false); } else { AudioController.MuteSound(this._spinningSoundInstance.name, true); } } }) } } public stopSpinningSfx() { AudioController.StopSound(this._props.spinningSound.name); this._spinningSoundInstance = undefined; } }