-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add background music, start work on music manager but there is lots o…
…f room for improvement
- Loading branch information
Showing
10 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { Scene, Sound, Tweens } from 'phaser'; | ||
|
||
export const INTRO_MUSIC = 'music-intro'; | ||
export const GAME_MUSIC = 'music-main'; | ||
|
||
export let Music: MusicManager; | ||
export function createMusicInstance(sound: Sound.BaseSoundManager) { | ||
Music = new MusicManager(sound); | ||
} | ||
|
||
type MusicKey = typeof INTRO_MUSIC | typeof GAME_MUSIC; | ||
|
||
class MusicManager { | ||
sound: Sound.BaseSoundManager; | ||
music?: Sound.BaseSound; | ||
volume = 0.5; | ||
|
||
scene: Scene; | ||
|
||
constructor(sound: Sound.BaseSoundManager) { | ||
this.sound = sound; | ||
|
||
sound.on('mute', (_soundManager: unknown, muted: boolean) => { | ||
if (this.music) { | ||
if (muted) fadeOutMusic(this.scene, this.music); | ||
else fadeInMusic(this.scene, this.music, this.volume); | ||
} | ||
}); | ||
|
||
sound.once('unlocked', () => { | ||
if (this.music && !this.music.isPlaying) { | ||
fadeInMusic(this.scene, this.music, this.volume); | ||
} | ||
}); | ||
} | ||
|
||
start(music: MusicKey, volume?: number) { | ||
if (this.music?.key === music && this.music?.isPlaying) return; | ||
|
||
this.stop(); | ||
this.volume = volume || this.volume; | ||
|
||
this.music = this.sound.get(music) || this.sound.add(music, { loop: true, volume: this.volume }); | ||
if (!this.sound.mute && !this.sound.locked) { | ||
fadeInMusic(this.scene, this.music, this.volume); | ||
} | ||
} | ||
|
||
stop() { | ||
if (this.music) fadeOutMusic(this.scene, this.music); | ||
this.music = undefined; | ||
} | ||
|
||
setScene(scene: Scene) { | ||
this.scene = scene; | ||
} | ||
} | ||
|
||
function fadeInMusic( | ||
scene: Scene | undefined, | ||
sound: Phaser.Sound.BaseSound, | ||
volume: number = 0.5, | ||
duration: number = 500 | ||
) { | ||
if (!scene) return; | ||
|
||
if (sound && !sound.pendingRemove) { | ||
sound.play({ volume: 0 }); | ||
scene.tweens.getTweensOf(sound).forEach((tween: Tweens.Tween) => tween.stop()); | ||
scene.tweens.add({ | ||
targets: sound, | ||
volume, | ||
duration, | ||
}); | ||
} | ||
} | ||
|
||
function fadeOutMusic(scene: Scene | undefined, sound: Phaser.Sound.BaseSound, duration: number = 500) { | ||
if (scene && sound.isPlaying) { | ||
scene.tweens.getTweensOf(sound).forEach((tween: Tweens.Tween) => tween.stop()); | ||
scene.tweens.add({ | ||
targets: sound, | ||
volume: 0, | ||
duration, | ||
onComplete: () => sound.stop(), | ||
}); | ||
} else { | ||
sound.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters