forked from Schnark/js13kgames-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel.js
113 lines (102 loc) · 3.25 KB
/
level.js
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
/*global Run, codeInput, display, editor*/
function Level (title, mapStr, onend) {
this.title = title;
this.mapStr = mapStr;
this.onend = onend;
this.abortButton = document.getElementById('abort-button');
this.runButton = document.getElementById('run-button');
this.cancelButton = document.getElementById('cancel-button');
this.resetButton = document.getElementById('reset-button');
this.pauseInput = document.getElementById('pause-input');
this.wrapper = document.getElementById('level-wrapper');
this.boundOnAbort = this.onAbort.bind(this);
this.boundOnStart = this.onStart.bind(this);
this.boundOnCancel = this.onCancel.bind(this);
this.boundOnReset = this.onReset.bind(this);
this.boundOnUpdatePause = this.onUpdatePause.bind(this);
}
Level.prototype.bind = function () {
this.abortButton.addEventListener('click', this.boundOnAbort);
this.runButton.addEventListener('click', this.boundOnStart);
this.cancelButton.addEventListener('click', this.boundOnCancel);
this.resetButton.addEventListener('click', this.boundOnReset);
this.pauseInput.addEventListener('change', this.boundOnUpdatePause);
};
Level.prototype.unbind = function () {
this.abortButton.removeEventListener('click', this.boundOnAbort);
this.runButton.removeEventListener('click', this.boundOnStart);
this.cancelButton.removeEventListener('click', this.boundOnCancel);
this.resetButton.removeEventListener('click', this.boundOnReset);
this.pauseInput.removeEventListener('change', this.boundOnUpdatePause);
};
Level.prototype.show = function () {
document.documentElement.scrollTop = 0;
display.title(this.title);
this.onReset();
this.bind();
this.wrapper.hidden = false;
this.abortButton.hidden = false;
codeInput.clear();
};
Level.prototype.showEditor = function () {
document.documentElement.scrollTop = 0;
display.title('Editor');
this.bind();
this.wrapper.hidden = false;
this.abortButton.hidden = false;
editor.show(this);
codeInput.disable();
};
Level.prototype.end = function (result) {
this.unbind();
this.onend(result, function () {
if (!this.title) {
editor.hide();
}
this.wrapper.hidden = true;
this.abortButton.hidden = true;
}.bind(this));
};
Level.prototype.onAbort = function () {
this.end(-1);
};
Level.prototype.onStart = function () {
var input = codeInput.get();
this.run = new Run(this.mapStr, input, function (done) {
this.cancelButton.disabled = true;
this.abortButton.disabled = false;
if (done) {
this.end(input.length);
} else {
this.resetButton.disabled = false;
this.resetButton.focus();
}
}.bind(this));
this.onUpdatePause();
codeInput.disable();
this.abortButton.disabled = true;
this.runButton.disabled = true;
this.cancelButton.disabled = false;
this.run.run();
};
Level.prototype.onCancel = function () {
this.run.cancel();
this.cancelButton.disabled = true;
this.abortButton.disabled = false;
};
Level.prototype.onReset = function () {
display.map(this.mapStr);
display.error('');
display.code('');
this.abortButton.disabled = false;
this.runButton.disabled = false;
this.cancelButton.disabled = true;
this.resetButton.disabled = true;
this.resetButton.blur();
codeInput.enable();
};
Level.prototype.onUpdatePause = function () {
if (this.run) {
this.run.setPause(250 * Math.pow(2, 4 - this.pauseInput.value / 2));
}
};