146 lines
3.2 KiB
JavaScript
146 lines
3.2 KiB
JavaScript
|
/**
|
||
|
* Creates a Audio object
|
||
|
* @param graphics GameLib.D3.Graphics
|
||
|
* @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 = {};
|
||
|
}
|
||
|
|
||
|
if (apiAudio instanceof GameLib.D3.Audio) {
|
||
|
return apiAudio;
|
||
|
}
|
||
|
|
||
|
GameLib.D3.API.Audio.call(
|
||
|
this,
|
||
|
apiAudio.id,
|
||
|
apiAudio.name,
|
||
|
apiAudio.path,
|
||
|
apiAudio.loop,
|
||
|
apiAudio.volume,
|
||
|
apiAudio.camera,
|
||
|
apiAudio.parentEntity
|
||
|
);
|
||
|
|
||
|
if (this.camera instanceof GameLib.D3.API.Camera) {
|
||
|
this.camera = new GameLib.D3.Camera(
|
||
|
this.graphics,
|
||
|
this.camera
|
||
|
)
|
||
|
}
|
||
|
|
||
|
GameLib.Component.call(
|
||
|
this,
|
||
|
GameLib.Component.COMPONENT_AUDIO,
|
||
|
{
|
||
|
'camera' : GameLib.D3.Camera
|
||
|
}
|
||
|
);
|
||
|
|
||
|
};
|
||
|
|
||
|
GameLib.D3.Audio.prototype = Object.create(GameLib.D3.API.Audio.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,
|
||
|
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);
|
||
|
}
|
||
|
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 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),
|
||
|
GameLib.Utils.IdOrNull(this.parentEntity)
|
||
|
);
|
||
|
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* Converts from an Object Audio to a GameLib.D3.Audio
|
||
|
* @param graphics GameLib.D3.Graphics
|
||
|
* @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
|
||
|
);
|
||
|
|
||
|
};
|