Browse Source

enhancement #1839: Enhanced the sound module and component

tags/v0.10.4^2
Shaun 2 years ago
parent
commit
d853eacef4
3 changed files with 53 additions and 3 deletions
  1. +31
    -0
      src/client/js/sound/sound.js
  2. +6
    -0
      src/client/js/system/client.js
  3. +16
    -3
      src/server/clientComponents/sound.js

+ 31
- 0
src/client/js/sound/sound.js View File

@@ -32,6 +32,7 @@ define([
init: function () {
events.on('onToggleAudio', this.onToggleAudio.bind(this));
events.on('onPlaySound', this.playSound.bind(this));
events.on('onPlaySoundAtPosition', this.onPlaySoundAtPosition.bind(this));
events.on('onManipulateVolume', this.onManipulateVolume.bind(this));

const { clientConfig: { sounds: loadSounds } } = globals;
@@ -67,6 +68,24 @@ define([
}
},

onPlaySoundAtPosition: function ({ position: { x, y }, file, volume }) {
const { player: { x: playerX, y: playerY } } = window;
const dx = Math.abs(x - playerX);
const dy = Math.abs(y - playerY);
const distance = Math.max(dx, dy);

const useVolume = volume * (1 - (Math.pow(distance, 2) / Math.pow(minDistance, 2)));

//eslint-disable-next-line no-undef, no-unused-vars
const sound = new Howl({
src: [file],
volume: useVolume,
loop: false,
autoplay: true,
html5: false
});
},

playSound: function (soundName) {
const soundEntry = this.sounds.find(s => s.name === soundName);
if (!soundEntry)
@@ -211,6 +230,13 @@ define([
addSound: function (
{ name: soundName, scope, file, volume = 1, x, y, w, h, area, music, defaultMusic, autoLoad, loop }
) {
if (this.sounds.some(s => s.file === file)) {
if (window.player?.x !== undefined)
this.update(window.player.x, window.player.y);

return;
}

if (!area && w) {
area = [
[x, y],
@@ -243,6 +269,11 @@ define([
};

this.sounds.push(soundEntry);

if (window.player?.x !== undefined)
this.update(window.player.x, window.player.y);

return soundEntry;
},

loadSound: function (file, loop = false, autoplay = false, volume = 1) {


+ 6
- 0
src/client/js/system/client.js View File

@@ -13,6 +13,12 @@ define([
transports: ['websocket']
});

window.socket = this.socket;

/*this.socket = io('http://127.0.0.1:3000', {
transports: ['websocket']
});*/

this.socket.on('connect', this.onConnected.bind(this, onReady));
this.socket.on('handshake', this.onHandshake.bind(this));
this.socket.on('event', this.onEvent.bind(this));


+ 16
- 3
src/server/clientComponents/sound.js View File

@@ -9,9 +9,11 @@ define([
sound: null,
volume: 0,

soundEntry: null,

init: function () {
const {
sound, volume, music, defaultMusic,
sound, volume, music, defaultMusic, loop = true,
obj: { zoneId, x, y, width, height, area }
} = this;

@@ -26,10 +28,21 @@ define([
area,
music,
defaultMusic,
loop: true
loop
};

soundManager.addSound(config);
this.soundEntry = soundManager.addSound(config);
},

extend: function (bpt) {
Object.assign(this, bpt);

Object.assign(this.soundEntry, bpt);
},

destroy: function () {
if (this.soundEntry?.sound)
this.soundEntry?.sound.stop();
}
};
});

Loading…
Cancel
Save