summaryrefslogtreecommitdiff
path: root/www/js/api.js
diff options
context:
space:
mode:
Diffstat (limited to 'www/js/api.js')
-rw-r--r--www/js/api.js125
1 files changed, 63 insertions, 62 deletions
diff --git a/www/js/api.js b/www/js/api.js
index 0043724..67c8320 100644
--- a/www/js/api.js
+++ b/www/js/api.js
@@ -12,25 +12,24 @@ window.html5ks.api = {
_fading: {},
- set_volume: function (target, delay, channel) {
+ set_volume: function (target, fade, channel) {
var audio = html5ks.elements.audio[channel],
- step = (target - audio.volume) / (delay * 20);
- if (!delay) {
- audio.volume = target;
- } else {
- this._fading[channel] = setInterval(function () {
- // clamp new volume 0-1
- audio.volume = Math.min(Math.max(audio.volume + step, 0), 1);
- switch (audio.volume) {
- case 0:
- audio.pause();
- /* falls through */
- case 1:
- clearInterval(this._fading[channel]);
- delete this._fading[channel];
- }
- }.bind(this), 50);
- }
+ step = (target - audio.volume) / (fade * 20);
+ var nextVol = function () {
+ // clamp new volume 0-1
+ audio.volume = Math.min(Math.max(audio.volume + step, 0), 1);
+ switch (audio.volume) {
+ case 0:
+ audio.pause();
+ /* falls through */
+ case 1:
+ clearInterval(this._fading[channel]);
+ delete this._fading[channel];
+ return false;
+ }
+ return true;
+ };
+ if (nextVol()) this._fading[channel] = setInterval(nextVol, 50);
return when.resolve();
},
@@ -77,7 +76,8 @@ window.html5ks.api = {
_nextType();
},
- play: function (channel, name, _, fade) {
+ play: function (cmd, args) {
+ var channel = cmd[1];
this.stop(channel);
var deferred = when.defer(),
audio = html5ks.elements.audio[channel];
@@ -85,13 +85,13 @@ window.html5ks.api = {
audio.loop = true;
}
html5ks.elements.audio[channel] = audio;
- html5ks.store[channel] = name;
+ html5ks.store[channel] = args.file;
var src = "dump/", volume;
switch (channel) {
case "music":
- var fname = html5ks.data.music[name];
+ var fname = html5ks.data.music[args.file.replace(/ $/, '')];
src += "bgm/" + fname;
volume = html5ks.persistent.musicVolume;
document.getElementById("current-music-track").innerHTML = fname.replace(/_/g, " ");
@@ -104,13 +104,13 @@ window.html5ks.api = {
audio.addEventListener("playing", function playing() {
audio.removeEventListener("playing", playing, false);
- if (fade) {
- html5ks.api.set_volume(volume, fade, channel);
+ if (args.fadein) {
+ html5ks.api.set_volume(volume, args.fadein, channel);
}
deferred.resolve();
}, false);
- audio.volume = fade ? 0 : volume;
+ audio.volume = args.fadein ? 0 : volume;
this._loadMedia(audio, src, [
['audio/ogg; codecs="opus"', "opus"],
['audio/ogg; codecs="vorbis"', "ogg"],
@@ -194,8 +194,8 @@ window.html5ks.api = {
real_target = html5ks.persistent.language + "_" + target.replace(/"/g, ''),
i = 0;
html5ks.fetch('script', real_target).then(function run(l) {
- if (l[i]) {
- html5ks.api.runInst(l[i++]).then(run, console.error);
+ if (l[i++]) {
+ html5ks.api.runInst(l[i]).then(function(){run(l)}, console.error);
} else {
deferred.resolve();
}
@@ -203,22 +203,28 @@ window.html5ks.api = {
return deferred.promise;
},
+ _safeCall: function (n, f, args) {
+ if (f) return f.apply(this, args);
+ else {
+ console.error("no such function " + n);
+ debugger;
+ return when.resolve();
+ }
+ },
+
runInst: function (inst) {
- var cmd = inst[0].replace(/"/g, ''),
- args = inst.slice(1);
- if (html5ks.data.characters[cmd]) {
- return this.say(cmd, args[0]);
- } else {
- if (this[cmd]) {
- return this[cmd].apply(this, args);
- } else if (inst.length === 1) {
- return this.say("name_only", cmd);
- } else {
- console.error("no such cmd " + cmd);
- return when.resolve();
- }
- }
+ var cmd = inst[0].replace(/"/g, '');
+ return this._safeCall(cmd, this[cmd], inst.slice(1));
+ },
+
+ UserStatement: function (line, parsed) {
+ var c = parsed[0][0];
+ return this._safeCall(c, this[c], parsed);
+ },
+
+ With: function () {
+ return when.resolve();
},
@@ -240,9 +246,9 @@ window.html5ks.api = {
},
- scene: function () {
+ Scene: function () {
html5ks.elements.show.innerHTML = "";
- return this.show.apply(this, arguments);
+ return this.Show.apply(this, arguments);
},
@@ -265,8 +271,11 @@ window.html5ks.api = {
bgright: { xpos: 0.6, xanchor: 0.5, ypos: 1.0, yanchor: 1.0 }
},
- show: function (name, type, location) {
+ Show: function (imspec) {
var deferred = when.defer();
+ var name =
+ console.log(imspec);
+ return when.resolve();
var lookup = document.getElementById(name),
el = lookup || document.createElement("img");
if (!location && !lookup) location = "center";
@@ -406,31 +415,27 @@ window.html5ks.api = {
});
},
- say: function (chrName, str, extend) {
+ Say: function (who, what, with_, extend) {
var deferred = when.defer(),
- chr = typeof chrName === "string" ? html5ks.data.characters[chrName] : chrName,
- w = /{w=?(\d*\.\d*)?}(.*)/.exec(str);
+ chr = html5ks.data.characters[who],
+ w = /{w=?(\d*\.\d*)?}(.*)/.exec(what);
if (!chr) {
- chr = {
- name: chrName
- };
- }
- if (typeof chr.what_prefix === "undefined") {
- chr.what_prefix = "“";
- chr.what_suffix = "”";
+ chr = { name: who };
}
+ chr.what_prefix = chr.what_prefix || "“";
+ chr.what_suffix = chr.what_suffix || "“";
this._lastchar = chr;
if (!extend && chr.what_prefix) {
- str = chr.what_prefix + str;
+ what = chr.what_prefix + what;
}
if ((!w || !w[1]) && chr.what_suffix) {
- str = str + chr.what_suffix;
+ what += chr.what_suffix;
}
- var text = this.dlgTag(str),
+ var text = this.dlgTag(what),
say, ctc;
if (chr.kind === "nvl") {
@@ -478,14 +483,14 @@ window.html5ks.api = {
ctc.style.display = "none";
deferred.resolve();
}
- html5ks.api._setNextTimeout(str, true);
+ html5ks.api._setNextTimeout(what, true);
};
if (html5ks.persistent.textSpeed == 200) {
ptxt(true);
}
- this._setNextTimeout(str, false);
+ this._setNextTimeout(what, false);
return deferred.promise;
},
@@ -501,10 +506,6 @@ window.html5ks.api = {
}
},
- extend: function (str) {
- return this.say(this._lastchar, str, true);
- },
-
Pause: function (duration) {
var deferred = when.defer();
setTimeout(function () {