diff --git a/public_html/js/Parser.js b/public_html/js/Parser.js index 64bc355..281d3ca 100644 --- a/public_html/js/Parser.js +++ b/public_html/js/Parser.js @@ -70,7 +70,7 @@ Parser.prototype.statement = function() { if (this.match(token, TokenType.SCENE)) return this.scene(); if (this.match(token, TokenType.PLAY)) return this.play(); // if (this.match(token, TokenType.QUEUE)) return this.queue(); -// if (this.match(token, TokenType.STOP)) return this.stop(); + if (this.match(token, TokenType.STOP)) return this.stop(); if (this.match(token, TokenType.SHOW)) return this.show(); if (this.match(token, TokenType.HIDE)) return this.hide(); @@ -328,6 +328,22 @@ Parser.prototype.playAmbience = function() { return false; }; +Parser.prototype.stop = function() { + if (this.match(TokenType.MUSIC)) { + var fade = this.matchFade(); + ViewActivity.getInstance().stopMusic(fade); + } + if (this.match(TokenType.SOUND) || this.match(TokenType.SOUNDLOOP)) { + var fade = this.matchFade(); + // ViewActivity.getInstance().stopSound(fade); + } + if (this.match(TokenType.AMBIENCE)) { + var fade = this.matchFade(); + // ViewActivity.getInstance().stopAmbience(fade); + } + return false; +}; + Parser.prototype.menu = function() { // menu: title? this.consume(TokenType.COLON); diff --git a/public_html/js/Views.js b/public_html/js/Views.js index 416e4ae..51c5e2b 100644 --- a/public_html/js/Views.js +++ b/public_html/js/Views.js @@ -10,6 +10,7 @@ function Views(parser) { this.musicPlayerAudio = new Audio(); this.soundPlayerAudio = new Audio(); + this.musicQueue = new Array(); this.backgroundName = ""; this.backgroundType = ""; @@ -30,6 +31,8 @@ function Views(parser) { $('#menu').hide(); } +Views.prototype.NO_FADE = new FadeInfo(); + Views.prototype.getInstance = function () { return this; }; @@ -320,13 +323,38 @@ Views.prototype.showGameMenuDialog = function (menu) { }; Views.prototype.music = function (name, fade) { - // !musicPlayerAudio.paused; - this.musicPlayerAudio.src = PathResolver.music(name); - this.musicPlayerAudio.play(); - if (fade.fadeIn || fade.fadeOut) { - var startVolume = fade.fadeIn ? 0.0 : 1.0; - $(this.musicPlayerAudio).prop("volume", startVolume); - var targetVolume = fade.fadeIn ? 1.0 : 0.0; - $(this.musicPlayerAudio).animate({volume: targetVolume}, fade.duration * 1000); + try { + this.stopMusic(this.musicPlayerAudio.fade); + this.musicPlayerAudio.src = PathResolver.music(name); + this.musicPlayerAudio.fade = fade; + var views = this; + this.musicPlayerAudio.addEventListener('ended', function () { + if (views.musicQueue.length > 0) { + views.music(views.musicQueue.pop(), views.NO_FADE); + } else { + this.currentTime = 0; + this.play(); + } + }); + this.musicPlayerAudio.play(); + if (fade.fadeIn) { + $(this.musicPlayerAudio).prop("volume", 0.0); + $(this.musicPlayerAudio).animate({volume: 1.0}, fade.duration * 1000); + } + } catch (e) { + console.log("music: " + name + " " + e); } }; + +Views.prototype.stopMusic = function (fade) { + if (this.musicPlayerAudio.paused) return; + + if (fade.fadeOut) { + $(this.musicPlayerAudio).animate({volume: 0.0}, fade.duration * 1000, function() { + this.pause(); + }); + } else { + this.musicPlayerAudio.pause(); + } +}; +