2017-01-26 23:52:45 +00:00
|
|
|
module TS.SpaceTac.View {
|
2015-04-22 21:42:40 +00:00
|
|
|
// Utility functions for sounds
|
|
|
|
export class Audio {
|
|
|
|
|
|
|
|
private game: Phaser.Game;
|
|
|
|
|
|
|
|
private music: Phaser.Sound;
|
|
|
|
|
|
|
|
constructor(game: Phaser.Game) {
|
|
|
|
this.game = game;
|
|
|
|
this.music = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the sound system is up and running
|
|
|
|
isActive(): boolean {
|
|
|
|
return this.game.sound.context ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Play a ponctual sound
|
|
|
|
playOnce(key: string): void {
|
|
|
|
if (this.isActive()) {
|
|
|
|
this.game.sound.play(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start a background music
|
|
|
|
startMusic(key: string): void {
|
2015-04-26 19:38:04 +00:00
|
|
|
key = "music-" + key;
|
2015-04-22 21:42:40 +00:00
|
|
|
if (this.isActive()) {
|
|
|
|
if (this.music && this.music.key !== key) {
|
|
|
|
this.stopMusic();
|
|
|
|
}
|
|
|
|
if (!this.music) {
|
2015-04-26 19:38:04 +00:00
|
|
|
this.music = this.game.sound.play(key, 1, true);
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop currently playing background music
|
|
|
|
stopMusic(): void {
|
|
|
|
if (this.isActive()) {
|
|
|
|
if (this.music) {
|
|
|
|
this.music.stop();
|
|
|
|
this.music = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle the mute status of the sound system
|
|
|
|
toggleMute(): void {
|
|
|
|
if (this.isActive()) {
|
|
|
|
if (this.game.sound.volume > 0) {
|
|
|
|
this.game.sound.volume = 0;
|
|
|
|
} else {
|
|
|
|
this.game.sound.volume = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|