-
Notifications
You must be signed in to change notification settings - Fork 2
/
level.js
354 lines (269 loc) · 8.96 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*global radio */
import {BaseControl,
Palette,
TileControl,
PlayControl} from 'gui';
import layout from 'layout';
import {Editor} from 'editor';
import {ProgramView, TapeView, colorForSymbol} from 'view';
import {Tape} from 'core';
import {Interpreter} from 'interpreter';
class LevelDisplay extends BaseControl {
constructor(paper, x, y, width, height, level) {
super(paper, x, y);
this.width = width;
this.height = height;
this.level = level;
this.programView = null;
this._createControls();
}
_createControls() {
this.programView = new ProgramView(
this._layer,
layout.MARGIN, layout.MARGIN,
this.level.program,
layout.PROGRAM_WIDTH,
layout.PROGRAM_HEIGHT
);
}
}
export class LevelEditor extends LevelDisplay {
constructor(paper, x, y, width, height, level) {
super(paper, x, y, width, height, level);
this.palette = null;
this.editor = null;
this._createControls();
}
_createControls() {
super._createControls();
const CONTROL_WIDTH = this.width - layout.CONTROL_X;
this.palette = new Palette(
this._layer,
layout.CONTROL_X + CONTROL_WIDTH / 8,
this.height / 2,
CONTROL_WIDTH * 3 / 4,
4
);
this.tileControl = new TileControl(
this._layer,
layout.CONTROL_X + 40, // x
layout.MARGIN, // y
CONTROL_WIDTH / 2 - layout.MARGIN / 2, // width
0 // height
);
this.playControls = new PlayControl(
this._layer,
layout.CONTROL_X,
this.height - 68 - layout.MARGIN,
68
);
this.playControls.x = layout.CONTROL_X + CONTROL_WIDTH / 2 - this.playControls.width / 2;
this.editor = new Editor(this._layer, this.programView, this.tileControl);
this.programView.drawProgram();
this.editor.enable();
}
onVisible() {
super.onVisible();
radio('play-clicked').subscribe([this._onPlayClicked, this]);
}
onHidden() {
super.onHidden();
radio('play-clicked').unsubscribe(this._onPlayClicked);
}
_onPlayClicked() {
radio('editor:start-level').broadcast({level: this.level, sender: this});
}
};
class TestVectorProgression {
constructor(testCases) {
this.testCases = testCases;
this.index = 0;
}
skip() {
this.index++;
}
get current() {
if (this.index < this.testCases.length) {
return this.testCases[this.index];
} else {
return null;
}
}
}
export class LevelRunner extends LevelDisplay {
constructor(paper, x, y, width, height, level) {
super(paper, x, y, width, height, level);
this.tapeView = null;
this.playControls = null;
this.progression = new TestVectorProgression(this.level.testCases);
this.currentTest = null;
// Time between program iterations
this.stepTime = 500;
this._createControls();
}
_createControls() {
super._createControls();
const CONTROL_WIDTH = this.width - layout.CONTROL_X;
this.playControls = new PlayControl(
this._layer,
layout.CONTROL_X,
this.height - 68 - layout.MARGIN,
68
);
this.playControls.x = layout.CONTROL_X + CONTROL_WIDTH / 2 - this.playControls.width / 2;
this.tapeView = new TapeView(
this._layer,
layout.CONTROL_X,
layout.MARGIN,
CONTROL_WIDTH - 10,
(CONTROL_WIDTH - 10) / 10,
new Tape(),
Math.floor((this.height / 2 - layout.MARGIN) / ((CONTROL_WIDTH - 10) / 10))
);
this.tapeView.drawTape();
this.programView.drawProgram();
}
onVisible() {
super.onVisible();
radio('play-clicked').subscribe([this._onPlayClicked, this]);
radio('pause-clicked').subscribe([this._onPauseClicked, this]);
radio('stop-clicked').subscribe([this._onStopClicked, this]);
}
onHidden() {
super.onHidden();
radio('play-clicked').unsubscribe(this._onPlayClicked);
radio('pause-clicked').unsubscribe(this._onPauseClicked);
radio('stop-clicked').unsubscribe(this._onStopClicked);
}
_onPlayClicked() {
this.start();
}
_onPauseClicked() {
this.pause(true);
}
_onStopClicked() {
this.stop();
radio('runner:stop').broadcast({level: this.level, sender: this});
}
drawToken(mat, animate, callback) {
if (!this.token) {
this.token = this._layer.circle(0, 0, 10);
}
this._layer.append(this.token);
// make sure token is on top
let head = this.tapeView.tape.head(), fill;
if (head && head.symbol != 'empty') {
fill = colorForSymbol(head);
} else {
fill = '#E0E';
}
this.token.animate({ fill: fill }, this.stepTime / 2);
if (!animate) {
this.token.transform(mat);
} else {
this.token.animate({ transform: mat }, this.stepTime, mina.linear, () => {
//field.drawTape();
if (callback)
callback();
});
}
}
start() {
this.isRunning = true;
this.isPaused = false;
this.interpreter = new Interpreter();
this.currentTest = this.progression.current;
if (!this.currentTest)
return;
const currentTape = Tape.clone(this.currentTest.input);
this.tapeView.setTape(currentTape);
this.tapeView.drawTape();
this.interpreter.setProgram(this.level.program);
this.interpreter.setTape(currentTape);
this.interpreter.start();
this.update();
}
stop() {
this.isRunning = false;
this.isPaused = false;
this.token && this.token.remove();
}
pause(shouldPause) {
this.isPaused = shouldPause;
}
// Governor for state when game is running
// Responsibilities are:
// Determine if test case has been passed or failed
// Call run
update() {
const test = this.currentTest, int = this.interpreter;
if (this.isRunning) {
if (!int.running) {
// Interpreter has stopped
const finishedProperly = int.accept == test.accept,
correctOuput = test.output.symbols.length > 0 ?
Tape.isEqual(int.tape, test.output) : // compare if output not empty
true;
// otherwise ignore final tape
console.log('Test finished.');
console.log(finishedProperly && correctOuput ? 'Passed' : 'Failed');
if (finishedProperly && correctOuput) {
this.progression.skip();
let nextTest = this.progression.current;
// If there is another test to run, start it
if (nextTest !== null) {
window.requestAnimationFrame(() => this.start());
}
}
this.isRunning = false;
} else {
this._step();
}
}
}
run() {
// If we aren't running, set everything up and start the loop
if (this.isRunning) {
// We're running. See if the interpreter has stopped
if (this.interpreter.running) {
this._step();
} else {
console.log('Program stopped.');
console.log(`Accepted: ${ this.interpreter.accept }`);
this.isRunning = false;
}
}
}
// Calls interpreter's step and manages animation
_step() {
if (!this.isPaused) {
let oldPos = this.interpreter.position,
corner = this.exchange(
this.programView.gridView.getGlobalCellMatrix(oldPos.x, oldPos.y, false)
);
this.drawToken(corner);
this.interpreter.step();
let curPos = this.interpreter.position,
curCorner = this.exchange(
this.programView.gridView.getGlobalCellMatrix(curPos.x, curPos.y, false)
);
this.drawToken(curCorner, true, this.update.bind(this));
} else {
requestAnimationFrame(this.update.bind(this));
}
}
/**
Convert one coordinate system to another.
Converts from system with global matrix g to system with global matrix l
*/
exchange(g) {
return this._layer.transform().globalMatrix.invert().add(g);
}
};
export class Level {
constructor(title, program, testCases) {
this.title = title;
this.program = program;
this.testCases = testCases;
}
};