Add ambience

This commit is contained in:
Victor 2015-12-09 10:55:10 +02:00
parent 4e5444f97d
commit 5337b544a9
3 changed files with 45 additions and 1 deletions

View File

@ -325,6 +325,7 @@ Parser.prototype.playSound = function() {
Parser.prototype.playAmbience = function() { Parser.prototype.playAmbience = function() {
var name = this.consumeMusicName(); var name = this.consumeMusicName();
var fade = this.matchFade(); var fade = this.matchFade();
ViewActivity.getInstance().ambience(name, fade);
return false; return false;
}; };
@ -357,7 +358,7 @@ Parser.prototype.stop = function() {
} }
if (this.match(TokenType.AMBIENCE)) { if (this.match(TokenType.AMBIENCE)) {
var fade = this.matchFade(); var fade = this.matchFade();
// ViewActivity.getInstance().stopAmbience(fade); ViewActivity.getInstance().stopAmbience(fade);
} }
return false; return false;
}; };

View File

@ -68,7 +68,17 @@ var PathResolver = new function () {
this.music = function (name) { this.music = function (name) {
return baseResDir + "music/" + name + ".ogg"; 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) { this.sound = function (name) {
if (name.startsWith("ambience_")) {
return this.ambience(name);
}
var path = baseResDir + "sfx/"; var path = baseResDir + "sfx/";
if (name.startsWith("sfx_")) path += name.substring(4); if (name.startsWith("sfx_")) path += name.substring(4);
else path += name; else path += name;

View File

@ -10,6 +10,7 @@ function Views(parser) {
this.musicPlayerAudio = new Audio(); this.musicPlayerAudio = new Audio();
this.soundPlayerAudio = new Audio(); this.soundPlayerAudio = new Audio();
this.ambiencePlayerAudio = new Audio();
this.musicQueue = new Array(); this.musicQueue = new Array();
this.soundQueue = new Array(); this.soundQueue = new Array();
@ -444,3 +445,35 @@ Views.prototype.stopSound = function (fade) {
this.soundPlayerAudio.pause(); 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();
}
};