import SaveDataManager from './SaveDataManager' ; export default class SoundManager { private static instance : SoundManager = null ; private sound : Phaser . SoundManager ; private music : Phaser . Sound = null ; private audioInstances : { [ name ] : Phaser . Sound ; } = { } ; private constructor ( game ) { this . sound = game . sound ; } public static getInstance ( game ? ) { if ( null === SoundManager . instance ) { if ( ! game ) { throw new Error ( 'Cant create a new instance without a game' ) ; } SoundManager . instance = new SoundManager ( game ) ; } return SoundManager . instance ; } public play ( key , volume = 1 , loop = false ) { if ( ! SaveDataManager . getInstance ( ) . sfx ) { return null ; } if ( ! this . audioInstances . hasOwnProperty ( key ) ) { this . audioInstances [ key ] = this . sound . add ( key ) ; } this . audioInstances [ key ] . play ( undefined , undefined , volume , loop , true ) ; return this . audioInstances [ key ] ; } public stop ( key ) { if ( this . audioInstances . hasOwnProperty ( key ) ) { this . audioInstances [ key ] . stop ( ) ; } } public playMusic ( key ) { if ( ! SaveDataManager . getInstance ( ) . music ) { this . music = this . sound . play ( key , 1 , true ) ; this . music . stop ( ) ; return ; } if ( null === this . music || this . music . name !== key ) { if ( null !== this . music && this . music . name !== key ) { this . music . stop ( ) ; } this . music = this . sound . play ( key , 1 , true ) ; } } public fadeMusicVolume ( duration , volume ) { if ( this . music ) { this . music . fadeTo ( duration , volume ) ; } } public stopMusic ( ) { if ( null === this . music ) { return ; } if ( this . music . isPlaying ) { this . music . stop ( ) ; } } public toggleSfx ( ) { SaveDataManager . getInstance ( ) . sfx = ! SaveDataManager . getInstance ( ) . sfx ; } public toggleMusic ( ) { SaveDataManager . getInstance ( ) . music = ! SaveDataManager . getInstance ( ) . music ; if ( ! SaveDataManager . getInstance ( ) . music ) { if ( this . music && this . music . isPlaying ) { this . stopMusic ( ) ; } } else { if ( this . music ) { this . music . play ( undefined , undefined , 1 , true ) ; } } } }