-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhat-is-it.js
352 lines (296 loc) · 7.41 KB
/
what-is-it.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
var imageSceneConfig =
{
key: 'imagescene',
active: true,
preload: preloadImageScene,
create: createImageScene
}
var buttonSceneConfig =
{
key: 'buttonscene',
active: true,
create: createButtonScene
}
var wellDoneSceneConfig =
{
key: 'welldonescene',
active: true,
preload: preloadWellDoneScene,
create: createWellDoneScene
}
var badLuckSceneConfig =
{
key: 'badluckscene',
active: true,
preload: preloadBadLuckScene,
create: createBadLuckScene
}
var whiteSceneConfig =
{
key: 'whitescene',
active: true,
preload: preloadWhiteScene,
create: createWhiteScene
}
var blackSceneConfig =
{
key: 'blackscene',
active: true,
preload: preloadBlackScene,
create: createBlackScene
}
var config = {
type: Phaser.WEBGL,
parent: 'phaser-example',
width: 800,
height: 600,
scene: [ imageSceneConfig, whiteSceneConfig, blackSceneConfig, buttonSceneConfig, badLuckSceneConfig, wellDoneSceneConfig ]
};
var game = new Phaser.Game(config);
var button_names;
var right_answer;
var score;
var scoreText;
var last_inc;
var last_but_one_inc;
var lives;
var livesText;
var answerButtons;
var pic;
var background_image;
var whatWasItText;
var spotlight;
var probe;
var speckle;
function preloadImageScene ()
{
let names = next_image();
button_names = names[1][0];
right_answer = button_names[0];
button_names = shuffle(button_names);
if (this.textures.exists('pic_image')){
this.textures.remove('pic_image');
}
this.load.svg('pic_image', names[0]);
console.log(this.textures.exists('pic_image'), this.textures.exists('mask'))
if (! this.textures.exists('mask')){
this.load.image('mask', 'assets/sprites/ultrasound_beam.png')
this.load.image('probe', 'assets/sprites/probe.png')
this.load.spritesheet('speckle', 'assets/sprites/strong_speckle.png',
{ frameWidth: 80, frameHeight: 101 });
}
}
function createImageScene ()
{
pic = this.add.image(300, 300, 'pic_image');
var ultrasound_on = false;
if ( (score > 8) && (Math.random() >= 0.5) )
{
console.log("ultrasound on");
ultrasound_on = true;
var spotlight = this.make.sprite({
x: 500,
y: 500,
key: 'mask',
add: false
});
probe = this.make.sprite({
x: 500,
y: 570,
key: 'probe',
add: true
});
speckle = this.make.sprite({
x: 500,
y: 500,
key: 'speckle',
add: true
});
spotlight.setScale(3);
probe.setScale(2.5);
speckle.setScale(3);
if (! this.textures.exists('speckle_cycle'))
{
this.anims.create({
key: 'speckle_cycle',
frames: this.anims.generateFrameNumbers('speckle', { start: 0, end: 9 }),
frameRate: 10,
repeat: -1
});
}
pic.mask = new Phaser.Display.Masks.BitmapMask(this, spotlight);
this.input.on('pointermove', function (pointer) {
let x_pos = 550;
if ( pointer.x < 550 ){
x_pos = pointer.x;
}
let y_pos = pointer.y - 110
spotlight.x = x_pos;
spotlight.y = y_pos;
probe.x = x_pos;
probe.y = y_pos + 70;
speckle.x = x_pos;
speckle.y = y_pos;
speckle.anims.play('speckle_cycle', true)
});
}
else{
console.log("ultrasound off");
}
game.scene.bringToTop('imagescene');
}
function createButtonScene ()
{
score = 0;
last_but_one_inc = 0;
last_inc = 1;
lives = 3;
answerButtons = []
for ( var i = 0 ; i < button_names.length ; i++ ){
let button = new TextButton(this, 610, 10 + i * 40, button_names[i], { fill: '#0f0' }, checkanswer);
answerButtons.push(button);
this.add.existing(answerButtons[i]);
}
scoreText = this.add.text(610, 560, 'Score:' + score, { fill: '#0f0' });
livesText = this.add.text(610, 580, 'Lives: ' + lives, { fill: '#0f0' });
update_buttons();
}
function update_buttons()
{
for ( var i = 0 ; i < button_names.length ; i++ ){
answerButtons[i].setText(button_names[i]);
}
}
function preloadBlackScene ()
{
this.load.svg('pic_black', 'assets/backgrounds/black.svg');
}
function preloadWhiteScene ()
{
this.load.svg('pic_white', 'assets/backgrounds/white.svg');
}
function preloadWellDoneScene ()
{
this.load.image('picwd', 'assets/backgrounds/iconmonstr-smiley-600.gif');
console.log("preloaded well done scene");
}
function preloadBadLuckScene ()
{
this.load.image('picbl', 'assets/backgrounds/iconmonstr-frown-thin_600.gif');
console.log("preloaded bad luck scene");
}
function createWellDoneScene ()
{
let smiley = this.add.image(300, 300, 'picwd');
game.scene.sendToBack('welldonescene');
console.log("created well done scene");
}
function createBadLuckScene ()
{
this.add.image(300, 300, 'picbl');
whatWasItText = this.add.text(220, 300, 'n/a', { fill: '#000' });
console.log("created badluckscene");
game.scene.sendToBack('badluckscene');
}
function createBlackScene ()
{
this.add.image(300, 300, 'pic_black');
}
function createWhiteScene ()
{
let smiley = this.add.image(300, 300, 'pic_white');
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function on_success(this_game)
{
let new_inc = last_inc + last_but_one_inc;
last_but_one_inc = last_inc;
last_inc = new_inc;
score += new_inc;
scoreText.setText('Score: ' + score);
game.scene.run('welldonescene');
game.scene.bringToTop('welldonescene');
game.scene.stop('imagescene');
game.scene.remove('imagescene');
await sleep(1000);
game.scene.add('imagescene', imageSceneConfig, true);
game.scene.bringToTop('imagescene');
game.scene.stop('welldonescene');
update_buttons();
}
async function on_fail()
{
last_but_one_inc = 0;
last_inc = 1;
lives -= 1;
livesText.setText('Lives: ' + lives)
game.scene.run('badluckscene');
game.scene.stop('imagescene');
game.scene.remove('imagescene');
game.scene.bringToTop('badluckscene');
whatWasItText.setText('It was a ' + right_answer);
await sleep(1600);
if ( lives > 0 )
{
game.scene.stop('badluckscene');
game.scene.add('imagescene', imageSceneConfig, true);
game.scene.bringToTop('imagescene');
update_buttons();
}
else
{
whatWasItText.setText('Game Over! You scored ' + score);
game.scene.stop('buttonscene');
game.scene.stop();
}
}
function checkanswer (text)
{
console.log(text + " =? " + right_answer);
if ( text == right_answer )
{
on_success(this);
}
else
{
on_fail();
}
}
function shuffle(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function next_image() {
let [categories] = quickDraw.test();
let random_number = Math.floor(Math.random() * 200);
let prefix = ""
if (Math.random() >= 0.5 )
{
prefix = "assets/data/black_on_white/";
game.scene.moveAbove('whitescene', 'blackscene');
}
else
{
prefix = "assets/data/white_on_black/";
game.scene.moveAbove('blackscene', 'whitescene');
}
let filename = prefix + categories[0] + "_" + zfill(random_number, 4) + ".svg"
return [filename, [categories]];
}
function zfill(number, size) {
number = number.toString();
while (number.length < size) number = "0" + number;
return number;
}