@fuwu-yuan/bgew
Version:
Baguette Game Engine Web is a french 2D Web game engine
105 lines (104 loc) • 2.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Sound = void 0;
const howler_1 = require("howler");
class Sound {
/**
*
* @param name Sound name
* @param src One or more src (same sound with multiple src formats)
* @param repeat Ether to repeat of not the sound at the end
* @param volume Volume to play sound by default (default 0.5)
* @param sprites Object defining sprites in this sound (optional)
*/
constructor(name, src, repeat = false, volume = 0.5, sprites = {}) {
if (!Array.isArray(src)) {
src = [src];
}
this._howl = new howler_1.Howl({
src: src,
autoplay: false,
loop: repeat,
volume: volume,
sprite: sprites,
onend: () => { }
});
}
/**
* Play the sound
*
* @param repeat Ether to repeat the sound at the end
* @param volume Optional volume (default 0.5)
* @param sprite Optional sprite name if your sound is divided by sprites
* @return id The sound id. Can be used to stop sound
*/
play(repeat, volume, sprite) {
let id = this._howl.play(sprite);
if (typeof repeat !== 'undefined') {
this.repeat(repeat, id);
}
if (typeof volume !== 'undefined') {
this.volume(volume, id);
}
return id;
}
/**
* Stop the current sound
*
* @param fadeout Ether to fadeout the sound before stopping it
* @param fadeDuration The fade duration if fadeout
* @param id Optional id of the sound
*/
stop(fadeout = false, fadeDuration = 1000, id) {
let oldVolume = this._howl.volume();
this._howl.once("fade", soundId => {
this._howl.stop(soundId);
this._howl.volume(oldVolume);
}, id);
if (fadeout) {
this._howl.fade(this._howl.volume(), 0, fadeDuration, id);
}
else {
this._howl.stop(id);
}
}
/**
* Mute/Unmute the sound
*
* @param mute True to mute, False to unmute
* @param id Optional sound id
*/
mute(mute = true, id) {
this._howl.mute(mute, id);
}
/**
* Change sound volume
*
* @param volume New volume
* @param id Optional sound id
*/
volume(volume, id) {
if (id) {
this._howl.volume(volume, id);
}
else {
this._howl.volume(volume);
}
}
/**
* Loop the sound
*
* @param repeat Ether to repeat or not the sound at the end
* @param id Optional sound id
*/
repeat(repeat = true, id) {
this._howl.loop(repeat, id);
}
get howl() {
return this._howl;
}
set howl(value) {
this._howl = value;
}
}
exports.Sound = Sound;