check subscription before remove, respect audio load time

beta.r3js.org
-=yb4f310 2017-11-16 19:34:35 +01:00
parent 4870bc285a
commit 5bd229869e
4 changed files with 45 additions and 4 deletions

View File

@ -148,7 +148,12 @@ GameLib.D3.ParticleEngine.prototype.updateInstance = function(property) {
GameLib.D3.ParticleEngine.prototype.remove = function() {
GameLib.Event.Subscribe(
if (this.removeSubscription) {
console.log('already another remove subscription for ' + this.name);
return;
}
this.removeSubscription = GameLib.Event.Subscribe(
GameLib.Event.REMOVE_PARTICLE_ENGINE,
function(data){
if (data.component === this) {
@ -180,6 +185,10 @@ GameLib.D3.ParticleEngine.prototype.remove = function() {
} else {
GameLib.Component.prototype.remove.call(this);
}
this.removeSubscription.remove();
this.removeSubscription = null;
}
}.bind(this)
);

View File

@ -75,6 +75,11 @@ GameLib.System = function(
componentType = GameLib.Component.COMPONENT_SYSTEM_PARTICLE;
}
if (apiSystem.systemType === GameLib.System.SYSTEM_TYPE_AUDIO) {
componentType = GameLib.Component.COMPONENT_SYSTEM_AUDIO;
linkedObjects.audioComponents = [GameLib.D3.Audio];
}
GameLib.Component.call(
this,
componentType

View File

@ -22,6 +22,8 @@ GameLib.System.Audio = function(
this.stopAudioSubscription = null;
this.audioComponents = [];
this.toPlay = [];
};
GameLib.System.Audio.prototype = Object.create(GameLib.System.prototype);
@ -68,6 +70,17 @@ GameLib.System.Audio.prototype.start = function() {
GameLib.System.Audio.prototype.instanceCreated = function(data) {
if (data.component instanceof GameLib.D3.Audio) {
GameLib.Utils.PushUnique(this.audioComponents, data.component);
var index = this.toPlay.indexOf(data.component.name);
if (index !== -1) {
GameLib.Event.Emit(
GameLib.Event.PLAY_AUDIO,
{
name : data.component.name
}
)
}
}
};
@ -77,10 +90,18 @@ GameLib.System.Audio.prototype.instanceCreated = function(data) {
*/
GameLib.System.Audio.prototype.playAudio = function(data) {
var found = false;
this.audioComponents.map(
function(audio) {
if (audio.name === data.name) {
found = true;
if (!audio.instance) {
console.log('audio not ready yet');
}
if (audio.instance.isPlaying && audio.overplay) {
audio.instance.stop();
}
@ -102,9 +123,15 @@ GameLib.System.Audio.prototype.playAudio = function(data) {
}
}
}
)
);
if (!found) {
/**
* This audio still has to load
*/
console.log('delaying play until loaded for: ' + data.name);
this.toPlay.push(data.name);
}
};
GameLib.System.Audio.prototype.pauseAudio = function(data) {

View File

@ -94,7 +94,7 @@ GameLib.System.Particle.prototype.removeComponent = function(data) {
var index = this.particleEngines.indexOf(data.component);
if (index !== -1) {
console.log('removing particle engine from system');
console.log('removing particle engine from system' + data.component.name);
this.particleEngines.splice(index, 1);