feat: broadcasting sound

This commit is contained in:
cha0s 2021-02-04 17:57:27 -06:00
parent d98435b49a
commit c9b2988e1c
5 changed files with 33 additions and 9 deletions

View File

@ -21,6 +21,7 @@
"@avocado/resource": "^2.0.0",
"@avocado/traits": "^2.0.0",
"@latus/core": "^2.0.0",
"@latus/socket": "^2.0.0",
"autoprefixer": "^9.8.6",
"debug": "4.3.1",
"lru-cache": "^6.0.0"

View File

@ -8,5 +8,8 @@ export default {
'@avocado/traits': gatherWithLatus(
require.context('./traits', false, /\.js$/),
),
'@latus/socket/packets': gatherWithLatus(
require.context('./packets', false, /\.js$/),
),
},
};

View File

@ -0,0 +1,11 @@
import {Packet} from '@latus/socket';
export default () => class PlaySoundPacket extends Packet {
static get data() {
return {
sound: 'string',
};
}
};

View File

@ -2,11 +2,18 @@ import {Trait} from '@avocado/traits';
export default (latus) => class Audible extends Trait {
#playing = {};
#sounds = {};
static behaviorTypes() {
return {
};
acceptPacket(packet) {
if ('PlaySound' === packet.constructor.type) {
this.entity.playSound(packet.data.sound);
}
}
cleanPackets() {
this.#playing = {};
}
static defaultParams() {
@ -41,11 +48,6 @@ export default (latus) => class Audible extends Trait {
};
}
static describeParams() {
return {
};
}
destroy() {
Object.values(this.#sounds).forEach((sound) => {
sound.destroy();
@ -71,6 +73,9 @@ export default (latus) => class Audible extends Trait {
playSound: (key) => {
if (this.entity.hasSound(key)) {
this.#sounds[key].play();
if ('client' !== process.env.SIDE) {
this.#playing[key] = true;
}
}
},
@ -89,4 +94,8 @@ export default (latus) => class Audible extends Trait {
}));
}
packets() {
return Object.keys(this.#playing).map((key) => ['PlaySound', {sound: key}]);
}
};

View File

@ -1009,7 +1009,7 @@
react-hot-loader "^4.13.0"
sass-loader "^10.1.0"
"@latus/socket@2.0.0":
"@latus/socket@2.0.0", "@latus/socket@^2.0.0":
version "2.0.0"
resolved "http://npm.cha0sdev/@latus%2fsocket/-/socket-2.0.0.tgz#da384daea0f227a0fa239afedde31117a82dbb0c"
integrity sha512-BOnQSiFXpBMtTqLgdUAlyoxTlR4J0sKMnhSh1IVSzqYPeR/apvMAaadjApSyGPWLyl+sq5119ATg2YuWUnqc1g==