From 5bd229869e3b8aed2e4857f86ba22a8f565e1e78 Mon Sep 17 00:00:00 2001 From: -=yb4f310 Date: Thu, 16 Nov 2017 19:34:35 +0100 Subject: [PATCH] check subscription before remove, respect audio load time --- src/game-lib-d3-particle-engine.js | 11 ++++++++++- src/game-lib-system-0.js | 5 +++++ src/game-lib-system-audio.js | 31 ++++++++++++++++++++++++++++-- src/game-lib-system-particle.js | 2 +- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/game-lib-d3-particle-engine.js b/src/game-lib-d3-particle-engine.js index f2f97cc..ef8efeb 100644 --- a/src/game-lib-d3-particle-engine.js +++ b/src/game-lib-d3-particle-engine.js @@ -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) ); diff --git a/src/game-lib-system-0.js b/src/game-lib-system-0.js index 90054aa..11f39dd 100644 --- a/src/game-lib-system-0.js +++ b/src/game-lib-system-0.js @@ -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 diff --git a/src/game-lib-system-audio.js b/src/game-lib-system-audio.js index 5bda769..c463a57 100644 --- a/src/game-lib-system-audio.js +++ b/src/game-lib-system-audio.js @@ -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) { diff --git a/src/game-lib-system-particle.js b/src/game-lib-system-particle.js index ce856d0..f3733cf 100644 --- a/src/game-lib-system-particle.js +++ b/src/game-lib-system-particle.js @@ -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);