From 5337b544a9b35c7d77af86374eceb94c0222b4c7 Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 9 Dec 2015 10:55:10 +0200 Subject: [PATCH] Add ambience --- public_html/js/Parser.js | 3 ++- public_html/js/Utils.js | 10 ++++++++++ public_html/js/Views.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/public_html/js/Parser.js b/public_html/js/Parser.js index 624c520..ef5a8e9 100644 --- a/public_html/js/Parser.js +++ b/public_html/js/Parser.js @@ -325,6 +325,7 @@ Parser.prototype.playSound = function() { Parser.prototype.playAmbience = function() { var name = this.consumeMusicName(); var fade = this.matchFade(); + ViewActivity.getInstance().ambience(name, fade); return false; }; @@ -357,7 +358,7 @@ Parser.prototype.stop = function() { } if (this.match(TokenType.AMBIENCE)) { var fade = this.matchFade(); - // ViewActivity.getInstance().stopAmbience(fade); + ViewActivity.getInstance().stopAmbience(fade); } return false; }; diff --git a/public_html/js/Utils.js b/public_html/js/Utils.js index d84471d..0963ad5 100644 --- a/public_html/js/Utils.js +++ b/public_html/js/Utils.js @@ -68,7 +68,17 @@ var PathResolver = new function () { this.music = function (name) { return baseResDir + "music/" + name + ".ogg"; }; + this.ambience = function (name) { + var path = baseResDir + "ambience/"; + if (name.startsWith("ambience_")) path += name.substring(9); + else path += name; + path += ".ogg"; + return path; + }; this.sound = function (name) { + if (name.startsWith("ambience_")) { + return this.ambience(name); + } var path = baseResDir + "sfx/"; if (name.startsWith("sfx_")) path += name.substring(4); else path += name; diff --git a/public_html/js/Views.js b/public_html/js/Views.js index 85ae3db..e0f7c9e 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.ambiencePlayerAudio = new Audio(); this.musicQueue = new Array(); this.soundQueue = new Array(); @@ -443,4 +444,36 @@ Views.prototype.stopSound = function (fade) { } else { this.soundPlayerAudio.pause(); } +}; + +Views.prototype.ambience = function (name, fade) { + try { + this.stopAmbience(this.ambiencePlayerAudio.fade); + this.ambiencePlayerAudio.src = PathResolver.ambience(name); + this.ambiencePlayerAudio.fade = fade; + var views = this; + this.ambiencePlayerAudio.addEventListener('ended', function () { + this.currentTime = 0; + this.play(); + }); + this.ambiencePlayerAudio.play(); + if (fade.fadeIn) { + $(this.ambiencePlayerAudio).prop("volume", 0.0); + $(this.ambiencePlayerAudio).animate({volume: 1.0}, fade.duration * 1000); + } + } catch (e) { + console.log("ambience: " + name + " " + e); + } +}; + +Views.prototype.stopAmbience = function (fade) { + if (this.ambiencePlayerAudio.paused) return; + + if (fade.fadeOut) { + $(this.ambiencePlayerAudio).animate({volume: 0.0}, fade.duration * 1000, function() { + this.pause(); + }); + } else { + this.ambiencePlayerAudio.pause(); + } }; \ No newline at end of file