2017-09-24 22:23:22 +00:00
|
|
|
module TK.SpaceTac.UI {
|
2018-05-15 14:57:45 +00:00
|
|
|
class AudioSettings {
|
|
|
|
main_volume = 1
|
|
|
|
music_volume = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions to play sounds and musics
|
|
|
|
*/
|
2015-04-22 21:42:40 +00:00
|
|
|
export class Audio {
|
2018-05-15 14:57:45 +00:00
|
|
|
private static SETTINGS = new AudioSettings();
|
|
|
|
private music: Phaser.Sound.BaseSound | undefined
|
2017-06-08 17:32:57 +00:00
|
|
|
private music_playing_volume = 1
|
2015-04-22 21:42:40 +00:00
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
constructor(private view: BaseView | null) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the sound system is active, and return a manager to operate with it
|
|
|
|
*/
|
|
|
|
private getManager(): Phaser.Sound.BaseSoundManager | null {
|
|
|
|
if (this.view) {
|
|
|
|
return this.view.sound;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
/**
|
|
|
|
* Check if an audio key is present in cache
|
|
|
|
*/
|
|
|
|
hasCache(key: string): boolean {
|
|
|
|
return this.view ? this.view.cache.audio.has(key) : false;
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
/**
|
|
|
|
* Play a single sound effect (fire-and-forget)
|
|
|
|
*/
|
2018-06-10 22:58:42 +00:00
|
|
|
playOnce(key: string, speed = 1): void {
|
|
|
|
if (speed != 1) {
|
|
|
|
// TODO
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
let manager = this.getManager();
|
|
|
|
if (manager) {
|
|
|
|
if (this.hasCache(key)) {
|
|
|
|
manager.play(key);
|
|
|
|
} else {
|
|
|
|
console.warn("Missing sound", key);
|
|
|
|
}
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
/**
|
|
|
|
* Start a background music in repeat
|
|
|
|
*/
|
2017-06-07 17:08:53 +00:00
|
|
|
startMusic(key: string, volume = 1): void {
|
2018-05-15 14:57:45 +00:00
|
|
|
let manager = this.getManager();
|
|
|
|
if (manager) {
|
2017-07-20 23:09:17 +00:00
|
|
|
this.stopMusic();
|
2018-05-15 14:57:45 +00:00
|
|
|
|
2015-04-22 21:42:40 +00:00
|
|
|
if (!this.music) {
|
2018-05-15 14:57:45 +00:00
|
|
|
key = "music-" + key;
|
|
|
|
if (this.hasCache(key)) {
|
|
|
|
this.music_playing_volume = volume;
|
|
|
|
this.music = manager.add(key, {
|
|
|
|
volume: volume * Audio.SETTINGS.music_volume,
|
|
|
|
loop: true
|
|
|
|
});
|
|
|
|
this.music.play();
|
|
|
|
} else {
|
|
|
|
console.warn("Missing music", key);
|
|
|
|
}
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-15 14:57:45 +00:00
|
|
|
/**
|
|
|
|
* Stop currently playing background music
|
|
|
|
*/
|
2015-04-22 21:42:40 +00:00
|
|
|
stopMusic(): void {
|
2018-05-15 14:57:45 +00:00
|
|
|
let music = this.music;
|
|
|
|
if (music) {
|
|
|
|
music.stop();
|
|
|
|
music.destroy();
|
|
|
|
this.music = undefined;
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-08 17:32:57 +00:00
|
|
|
/**
|
|
|
|
* Get the main volume (0-1)
|
|
|
|
*/
|
|
|
|
getMainVolume(): number {
|
2018-05-15 14:57:45 +00:00
|
|
|
return Audio.SETTINGS.main_volume;
|
2017-06-08 17:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the main volume (0-1)
|
|
|
|
*/
|
|
|
|
setMainVolume(value: number) {
|
2018-05-15 14:57:45 +00:00
|
|
|
Audio.SETTINGS.main_volume = clamp(value, 0, 1);
|
|
|
|
|
|
|
|
let manager = this.getManager();
|
|
|
|
if (manager) {
|
|
|
|
manager.volume = Audio.SETTINGS.main_volume;
|
2017-06-08 17:32:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the music volume (0-1)
|
|
|
|
*/
|
|
|
|
getMusicVolume(): number {
|
2018-05-15 14:57:45 +00:00
|
|
|
return Audio.SETTINGS.music_volume;
|
2017-06-08 17:32:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the music volume (0-1)
|
|
|
|
*/
|
|
|
|
setMusicVolume(value: number) {
|
2018-05-15 14:57:45 +00:00
|
|
|
Audio.SETTINGS.music_volume = clamp(value, 0, 1);
|
|
|
|
|
|
|
|
let music = this.music;
|
|
|
|
if (music) {
|
|
|
|
// TODO Set music volume
|
|
|
|
if (value) {
|
|
|
|
music.resume();
|
|
|
|
} else {
|
|
|
|
music.pause();
|
2015-04-22 21:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|