Created
December 11, 2015 13:52
-
-
Save Purexo/988b6bb70261a8f7d18b to your computer and use it in GitHub Desktop.
Superpowers Abstraction Sound
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
namespace Sound { | |
export interface contructorSoundController { | |
MUSIC_PATH: string; | |
SOUND_PATH: string; | |
// musics['category']['nom'] = 'relative/path/from/MUSIC_PATH' | |
// tableau associatif de tableau associatif | |
musics: { | |
[index: string]: { | |
[index: string] : string | |
} | |
}; | |
// sounds['category']['nom'] = 'relative/path/from/MUSIC_PATH' | |
// tableau associatif de tableau associatif | |
sounds: { | |
[index: string]: { | |
[index: string] : string | |
} | |
}; | |
MusicVolume ? : number; | |
SoundVolume ? : number; | |
MasterVolume ? : number; | |
} | |
export class SoundController { | |
/* --- Properties --- */ | |
private MUSIC_PATH : string; | |
private SOUND_PATH : string; | |
private MusicVolume : number; // 0 - 100 | |
private SoundVolume : number; // 0 - 100 | |
private MasterVolume : number; // 0 - 100 | |
private Musics : { [index: string]: { [index: string] : Sup.Audio.SoundPlayer } }; | |
private Sounds : { [index: string]: { [index: string] : Sup.Audio.SoundPlayer } }; | |
/* --- Constructor --- */ | |
contructor (params : contructorSoundController) { | |
this.MUSIC_PATH = params.MUSIC_PATH; | |
this.SOUND_PATH = params.SOUND_PATH; | |
for (let category in params.musics) { | |
this.Musics[category] = {}; | |
for (let name in params.musics[category]) { | |
let relative_path = params.musics[category][name]; | |
this.Musics[category][name] = new Sup.Audio.SoundPlayer(Sup.get(this.MUSIC_PATH + relative_path, Sup.Sound)); | |
} | |
} | |
for (let category in params.sounds) { | |
this.Sounds[category] = {}; | |
for (let name in params.sounds[category]) { | |
let relative_path = params.sounds[category][name]; | |
this.Sounds[category][name] = new Sup.Audio.SoundPlayer(Sup.get(this.MUSIC_PATH + relative_path, Sup.Sound)); | |
} | |
} | |
Sup.Audio.setMasterVolume(this.MasterVolume / 500); | |
this.musicVolume = params.MusicVolume ? params.MusicVolume : 100; | |
this.soundVolume = params.SoundVolume ? params.SoundVolume : 100; | |
this.masterVolume = params.MasterVolume ? params.MasterVolume : 100; | |
} | |
/* --- Getter / Setter --- */ | |
get musicVolume() { return this.MusicVolume; } | |
get soundVolume() { return this.SoundVolume; } | |
get masterVolume() { return this.MasterVolume; } | |
set masterVolume(v : number) { | |
this.MasterVolume = Sup.Math.clamp(v, 0, 100); | |
Sup.Audio.setMasterVolume(this.MasterVolume / 500); | |
} | |
set soundVolume(v : number) { | |
this.SoundVolume = Sup.Math.clamp(v, 0, 100); | |
for (let category in this.Sounds) { | |
for (let name in this.Sounds[category]) { | |
this.Sounds[category][name].setVolume(this.MusicVolume / 500); | |
} | |
} | |
} | |
set musicVolume(v : number) { | |
this.MusicVolume = Sup.Math.clamp(v, 0, 100); | |
for (let category in this.Musics) { | |
for (let name in this.Musics[category]) { | |
this.Musics[category][name].setVolume(this.MusicVolume / 500); | |
} | |
} | |
} | |
/* --- Methods --- */ | |
randomMusic(category ? : string) { | |
if (category && this.Musics[category]) { | |
let tab : {category : string, name : string}[] = []; | |
for (let name in this.Musics[category]) { | |
tab.push({category, name}); | |
} | |
let r = Sup.Math.Random.integer(0, tab.length - 1); | |
this.playMusic(tab[r].category, tab[r].name); | |
return true; | |
} | |
let tab : {category : string, name : string}[] = []; | |
for (let category in this.Musics) { | |
for (let name in this.Musics[category]) { | |
tab.push({category, name}); | |
} | |
} | |
let r = Sup.Math.Random.integer(0, tab.length - 1); | |
this.playMusic(tab[r].category, tab[r].name); | |
return true; | |
} | |
playSound(category : string, name : string) { | |
return this.caller('Sounds', 'play', category, name); | |
} | |
playMusic(category : string, name : string) { | |
return this.caller('Musics', 'play', category, name); | |
} | |
stopSound(category : string, name : string) { | |
return this.caller('Sounds', 'stop', category, name); | |
} | |
stopMusic(category : string, name : string) { | |
return this.caller('Musics', 'stop', category, name); | |
} | |
pauseSound(category : string, name : string) { | |
return this.caller('Sounds', 'pause', category, name); | |
} | |
pauseMusic(category : string, name : string) { | |
return this.caller('Musics', 'pause', category, name); | |
} | |
isPlayingSound(category : string, name : string) { | |
return this.caller('Sounds', 'isPlaying', category, name); | |
} | |
isPlayingMusic(category : string, name : string) { | |
return this.caller('Musics', 'isPlaying', category, name); | |
} | |
getStateSound(category : string, name : string) { | |
return this.caller('Sounds', 'getState', category, name); | |
} | |
getStateMusic(category : string, name : string) { | |
return this.caller('Musics', 'getState', category, name); | |
} | |
getLoopSound(category : string, name : string) { | |
return this.caller('Sounds', 'getLoop', category, name); | |
} | |
getLoopMusic(category : string, name : string) { | |
return this.caller('Musics', 'getLoop', category, name); | |
} | |
setLoopSound(category : string, name : string, looping: boolean) { | |
return this.caller('Sounds', 'setLoop', category, name, looping); | |
} | |
setLoopMusic(category : string, name : string, looping: boolean) { | |
return this.caller('Musics', 'setLoop', category, name, looping); | |
} | |
getPanSound(category : string, name : string) { | |
return this.caller('Sounds', 'getPan', category, name); | |
} | |
getPanMusic(category : string, name : string) { | |
return this.caller('Musics', 'getPan', category, name); | |
} | |
setPanSound(category : string, name : string, pan: number) { | |
return this.caller('Sounds', 'setPan', category, name, pan); | |
} | |
setPanMusic(category : string, name : string, pan: number) { | |
return this.caller('Musics', 'setPan', category, name, pan); | |
} | |
getPitchSound(category : string, name : string) { | |
return this.caller('Sounds', 'getPitch', category, name); | |
} | |
getPitchMusic(category : string, name : string) { | |
return this.caller('Musics', 'getPitch', category, name); | |
} | |
setPitchSound(category : string, name : string, pitch: number) { | |
return this.caller('Sounds', 'setPitch', category, name, pitch); | |
} | |
setPitchMusic(category : string, name : string, pitch: number) { | |
return this.caller('Musics', 'setPitch', category, name, pitch); | |
} | |
// Va appeler si possible l'api Sup.Audio.SoundPlayer | |
private caller(type : string, funcname : string, category : string, name : string, param ? : number | boolean) : | |
Sup.Audio.SoundPlayer | boolean | Sup.Audio.SoundPlayer.State | |
{ | |
if (this[type][category]) { | |
if (this[type][category][name]) { | |
return this[type][category][name][funcname](param); | |
} | |
Sup.log(`name : "${name}" not found in category : "${category}"`); | |
return false; | |
} | |
Sup.log(`category : "${category}" not found`); | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment