audio components

beta.r3js.org
-=yb4f310 2017-11-15 23:42:03 +01:00
parent a695a2541f
commit f2fa1e0be5
6 changed files with 258 additions and 177 deletions

View File

@ -35,11 +35,11 @@ GameLib.Event.ANIMATE_TEXTURE_INSTANCE = 0x11;
GameLib.Event.REMOVE_PARTICLE_ENGINE = 0x12;
GameLib.Event.PAUSE = 0x13;
GameLib.Event.TEXTURE_INSTANCE_UPDATED = 0x14;
//GameLib.Event.MATERIAL_INSTANCE_CREATED = 0x15;
GameLib.Event.PLAY_AUDIO = 0x15;
GameLib.Event.MATERIAL_INSTANCE_UPDATED = 0x16;
//GameLib.Event.MESH_INSTANCE_CREATED = 0x17;
GameLib.Event.PAUSE_AUDIO = 0x17;
GameLib.Event.MESH_INSTANCE_UPDATED = 0x18;
//GameLib.Event.LIGHT_INSTANCE_CREATED = 0x19;
GameLib.Event.STOP_AUDIO = 0x19;
GameLib.Event.LIGHT_INSTANCE_UPDATED = 0x1a;
GameLib.Event.DELETE_COMPONENT = 0x1b;
GameLib.Event.COMPONENT_DOWNLOAD_COMPLETE = 0x1c;
@ -142,11 +142,11 @@ GameLib.Event.GetEventName = function(number) {
case 0x12 : return 'remove_particle_engine';
case 0x13 : return 'pause';
case 0x14 : return 'texture_instance_updated';
case 0x15 : return 'unused';//'material_instance_created';
case 0x15 : return 'play_audio';
case 0x16 : return 'material_instance_updated';
case 0x17 : return 'unused';//'mesh_instance_created';
case 0x17 : return 'pause_audio';
case 0x18 : return 'mesh_instance_updated';
case 0x19 : return 'unused';//'light_instance_created';
case 0x19 : return 'stop_audio';
case 0x1a : return 'light_instance_updated';
case 0x1b : return 'delete_component';
case 0x1c : return 'component_download_complete';

View File

@ -290,6 +290,8 @@ GameLib.Component.COMPONENT_MESH_LINE = 0x51;
GameLib.Component.COMPONENT_PARTICLE_ENGINE = 0x52;
GameLib.Component.COMPONENT_SYSTEM_PARTICLE = 0x53;
GameLib.Component.COMPONENT_PARTICLE = 0x54;
GameLib.Component.COMPONENT_AUDIO = 0x55;
GameLib.Component.COMPONENT_SYSTEM_AUDIO = 0x56;
/**
* Returns string name for component number
@ -377,6 +379,8 @@ GameLib.Component.GetComponentName = function(number) {
case 0x52 : return 'GameLib.D3.ParticleEngine';
case 0x53 : return 'GameLib.D3.System.Particle';
case 0x54 : return 'GameLib.D3.Particle';
case 0x55 : return 'GameLib.D3.Audio';
case 0x56 : return 'GameLib.D3.System.Audio';
break;
}

View File

@ -1,51 +1,21 @@
/**
* Raw Particle API object - should always correspond with the Particle Schema
* Raw Audio API object - should always correspond with the Audio Schema
* @param id
* @param name
* @param lifeTime
* @param elapsed
* @param mesh
* @param opacityType
* @param opacityFactor
* @param positionOffsetType
* @param positionOffset
* @param positionOffsetFn
* @param directionType
* @param rotation
* @param scale
* @param direction
* @param directionFn
* @param speed
* @param scaleFn
* @param scaleType
* @param rotationType
* @param rotationFn
* @param parentEngine
* @param path
* @param loop
* @param volume
* @param camera
* @param parentEntity
* @constructor
*/
GameLib.D3.API.Particle = function(
GameLib.D3.API.Audio = function(
id,
name,
lifeTime,
elapsed,
mesh,
opacityType,
opacityFactor,
positionOffsetType,
positionOffset,
positionOffsetFn,
directionType,
direction,
directionFn,
speed,
scaleType,
scale,
scaleFn,
rotationType,
rotation,
rotationFn,
parentEngine,
path,
loop,
volume,
camera,
parentEntity
) {
@ -55,104 +25,29 @@ GameLib.D3.API.Particle = function(
this.id = id;
if (GameLib.Utils.UndefinedOrNull(name)) {
name = 'Particle (' + this.id + ')';
name = 'Audio (' + this.id + ')';
}
this.name = name;
if (GameLib.Utils.UndefinedOrNull(lifeTime)) {
lifeTime = 10;
if (GameLib.Utils.UndefinedOrNull(path)) {
path = '';
}
this.lifeTime = lifeTime;
this.path = path;
if (GameLib.Utils.UndefinedOrNull(elapsed)) {
elapsed = 0;
if (GameLib.Utils.UndefinedOrNull(loop)) {
loop = false;
}
this.elapsed = elapsed;
this.loop = loop;
if (GameLib.Utils.UndefinedOrNull(mesh)) {
mesh = null;
if (GameLib.Utils.UndefinedOrNull(volume)) {
volume = 0.5;
}
this.mesh = mesh;
this.volume = volume;
if (GameLib.Utils.UndefinedOrNull(opacityType)) {
opacityType = GameLib.D3.Particle.OPACITY_TYPE_CONSTANT;
if (GameLib.Utils.UndefinedOrNull(camera)) {
camera = null;
}
this.opacityType = opacityType;
if (GameLib.Utils.UndefinedOrNull(opacityFactor)) {
opacityFactor = 0.01;
}
this.opacityFactor = opacityFactor;
if (GameLib.Utils.UndefinedOrNull(positionOffsetType)) {
positionOffsetType = GameLib.D3.Particle.POSITION_OFFSET_TYPE_CONSTANT;
}
this.positionOffsetType = positionOffsetType;
if (GameLib.Utils.UndefinedOrNull(positionOffset)) {
positionOffset = new GameLib.API.Vector3(0, 0, 0);
}
this.positionOffset = positionOffset;
if (GameLib.Utils.UndefinedOrNull(positionOffsetFn)) {
positionOffsetFn = '//@ sourceURL=positionOffsetFn.js';
}
this.positionOffsetFn = positionOffsetFn;
if (GameLib.Utils.UndefinedOrNull(directionType)) {
directionType = GameLib.D3.Particle.DIRECTION_TYPE_CONSTANT;
}
this.directionType = directionType;
if (GameLib.Utils.UndefinedOrNull(direction)) {
direction = new GameLib.API.Vector3(0, 1, 0);
}
this.direction = direction;
if (GameLib.Utils.UndefinedOrNull(directionFn)) {
directionFn = '//@ sourceURL=directionFn.js';
}
this.directionFn = directionFn;
if (GameLib.Utils.UndefinedOrNull(speed)) {
speed = 1;
}
this.speed = speed;
if (GameLib.Utils.UndefinedOrNull(scaleType)) {
scaleType = GameLib.D3.Particle.SCALE_TYPE_CONSTANT;
}
this.scaleType = scaleType;
if (GameLib.Utils.UndefinedOrNull(scale)) {
scale = new GameLib.API.Vector3(1, 1, 1);
}
this.scale = scale;
if (GameLib.Utils.UndefinedOrNull(scaleFn)) {
scaleFn = '//@ sourceURL=scaleFn.js';
}
this.scaleFn = scaleFn;
if (GameLib.Utils.UndefinedOrNull(rotationType)) {
rotationType = GameLib.D3.Particle.ROTATION_TYPE_CONSTANT;
}
this.rotationType = rotationType;
if (GameLib.Utils.UndefinedOrNull(rotation)) {
rotation = new GameLib.API.Vector3(0, 0, 0);
}
this.rotation = rotation;
if (GameLib.Utils.UndefinedOrNull(rotationFn)) {
rotationFn = '//@ sourceURL=rotationFn.js';
}
this.rotationFn = rotationFn;
if (GameLib.Utils.UndefinedOrNull(parentEngine)) {
parentEngine = null;
}
this.parentEngine = parentEngine;
this.camera = camera;
if (GameLib.Utils.UndefinedOrNull(parentEntity)) {
parentEntity = null;
@ -160,48 +55,33 @@ GameLib.D3.API.Particle = function(
this.parentEntity = parentEntity;
};
GameLib.D3.API.Particle.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Particle.prototype.constructor = GameLib.D3.API.Particle;
GameLib.D3.API.Audio.prototype = Object.create(GameLib.Component.prototype);
GameLib.D3.API.Audio.prototype.constructor = GameLib.D3.API.Audio;
/**
* Creates an API Particle from an Object Particle
* @param objectParticle
* Creates an API Audio from an Object Audio
* @param objectAudio
* @constructor
*/
GameLib.D3.API.Particle.FromObject = function(objectParticle) {
GameLib.D3.API.Audio.FromObject = function(objectAudio) {
var apiMesh = null;
if (objectParticle.mesh) {
if (objectParticle.mesh instanceof Object) {
apiMesh = GameLib.D3.API.Mesh.FromObject(objectParticle.mesh);
var apiCamera = null;
if (objectAudio.camera) {
if (objectAudio.camera instanceof Object) {
apiCamera = GameLib.D3.API.Camera.FromObject(objectAudio.camera);
} else {
apiMesh = objectParticle.mesh;
apiCamera = objectAudio.camera;
}
}
return new GameLib.D3.API.Particle(
objectParticle.id,
objectParticle.name,
objectParticle.lifeTime,
objectParticle.elapsed,
apiMesh,
objectParticle.opacityType,
objectParticle.opacityFactor,
objectParticle.positionOffsetType,
GameLib.API.Vector3.FromObject(objectParticle.positionOffset),
objectParticle.positionOffsetFn,
objectParticle.directionType,
GameLib.API.Vector3.FromObject(objectParticle.direction),
objectParticle.directionFn,
objectParticle.speed,
objectParticle.scaleType,
GameLib.API.Vector3.FromObject(objectParticle.scale),
objectParticle.scaleFn,
objectParticle.rotationType,
GameLib.API.Vector3.FromObject(objectParticle.rotation),
objectParticle.rotationFn,
objectParticle.parentEngine,
objectParticle.parentEntity
return new GameLib.D3.API.Audio(
objectAudio.id,
objectAudio.name,
objectAudio.path,
objectAudio.loop,
objectAudio.volume,
apiCamera,
objectAudio.parentEntity
);
};

145
src/game-lib-d3-audio.js Normal file
View File

@ -0,0 +1,145 @@
/**
* 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
);
};

View File

@ -96,6 +96,7 @@ GameLib.System.SYSTEM_TYPE_LINKING = 0x40;
GameLib.System.SYSTEM_TYPE_CUSTOM = 0x80;
GameLib.System.SYSTEM_TYPE_VISUALIZATION = 0x100;
GameLib.System.SYSTEM_TYPE_PARTICLE = 0x200;
GameLib.System.SYSTEM_TYPE_AUDIO = 0x400;
GameLib.System.SYSTEM_TYPE_ALL = 0xFFFF;
GameLib.System.prototype.createInstance = function() {

View File

@ -1,18 +1,11 @@
/**
* System takes care of updating all the entities (based on their component data)
* @param graphics
* @param camera
* @param apiSystem GameLib.API.System
* @constructor
*/
GameLib.System.Audio = function(
graphics,
camera,
apiSystem
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
GameLib.System.call(
this,
apiSystem
@ -22,8 +15,13 @@ GameLib.System.Audio = function(
this.removeComponentSubscription = null;
this.beforeRenderSubscription = null;
this.playAudioSubscription = null;
this.pauseAudioSubscription = null;
this.stopAudioSubscription = null;
this.audioComponents = [];
};
GameLib.System.Audio.prototype = Object.create(GameLib.System.prototype);
@ -46,6 +44,21 @@ GameLib.System.Audio.prototype.start = function() {
this.removeComponent.bind(this)
);
this.playAudioSubscription = GameLib.Event.Subscribe(
GameLib.Event.PLAY_AUDIO,
this.playAudio.bind(this)
);
this.pauseAudioSubscription = GameLib.Event.Subscribe(
GameLib.Event.PAUSE_AUDIO,
this.pauseAudio.bind(this)
);
this.stopAudioSubscription = GameLib.Event.Subscribe(
GameLib.Event.STOP_AUDIO,
this.stopAudio.bind(this)
);
};
/**
@ -53,18 +66,53 @@ GameLib.System.Audio.prototype.start = function() {
* @param data
*/
GameLib.System.Audio.prototype.instanceCreated = function(data) {
if (data.component instanceof GameLib.D3.Audio) {
GameLib.Utils.PushUnique(this.audioComponents, data.component);
}
};
/**
* Removes a particle engine from this system
* @param data
*/
GameLib.System.Audio.prototype.removeComponent = function(data) {
GameLib.System.Audio.prototype.playAudio = function(data) {
this.audioComponents.map(
function(audio) {
if (audio.name === data.name) {
if (!audio.instance.isPlaying) {
audio.instance.play();
}
}
}
)
};
GameLib.System.Audio.prototype.pauseAudio = function(data) {
};
GameLib.System.Audio.prototype.stopAudio = function(data) {
this.audioComponents.map(
function(audio) {
if (audio.name === data.name) {
if (audio.instance.isPlaying) {
audio.instance.stop();
}
}
}
)
};
GameLib.System.Audio.prototype.removeComponent = function(data) {
};
/**
* Stop this system (remove all event listeners)
*/
@ -74,5 +122,8 @@ GameLib.System.Audio.prototype.stop = function() {
this.instanceCreatedSubscription.remove();
this.removeComponentSubscription.remove();
this.playAudioSubscription.remove();
this.pauseAudioSubscription.remove();
this.stopAudioSubscription.remove();
};