Kaynağa Gözat

enhancement #1839: Enhanced the sound module and component

tags/v0.10.4^2
Shaun 2 yıl önce
ebeveyn
işleme
d853eacef4
3 değiştirilmiş dosya ile 53 ekleme ve 3 silme
  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 Dosyayı Görüntüle

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


const { clientConfig: { sounds: loadSounds } } = globals; 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) { playSound: function (soundName) {
const soundEntry = this.sounds.find(s => s.name === soundName); const soundEntry = this.sounds.find(s => s.name === soundName);
if (!soundEntry) if (!soundEntry)
@@ -211,6 +230,13 @@ define([
addSound: function ( addSound: function (
{ name: soundName, scope, file, volume = 1, x, y, w, h, area, music, defaultMusic, autoLoad, loop } { 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) { if (!area && w) {
area = [ area = [
[x, y], [x, y],
@@ -243,6 +269,11 @@ define([
}; };


this.sounds.push(soundEntry); 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) { loadSound: function (file, loop = false, autoplay = false, volume = 1) {


+ 6
- 0
src/client/js/system/client.js Dosyayı Görüntüle

@@ -13,6 +13,12 @@ define([
transports: ['websocket'] 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('connect', this.onConnected.bind(this, onReady));
this.socket.on('handshake', this.onHandshake.bind(this)); this.socket.on('handshake', this.onHandshake.bind(this));
this.socket.on('event', this.onEvent.bind(this)); this.socket.on('event', this.onEvent.bind(this));


+ 16
- 3
src/server/clientComponents/sound.js Dosyayı Görüntüle

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


soundEntry: null,

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


@@ -26,10 +28,21 @@ define([
area, area,
music, music,
defaultMusic, 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();
} }
}; };
}); });

Yükleniyor…
İptal
Kaydet