avocado-old/packages/sound/traits/audible.trait.js
2019-04-20 16:02:41 -05:00

55 lines
866 B
JavaScript

import {Howl, Howler} from 'howler';
import {Trait} from '@avocado/entity';
export class Audible extends Trait {
static defaultParams() {
return {
sounds: {},
};
}
hydrate() {
this.loadSounds();
}
initialize() {
this._sounds = this.params.get('sounds').toJS();
this.sounds = {};
}
destroy() {
for (const key in this.sounds) {
const sound = this.sounds[key];
sound.unload();
}
}
loadSounds() {
for (const key in this._sounds) {
const soundJSON = this._sounds[key];
this.sounds[key] = new Howl(soundJSON);
}
}
methods() {
return {
hasSound: (key) => {
return !!this.sounds[key];
},
playSound: (key) => {
const sound = this.sounds[key];
if (!sound) {
return;
}
sound.play();
},
};
}
}