From 738a90d6884ab580bec8d4af20fd792c809cdc1a Mon Sep 17 00:00:00 2001 From: Kevin Ngo Date: Tue, 2 Oct 2018 05:15:56 -0700 Subject: [PATCH] some memory savings in beat-loader.js --- src/components/beat-loader.js | 87 +++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 40 deletions(-) diff --git a/src/components/beat-loader.js b/src/components/beat-loader.js index 1fd55a9..c619bce 100644 --- a/src/components/beat-loader.js +++ b/src/components/beat-loader.js @@ -5,10 +5,10 @@ var utils = require('../utils'); */ AFRAME.registerComponent('beat-loader', { schema: { - challengeId: {type: 'string'}, - difficulty: {type: 'string'}, + beatAnticipationTime: {default: 2.0}, beatSpeed: {default: 4.0}, - beatAnticipationTime: {default: 2.0} + challengeId: {type: 'string'}, + difficulty: {type: 'string'} }, orientations: [180, 0, 270, 90, 225, 135, 315, 45, 0], @@ -55,6 +55,7 @@ AFRAME.registerComponent('beat-loader', { tick: function (time, delta) { var audioEl = this.el.components.song.audio; + var i; var notes; var obstacles; var lastTime = this.lastTime || 0; @@ -67,60 +68,68 @@ AFRAME.registerComponent('beat-loader', { obstacles = this.beatData._obstacles; this.bpm = this.beatData._beatsPerMinute; msPerBeat = 1000 * 60 / this.beatData._beatsPerMinute; - for (i=0; i < notes.length; ++i) { + for (i = 0; i < notes.length; ++i) { noteTime = notes[i]._time * msPerBeat; if (noteTime > lastTime && noteTime <= lastTime + delta) { notes[i].time = noteTime; this.generateBeat(notes[i]); - } + } } for (i=0; i < obstacles.length; ++i) { noteTime = obstacles[i]._time * msPerBeat; if (noteTime > lastTime && noteTime <= lastTime + delta) { // this.generateWall(obstacles[i]); - } + } } this.lastTime = lastTime + delta; // Sync audio with first element. if (this.audioSync || !this.first) { return; } - if (this.first.el.object3D.position.z < this.el.sceneEl.camera.el.object3D.position.z) { return; } + if (this.first.el.object3D.position.z < this.el.sceneEl.camera.el.object3D.position.z) { + return; + } this.audioSync = true; this.el.components.song.audio.currentTime = this.first.time; }, - generateBeat: function (noteInfo) { - var color; - var orientation; - var el; - var type = noteInfo._cutDirection === 8 ? 'dot' : 'arrow'; - color = noteInfo._type === 0 ? 'red' : 'blue'; - if (noteInfo._type === 3) { - type = 'mine'; - color = undefined; - } - el = this.requestBeat(type, color); - if (!el) { return; } - el.setAttribute('beat', { - color: color, - type: type, - speed: this.data.beatSpeed}); - el.object3D.position.set( - this.horizontalPositions[noteInfo._lineIndex], - this.verticalPositions[noteInfo._lineLayer], - -this.data.beatAnticipationTime * this.data.beatSpeed - ); - el.setAttribute('rotation', {x: 0, y: 0, z: this.orientations[noteInfo._cutDirection]}); - el.play(); - - if (this.first) { return; } - this.first = { - el: el, - time: noteInfo._time + generateBeat: (function () { + const beatObj = {}; + + return function (noteInfo) { + var color; + var orientation; + var el; + var type = noteInfo._cutDirection === 8 ? 'dot' : 'arrow'; + color = noteInfo._type === 0 ? 'red' : 'blue'; + if (noteInfo._type === 3) { + type = 'mine'; + color = undefined; + } + el = this.requestBeat(type, color); + if (!el) { return; } + + beatObj.color = color; + beatObj.type = type; + beatObj.speed = this.data.beatSpeed; + el.setAttribute('beat', beatObj); + el.object3D.position.set( + this.horizontalPositions[noteInfo._lineIndex], + this.verticalPositions[noteInfo._lineLayer], + -this.data.beatAnticipationTime * this.data.beatSpeed + ); + el.object3D.rotation.z = THREE.Math.degToRad(this.orientations[noteInfo._cutDirection]); + el.play(); + + if (this.first) { return; } + + this.first = { + el: el, + time: noteInfo._time + }; }; - }, + })(), // generateWall: function (wallInfo) { // var el = this.el.sceneEl.components.pool__wall.requestEntity(); @@ -144,11 +153,11 @@ AFRAME.registerComponent('beat-loader', { // time: wallInfo._time // }; // }, - + requestBeat: function (type, color) { var beatPoolName = 'pool__beat-' + type; var pool; - if (color) {beatPoolName += '-' + color; } + if (color) {beatPoolName += '-' + color; } pool = this.el.sceneEl.components[beatPoolName]; if (!pool) { console.warn('Poo ' + beatPoolName + ' unavailable'); @@ -167,5 +176,3 @@ function updateQueryParam(uri, key, value) { return uri + separator + key + '=' + value; } } - -