11import { clamp , normalize } from 'common/utils/math' ;
22import { debugDepth , isDebug } from 'systems/flags' ;
3+ import { runCallback , sequence , wait } from 'systems/sequence' ;
34
45export class SpatialAudio extends Phaser . GameObjects . Container {
56 public sound : Phaser . Sound . WebAudioSound | Phaser . Sound . HTML5AudioSound | Phaser . Sound . NoAudioSound ;
67
78 private graphics : Phaser . GameObjects . Graphics ;
89
10+ private isDestroyed = false ;
11+
912 constructor (
1013 scene : Phaser . Scene ,
1114 key : string ,
@@ -26,10 +29,6 @@ export class SpatialAudio extends Phaser.GameObjects.Container {
2629 this . graphics . setDepth ( debugDepth ( ) ) . fillStyle ( 0xff0000 , 1 ) . fillPoint ( d . translateX , d . translateY , 2 ) ;
2730 }
2831
29- if ( ! this . sound . isPlaying ) {
30- return ;
31- }
32-
3332 const camera = this . scene . cameras . main ;
3433
3534 const distanceTo = new Phaser . Math . Vector2 (
@@ -45,6 +44,8 @@ export class SpatialAudio extends Phaser.GameObjects.Container {
4544 }
4645
4746 public destroy ( fromScene ?: boolean ) : void {
47+ this . isDestroyed = true ;
48+
4849 this . sound . destroy ( ) ;
4950
5051 this . graphics . destroy ( ) ;
@@ -89,7 +90,17 @@ export class SpatialAudio extends Phaser.GameObjects.Container {
8990 }
9091
9192 public play ( ) {
92- this . sound . isPaused ? this . sound . resume ( ) : this . sound . play ( ) ;
93+ sequence ( this . scene )
94+ . of ( [
95+ wait ( 50 ) ,
96+ runCallback ( ( ) => {
97+ if ( ! this . isDestroyed ) {
98+ this . sound . isPaused ? this . sound . resume ( ) : this . sound . play ( ) ;
99+ }
100+ } ) ,
101+ ] )
102+ . start ( )
103+ . destroyWhenComplete ( ) ;
93104
94105 return this ;
95106 }
0 commit comments