From f893e01637716b1e078ae434b1bc71dae7fc6f5d Mon Sep 17 00:00:00 2001 From: Robert Long <robert@robertlong.me> Date: Fri, 9 Feb 2018 10:41:56 -0800 Subject: [PATCH] Update getNetworkedEntity. --- src/components/audio-feedback.js | 57 +++++++--------------------- src/systems/personal-space-bubble.js | 31 +++++++-------- 2 files changed, 29 insertions(+), 59 deletions(-) diff --git a/src/components/audio-feedback.js b/src/components/audio-feedback.js index 4f5a5c9e5..0e773f182 100644 --- a/src/components/audio-feedback.js +++ b/src/components/audio-feedback.js @@ -1,26 +1,10 @@ -const waitForConnected = function() { - return new Promise(resolve => { - NAF.clientId - ? resolve() - : document.body.addEventListener("connected", resolve); - }); -}; - AFRAME.registerComponent("networked-audio-analyser", { schema: {}, init() { - waitForConnected() - .then(() => { - const networkedEl = NAF.utils.getNetworkedEntity(this.el); - if (!networkedEl) { - return Promise.reject( - "Audio Analyzer must be added on a node, or a child of a node, with the `networked` component." - ); - } - const ownerId = networkedEl.components.networked.data.owner; - return NAF.connection.adapter.getMediaStream(ownerId); - }) - .then(stream => { + NAF.utils.getNetworkedEntity(this.el).then(networkedEl => { + const ownerId = networkedEl.components.networked.data.owner; + + NAF.connection.adapter.getMediaStream(ownerId).then(stream => { if (!stream) { return; } @@ -31,6 +15,7 @@ AFRAME.registerComponent("networked-audio-analyser", { this.levels = new Uint8Array(this.analyser.frequencyBinCount); source.connect(this.analyser); }); + }); }, tick: function() { @@ -38,8 +23,8 @@ AFRAME.registerComponent("networked-audio-analyser", { this.analyser.getByteFrequencyData(this.levels); - var sum = 0; - for (var i = 0; i < this.levels.length; i++) { + let sum = 0; + for (let i = 0; i < this.levels.length; i++) { sum += this.levels[i]; } this.volume = sum / this.levels.length; @@ -59,24 +44,16 @@ AFRAME.registerComponent("matcolor-audio-feedback", { this.onAudioFrequencyChange = this.onAudioFrequencyChange.bind(this); this.el.addEventListener("model-loaded", () => { - this.mat = this.el.object3D.getObjectByName( - this.data.objectName - ).material; + this.mat = this.el.object3D.getObjectByName(this.data.objectName).material; }); }, play() { - (this.data.analyserSrc || this.el).addEventListener( - "audioFrequencyChange", - this.onAudioFrequencyChange - ); + (this.data.analyserSrc || this.el).addEventListener("audioFrequencyChange", this.onAudioFrequencyChange); }, pause() { - (this.data.analyserSrc || this.el).removeEventListener( - "audioFrequencyChange", - this.onAudioFrequencyChange - ); + (this.data.analyserSrc || this.el).removeEventListener("audioFrequencyChange", this.onAudioFrequencyChange); }, onAudioFrequencyChange(e) { @@ -98,23 +75,15 @@ AFRAME.registerComponent("scale-audio-feedback", { }, play() { - (this.data.analyserSrc || this.el).addEventListener( - "audioFrequencyChange", - this.onAudioFrequencyChange - ); + (this.data.analyserSrc || this.el).addEventListener("audioFrequencyChange", this.onAudioFrequencyChange); }, pause() { - (this.data.analyserSrc || this.el).removeEventListener( - "audioFrequencyChange", - this.onAudioFrequencyChange - ); + (this.data.analyserSrc || this.el).removeEventListener("audioFrequencyChange", this.onAudioFrequencyChange); }, onAudioFrequencyChange(e) { const { minScale, maxScale } = this.data; - this.el.object3D.scale.setScalar( - minScale + (maxScale - minScale) * e.detail.volume / 255 - ); + this.el.object3D.scale.setScalar(minScale + (maxScale - minScale) * e.detail.volume / 255); } }); diff --git a/src/systems/personal-space-bubble.js b/src/systems/personal-space-bubble.js index 9087ec5a4..563818408 100644 --- a/src/systems/personal-space-bubble.js +++ b/src/systems/personal-space-bubble.js @@ -1,5 +1,5 @@ -var invaderPos = new AFRAME.THREE.Vector3(); -var bubblePos = new AFRAME.THREE.Vector3(); +const invaderPos = new AFRAME.THREE.Vector3(); +const bubblePos = new AFRAME.THREE.Vector3(); AFRAME.registerSystem("personal-space-bubble", { init() { @@ -12,7 +12,7 @@ AFRAME.registerSystem("personal-space-bubble", { }, unregisterBubble(el) { - var index = this.bubbles.indexOf(el); + const index = this.bubbles.indexOf(el); if (index !== -1) { this.bubbles.splice(index, 1); @@ -20,16 +20,17 @@ AFRAME.registerSystem("personal-space-bubble", { }, registerInvader(el) { - var networkedEl = NAF.utils.getNetworkedEntity(el); - var owner = NAF.utils.getNetworkOwner(networkedEl); + NAF.utils.getNetworkedEntity(el).then(networkedEl => { + const owner = NAF.utils.getNetworkOwner(networkedEl); - if (owner !== NAF.clientId) { - this.invaders.push(el); - } + if (owner !== NAF.clientId) { + this.invaders.push(el); + } + }); }, unregisterInvader(el) { - var index = this.invaders.indexOf(el); + const index = this.invaders.indexOf(el); if (index !== -1) { this.invaders.splice(index, 1); @@ -48,20 +49,20 @@ AFRAME.registerSystem("personal-space-bubble", { // Loop through all of the space bubbles (usually one) for (var i = 0; i < this.bubbles.length; i++) { - var bubble = this.bubbles[i]; + const bubble = this.bubbles[i]; bubblePos.setFromMatrixPosition(bubble.object3D.matrixWorld); - var radius = bubble.components["personal-space-bubble"].data.radius; - var radiusSquared = radius * radius; + const radius = bubble.components["personal-space-bubble"].data.radius; + const radiusSquared = radius * radius; // Hide the invader if inside the bubble - for (var j = 0; j < this.invaders.length; j++) { - var invader = this.invaders[j]; + for (let j = 0; j < this.invaders.length; j++) { + const invader = this.invaders[j]; invaderPos.setFromMatrixPosition(invader.object3D.matrixWorld); - var distanceSquared = bubblePos.distanceTo(invaderPos); + const distanceSquared = bubblePos.distanceTo(invaderPos); invader.object3D.visible = distanceSquared > radiusSquared; } -- GitLab