summaryrefslogtreecommitdiff
path: root/www/js/html5ks.js
blob: 6c0234e37a3657b2ae05c929c1e1898f237171f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
(function () {
  "use strict";
  window.html5ks = {
    data: {
      script: {}
    },
    persistent: {
      seen_scenes: {},
      attraction: {
        kenji: 0,
        sc: 0,
        hanako: 0
      },
      hdisabled: false,
      settings: {
        // ms per character
        autospeed: 20,
        fade: 100,
        gotit: false,
        scale: true
      }
    },
    state: {
      auto: false
    },
    initElements: function () {
      this.elements = {
        container: document.getElementById("container"),
        video: document.getElementById("vid"),
        audio: {
          music: new Audio(),
          ambient: new Audio(),
          sound: new Audio()
        },
        who: document.getElementById("who"),
        say: document.getElementById("say"),
        bg: document.getElementById("bg"),
        window: document.getElementById("window")
      };
      this.elements.audio.music.loop = true;
      this.elements.audio.ambient.loop = true;
    },
    load: function () {
      if (localStorage.persistent) this.persistent = JSON.parse(localStorage.persistent);
    },
    save: function () {
      localStorage.persistent = JSON.stringify(this.persistent);
    },
    scale: function () {
      var newScale = 1;
      if (html5ks.persistent.settings.scale) {
        var height = document.documentElement.clientHeight,
            width = document.documentElement.clientWidth;
        if (height / width <= 0.75) { // widescreen
          newScale = height / 600;
        } else {
          newScale = width / 800;
        }
      } else {
        newScale = 1;
      }
      html5ks.elements.container.style.webkitTransform = "scale(" + newScale + ")";
      html5ks.elements.container.style.mozTransform = "scale(" + newScale + ")";
      html5ks.elements.container.style.transform = "scale(" + newScale + ")";
    },
    next: function () {},
    initEvents: function () {
      window.addEventListener("resize", html5ks.scale, false);
      this.elements.container.addEventListener("mouseup", function () {
        html5ks.next();
      }, false);
    },
    warnUnsupported: function () {
      if (!html5ks.persistent.settings.gotit) {
        if (!(/Firefox/.test(navigator.userAgent))) {
          document.getElementById("html-svg-filter").style.display = "block";
        }
        var warn = document.getElementById("warn-container");
        document.getElementById("gotit").addEventListener("mouseup", function () {
          warn.style.mozAnimation = "0.5s dissolveout";
          warn.style.webkitAnimation = "0.5s dissolveout";
          warn.style.animation = "0.5s dissolveout";
          warn.style.opacity = 0;
          html5ks.persistent.settings.gotit = true;
        }, false);
        var warns = document.getElementById("warns").children;
        for (var i = 0; i < warns.length; i++) {
          if (window.getComputedStyle(warns[i]).getPropertyValue("display") !== "none") {
            warn.style.visibility = "visible";
          }
        }
      }
    },
    onload: function () {
      this.initElements();
      this.load();
      this.scale();
      this.initEvents();
      this.warnUnsupported();
    },
    winload: function () {
      this.fetch("script", "a1-monday").then(function () {
        html5ks.api.movie_cutscene("4ls").then(function () {
          html5ks.api.iscene("en_NOP1")
        });
      });
    },
    fetch: function (type, name) {
      var deferred = when.defer();
      switch (type) {
        case "script":
          var script = html5ks.data.script;
          if (script[name]) {
            deferred.resolve();
          } else {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", "/scripts/script-" + name + ".json");
            xhr.onreadystatechange = function () {
              script[name] = true;
              if (xhr.readyState === 4) {
                var resp = JSON.parse(xhr.responseText);
                for (var label in resp) {
                  script[label] = resp[label];
                }
                deferred.resolve();
              }
            };
            xhr.send();
          }
          break;
        default:
          throw new Error("fetchtype " + type + " not implemented");
      }
      return deferred.promise;
    }
  };
  document.addEventListener("DOMContentLoaded", function () {
    html5ks.onload();
  }, false);
  window.addEventListener("load", function () {
    html5ks.winload();
  }, false);
}());