/** * Creates a Audio object * @param graphics GameLib.GraphicsRuntime * @param apiAudio GameLib.D3.API.Audio * @constructor */ GameLib.D3.Audio = function( graphics, apiAudio ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (GameLib.Utils.UndefinedOrNull(apiAudio)) { apiAudio = {}; } GameLib.D3.API.Audio.call( this, apiAudio.id, apiAudio.name, apiAudio.path, apiAudio.loop, apiAudio.volume, apiAudio.camera, apiAudio.overplay, apiAudio.pause, apiAudio.parentEntity ); if (this.camera instanceof GameLib.D3.API.Camera) { this.camera = new GameLib.D3.Camera( this.graphics, this.camera ) } GameLib.Component.call( this, { 'camera' : GameLib.D3.Camera } ); }; GameLib.D3.Audio.prototype = Object.create(GameLib.Component.prototype); GameLib.D3.Audio.prototype.constructor = GameLib.D3.Audio; /** * Audio create instance */ GameLib.D3.Audio.prototype.createInstance = function() { if (GameLib.Utils.UndefinedOrNull(this.camera) || GameLib.Utils.UndefinedOrNull(this.camera.instance)) { console.log('audio not ready to create instance'); return; } GameLib.Event.Emit( GameLib.Event.GET_API_URL, null, function(data) { this.apiUrl = data.apiUrl; }.bind(this) ); //Create an AudioListener and add it to the camera var listener = new THREE.AudioListener(); this.camera.instance.add( listener ); // create a global audio source this.instance = new THREE.Audio( listener ); var audioLoader = new THREE.AudioLoader(); //Load a sound and set it as the Audio object's buffer audioLoader.load( this.apiUrl + this.path + '?ts=' + Date.now(), function( buffer ) { this.instance.setBuffer( buffer ); this.instance.setLoop( this.loop ); this.instance.setVolume( this.volume ); GameLib.Component.prototype.createInstance.call(this); }.bind(this) ); }; /** * Updates the instance with the current state */ GameLib.D3.Audio.prototype.updateInstance = function(property) { if (property === 'loop') { this.instance.setLoop(this.loop); } if (property === 'volume') { this.instance.setVolume(this.volume); } if (property === 'paused') { if (this.paused) { this.instance.pause(); } else { this.instance.play(); } } }; /** * Converts a GameLib.D3.Audio to a new GameLib.D3.API.Audio * @returns {GameLib.D3.API.Audio} */ GameLib.D3.Audio.prototype.toApiObject = function() { return new GameLib.D3.API.Audio( this.id, this.name, this.path, this.loop, this.volume, GameLib.Utils.IdOrNull(this.camera), this.overplay, this.paused, GameLib.Utils.IdOrNull(this.parentEntity) ); }; /** * Converts from an Object Audio to a GameLib.D3.Audio * @param graphics GameLib.GraphicsRuntime * @param objectAudio Object * @returns {GameLib.D3.Audio} * @constructor */ GameLib.D3.Audio.FromObject = function(graphics, objectAudio) { var apiAudio = GameLib.D3.API.Audio.FromObject(objectAudio); return new GameLib.D3.Audio( graphics, apiAudio ); }; GameLib.D3.Audio.prototype.play = function() { this.instance.play(); };