-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdentidoo.js
More file actions
779 lines (731 loc) · 21.9 KB
/
dentidoo.js
File metadata and controls
779 lines (731 loc) · 21.9 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// dentidoo, the dentist's favourite game !
// IDEAs:
// - shortcuts from one cell to another
// - dentist symbol: heals all caries at once
// ***********************************************
// global variables
// ***********************************************
var DEBUG = true; // display debug info
// Board definition; levels are defined in companion file "levels.js"
var BOARD_EMPTY = ".";
var BOARD_WALL = "#";
var BOARD_SPRING = "£";
var BOARD_CLOUD = "*";
var BOARD_START = "S";
var BOARD_END = "E";
var BOARD_CANDY = "c";
var BOARD_CHOCOLATE = "h";
var BOARD_LOLLIPOP = "l";
var BOARD_TOOTHBRUSH = "T";
var BOARD_GIFT = "g";
var Z_SYMBOLS = 0; // background
var Z_TEETH = 1; // in front of symbols
var Z_CLOUD = 2; // in front of teeth
var GRID_SIZE = 50; // size in pixels
var GRID_DIMENSION = 10; // 10 * 10
var CANVAS_MARGIN = 10; // left, right, bottom margin
var CANVAS_HEADER_HEIGHT = 60;
var CANVAS_FOOTER_HEIGTH = 160; // for logo and debug info and buttons
var CANVAS_WIDTH = CANVAS_MARGIN + GRID_SIZE * GRID_DIMENSION + CANVAS_MARGIN;
var CANVAS_HEIGHT =
CANVAS_HEADER_HEIGHT +
GRID_SIZE * GRID_DIMENSION +
CANVAS_MARGIN +
CANVAS_FOOTER_HEIGTH;
var SCORE_INCREMENT = 20;
var SCORE_NOMINAL_TIME = 20 * 1000; // time to get SCORE_INCREMENT [milliseconds]
const MOVE_REQUEST = {
NONE: "none",
LEFT: "left",
RIGHT: "right",
UP: "up",
DOWN: "down",
};
var doShowIntro = true;
var doBindGlobalEvents = true;
var level = 0;
var score = 0;
var numMoves = 0;
var arrowKeysPressed = 0;
var moveRequest = MOVE_REQUEST.NONE;
var stopped = false;
var entities = Create2DArray(GRID_DIMENSION);
var scoreText;
var levelText;
var levelNameText;
var doActions = []; // list of actions (for undo), each item is one move
var doAction = {}; // dictionary of action(s) for each entity Id for one move (for undo)
// ***********************************************
// init Crafty
// ***********************************************
var viewportScale = calculateScalingFactor(available_width, available_height);
Crafty.init(CANVAS_WIDTH * viewportScale, CANVAS_HEIGHT * viewportScale); // uses by default 'cr-stage' div
Crafty.sprite(
50, // tile
50, // tileh
"tooth.png",
{
tooth_0: [0, 0], // clean
tooth_1: [1, 0],
tooth_2: [2, 0],
tooth_3: [3, 0],
tooth_4: [4, 0],
tooth_5: [5, 0], // broken
}
);
Crafty.sprite(
50, // tile
50, // tileh
"symbols.png",
{
ball_grey: [0, 0], // empty board cell
ball_green: [1, 0], // exit unlocked
ball_red: [2, 0], // exit locked
chocolate: [3, 0],
candy: [4, 0],
lollipop: [5, 0],
toothbrush: [6, 0],
wall: [7, 0],
spring: [0, 1],
cloud: [1, 1],
gift: [2, 1],
}
);
Crafty.sprite(
100, // tile
100, // tileh
"buttons.png",
{
button_left: [0, 0],
button_up: [1, 0],
button_down: [2, 0],
button_right: [3, 0],
button_restart: [4, 0],
}
);
// ***********************************************
// functions
// ***********************************************
// calculate viewport scaling factor
function calculateScalingFactor(availableWidth, availableHeight){
stageWidth = CANVAS_WIDTH;
stageHeight = CANVAS_HEIGHT;
stageAspectRatio = stageWidth/stageHeight;
availableAspectRatio = availableWidth/availableHeight;
if (stageAspectRatio < availableAspectRatio){
// fit on height: the stage will occupy the full window's height
scaleFactor = availableHeight / stageHeight;
} else {
// fit on width: the stage will occupy the full window's width
scaleFactor = availableWidth / stageWidth;
}
return scaleFactor;
}
// resize and scale the viewport upon browser window resize
function resizeViewport(){
viewportScale = calculateScalingFactor(available_width, available_height);
Crafty.viewport.width = CANVAS_WIDTH * viewportScale;
Crafty.viewport.height = CANVAS_HEIGHT * viewportScale;
Crafty.viewport.scale(viewportScale);
}
// create 2D array
function Create2DArray(rows) {
var arr = [];
for (var i = 0; i < rows; i++) {
arr[i] = [];
}
return arr;
}
// convert i in grid to x pixel position
function pos_x(i) {
return CANVAS_MARGIN + GRID_SIZE * i;
}
// convert x pixel position to i grid position
function pos_i(x) {
return (x - CANVAS_MARGIN) / GRID_SIZE;
}
// convert j in grid to y pixel position
function pos_y(j) {
return CANVAS_HEADER_HEIGHT + GRID_SIZE * j;
}
// convert y pixel position to j grid position
function pos_j(y) {
return (y - CANVAS_HEADER_HEIGHT) / GRID_SIZE;
}
// show a message
function showMessage(aText) {
Crafty.e("2D, Canvas, Text, Keyboard")
.attr({
x: CANVAS_WIDTH / 2 - 150 + 20,
y: 300,
w: 260,
})
.textFont({
size: "40px",
})
.text(aText);
}
// draw header
function drawHeader() {
scoreText = Crafty.e("2D, Canvas, Text, Persists")
.attr({
x: CANVAS_WIDTH - 80,
y: 10,
w: 50,
})
.textFont({
size: "25px",
})
.text(function () {
return score.toString();
})
.dynamicTextGeneration(true);
levelText = Crafty.e("2D, Canvas, Text")
.attr({
x: CANVAS_MARGIN + 10,
y: 10,
w: 50,
})
.textFont({
size: "25px",
})
.text(function () {
return (level + 1).toString();
})
.dynamicTextGeneration(false);
levelNameText = Crafty.e("2D, Canvas, Text")
.attr({
x: CANVAS_MARGIN + 60,
y: 10,
w: 400,
})
.textFont({
size: "25px",
})
.text(function () {
return levels[level]["name"];
})
.dynamicTextGeneration(false);
}
// draw footer
function drawFooter() {
var logo = Crafty.e("2D, Canvas, Image")
.image("logo.png")
.attr({
x: CANVAS_WIDTH - 175,
y: CANVAS_HEIGHT - CANVAS_FOOTER_HEIGTH,
});
if (DEBUG) {
Crafty.e("2D, Canvas, Text")
.attr({
x: CANVAS_MARGIN,
y: CANVAS_HEIGHT - CANVAS_FOOTER_HEIGTH + 20,
})
.text(function () {
return "numMoves:" + numMoves;
})
.dynamicTextGeneration(true)
.textColor("#FF0000");
}
if (DEBUG && 0) {
Crafty.e("2D, Canvas, Text")
.attr({
x: 100,
y: CANVAS_HEIGHT - CANVAS_FOOTER_HEIGTH + 20,
})
.text(function () {
return "arrowKeysPressed:" + arrowKeysPressed;
})
.dynamicTextGeneration(true)
.textColor("#FF0000");
}
// buttons
buttons_h = 100;
buttons_y = CANVAS_HEIGHT - buttons_h;
Crafty.e("2D, Canvas, Mouse, button_left")
.attr({ x: CANVAS_MARGIN, y: buttons_y })
.bind("Click", function (MouseEvent) {
if (!stopped) {
moveRequest = MOVE_REQUEST.LEFT;
}
});
Crafty.e("2D, Canvas, Mouse, button_up")
.attr({ x: CANVAS_MARGIN + buttons_h, y: buttons_y })
.bind("Click", function (MouseEvent) {
if (!stopped) {
moveRequest = MOVE_REQUEST.UP;
}
});
Crafty.e("2D, Canvas, Mouse, button_down")
.attr({ x: CANVAS_MARGIN + buttons_h * 2, y: buttons_y })
.bind("Click", function (MouseEvent) {
if (!stopped) {
moveRequest = MOVE_REQUEST.DOWN;
}
});
Crafty.e("2D, Canvas, Mouse, button_right")
.attr({ x: CANVAS_MARGIN + buttons_h * 3, y: buttons_y })
.bind("Click", function (MouseEvent) {
if (!stopped) {
moveRequest = MOVE_REQUEST.RIGHT;
}
});
Crafty.e("2D, Canvas, Mouse, button_restart")
.attr({ x: CANVAS_MARGIN + buttons_h * 4, y: buttons_y })
.bind("Click", function (MouseEvent) {
// restart this level; (same as keyboard key R)
stopped = false;
Crafty.scene("main"); // restart the scene
});
}
// draw board (populate entities)
function drawBoard() {
for (let j = 0; j < GRID_DIMENSION; j++) {
// j = y
for (let i = 0; i < GRID_DIMENSION; i++) {
// i = x
element = levels[level]["grid"][j].charAt(i);
switch (element) {
case BOARD_EMPTY:
default:
entities[i][j] = Crafty.e("2D, Canvas, ball_grey");
break;
case BOARD_WALL:
entities[i][j] = Crafty.e("2D, Canvas, wall");
break;
case BOARD_SPRING:
entities[i][j] = Crafty.e("2D, Canvas, spring");
break;
case BOARD_CLOUD:
entities[i][j] = Crafty.e("2D, Canvas, cloud").attr({
z: Z_CLOUD,
});
break;
case BOARD_START:
// put a grey ball
entities[i][j] = Crafty.e("2D, Canvas, ball_grey");
// and place one tooth in front of it
Crafty.e("2D, Canvas, tooth, tooth_0").attr({
x: pos_x(i),
y: pos_y(j),
z: Z_TEETH, // in front of symbols which are at Z=0
});
break;
case BOARD_END:
entities[i][j] = Crafty.e("2D, Canvas, ball_green"); // initially the exit is unlocked
break;
case BOARD_CANDY:
entities[i][j] = Crafty.e("2D, Canvas, candy");
break;
case BOARD_CHOCOLATE:
entities[i][j] = Crafty.e("2D, Canvas, chocolate");
break;
case BOARD_LOLLIPOP:
entities[i][j] = Crafty.e("2D, Canvas, lollipop");
break;
case BOARD_TOOTHBRUSH:
entities[i][j] = Crafty.e("2D, Canvas, toothbrush");
break;
case BOARD_GIFT:
entities[i][j] = Crafty.e("2D, Canvas, gift");
break;
}
entities[i][j].attr({
x: pos_x(i),
y: pos_y(j),
});
}
}
}
// check if position is inside the board
function isInBoard(ax, ay) {
return ax >= 0 && ax < GRID_DIMENSION && ay >= 0 && ay < GRID_DIMENSION;
}
// return destination after moving by some steps in one direction
function moveOneStep(ax, ay, aMoveRequest, aStep) {
switch (aMoveRequest) {
case MOVE_REQUEST.LEFT:
ax -= aStep;
break;
case MOVE_REQUEST.RIGHT:
ax += aStep;
break;
case MOVE_REQUEST.UP:
ay -= aStep;
break;
case MOVE_REQUEST.DOWN:
ay += aStep;
break;
}
return [ax, ay];
}
// hop on spring(s), starting from position ax,ay occupied by a spring
function hopOnSprings(ax, ay, aMoveRequest) {
while (isInBoard(ax, ay) && entities[ax][ay].has("spring")) {
// jump 2 positions in the requested direction
[ax, ay] = moveOneStep(ax, ay, aMoveRequest, 2);
}
return [ax, ay];
}
// toggle component, and keep trace of it (for undo)
function toggleComponentAndRememberIt(aEntity, oldComponent, newComponent) {
aEntity.toggleComponent(oldComponent, newComponent);
entityId = aEntity.getId().toString();
if (!(entityId in doAction)) {
doAction[entityId] = {};
}
doAction[entityId]["oldComponent"] = oldComponent;
doAction[entityId]["newComponent"] = newComponent;
}
// check and manage effects when a tooth hits a symbol at destination
function checkHitSymbols(aToothEntity, destination) {
if (
destination.has("candy") ||
destination.has("chocolate") ||
destination.has("lollipop")
) {
// make symbol disappear, replaced by empty cell
if (destination.has("candy")) {
toggleComponentAndRememberIt(destination, "candy", "ball_grey");
}
if (destination.has("chocolate")) {
toggleComponentAndRememberIt(destination, "chocolate", "ball_grey");
}
if (destination.has("lollipop")) {
toggleComponentAndRememberIt(destination, "lollipop", "ball_grey");
}
// one more carie
if (aToothEntity.has("tooth_0")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_0", "tooth_1");
} else if (aToothEntity.has("tooth_1")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_1", "tooth_2");
} else if (aToothEntity.has("tooth_2")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_2", "tooth_3");
} else if (aToothEntity.has("tooth_3")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_3", "tooth_4");
} else if (aToothEntity.has("tooth_4")) {
// this tooth is broken
toggleComponentAndRememberIt(aToothEntity, "tooth_4", "tooth_5");
}
} else if (destination.has("toothbrush")) {
// make symbol disappear, replaced by empty cell
toggleComponentAndRememberIt(destination, "toothbrush", "ball_grey");
// remove one carie
if (aToothEntity.has("tooth_1")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_1", "tooth_0");
} else if (aToothEntity.has("tooth_2")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_2", "tooth_1");
} else if (aToothEntity.has("tooth_3")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_3", "tooth_2");
} else if (aToothEntity.has("tooth_4")) {
toggleComponentAndRememberIt(aToothEntity, "tooth_4", "tooth_3");
}
} else if (destination.has("gift")) {
// make the gift disappear, replaced by a gray ball
toggleComponentAndRememberIt(destination, "gift", "ball_grey");
// Show the gift message, if provided
if ("gift" in levels[level]) {
showMessage(levels[level].gift);
}
}
}
// move one tooth
// returns true if the tooth has effectively moved, and false if the moveRequest
// does not move the tooth (because we hit the border, a wall, another tooth...)
function moveOneTooth(aToothEntity, aMoveRequest) {
toothMoved = false;
let oldTooth_x = pos_i(aToothEntity.x);
let oldTooth_y = pos_j(aToothEntity.y);
[newTooth_x, newTooth_y] = moveOneStep(
oldTooth_x,
oldTooth_y,
aMoveRequest,
1
);
if (isInBoard(newTooth_x, newTooth_y)) {
destination = entities[newTooth_x][newTooth_y];
if (destination.has("spring")) {
[newTooth_x, newTooth_y] = hopOnSprings(
newTooth_x,
newTooth_y,
aMoveRequest
);
}
if (isInBoard(newTooth_x, newTooth_y)) {
destination = entities[newTooth_x][newTooth_y];
// validate new position only if we don't enter a wall or another tooth,
if (!destination.has("wall") && isPositionFree(newTooth_x, newTooth_y)) {
aToothEntity.x = pos_x(newTooth_x);
aToothEntity.y = pos_y(newTooth_y);
toothMoved = true;
// Save move action (for undo)
entityId = aToothEntity.getId().toString();
doAction[entityId] = {};
doAction[entityId]["old_x"] = oldTooth_x;
doAction[entityId]["old_y"] = oldTooth_y;
// check hit symbols
checkHitSymbols(aToothEntity, destination);
}
}
}
return toothMoved;
}
// return array of tooth entities, ordered according to one direction
function orderedTeeth(aMoveRequest) {
var teethEntities = Crafty("tooth").get();
teethEntities.sort(function (a, b) {
var result;
switch (aMoveRequest) {
case MOVE_REQUEST.LEFT:
result = a.x - b.x;
break;
case MOVE_REQUEST.RIGHT:
result = b.x - a.x;
break;
case MOVE_REQUEST.UP:
result = a.y - b.y;
break;
case MOVE_REQUEST.DOWN:
result = b.y - a.y;
break;
}
return result;
});
return teethEntities;
}
// move all teeth according to the move request and update exit balls accordingly
// returns true if at least one tooth has effectively moved, and false if the moveRequest
// does not move any tooth (because we hit the border, a wall, another tooth...)
function moveAllTeeth(aMoveRequest) {
doAction = {}; // start from scratch again (for undo)
atLeastOneToothMoved = false;
// order the teeth according to the move direction: we move first
// the tooth which is further away in that direction to free up the
// trail behind it.
orderedTeeth(aMoveRequest).forEach(function (toothEntity) {
if (moveOneTooth(toothEntity, aMoveRequest)) {
atLeastOneToothMoved = true;
}
});
if (atLeastOneToothMoved) {
refreshExits(); // update colour of exit balls
doActions.push(doAction); // add action in the "undo" list
}
return atLeastOneToothMoved;
}
// check if position x, y is free, i.e. not ocupied by one tooth
function isPositionFree(ax, ay) {
var isFree = true;
var teethEntities = Crafty("tooth").get();
for (i = 0; i < teethEntities.length; i++) {
if (pos_i(teethEntities[i].x) == ax && pos_j(teethEntities[i].y) == ay) {
isFree = false;
break;
}
}
return isFree;
}
function isOneToothBroken() {
return Crafty("tooth_5").length > 0;
}
// check if all teeth are clean, and on a green exit ball
function isAllTeethSaved() {
var numSaved = 0;
var teethEntities = Crafty("tooth").get();
teethEntities.forEach(function (toothEntity) {
if (toothEntity.has("tooth_0")) {
i = pos_i(toothEntity.x);
j = pos_j(toothEntity.y);
if (entities[i][j].has("ball_green")) {
numSaved++;
}
}
});
return numSaved == teethEntities.length;
}
function isAllTeethClean() {
var numClean = Crafty("tooth_0").length;
var numTeeth = Crafty("tooth").length;
return numClean == numTeeth;
}
// Update colour of exit balls: green or red
function refreshExits() {
if (isAllTeethClean()) {
// turn the exit(s) green, if they aren't yet
if (Crafty("ball_red").length != 0) {
Crafty("ball_red").each(function () {
toggleComponentAndRememberIt(this, "ball_red", "ball_green");
});
}
} else {
// turn the exit(s) red, if they aren't yet
if (Crafty("ball_green").length != 0) {
Crafty("ball_green").each(function () {
toggleComponentAndRememberIt(this, "ball_green", "ball_red");
});
}
}
}
// undo last move
function undoLastMove() {
// move all teeth back where they were
// and revert all transformations (for teeth and for symbols)
doAction = doActions.pop();
for (var entityId in doAction) {
entity = Crafty(parseInt(entityId));
if ("old_x" in doAction[entityId]) {
entity.x = pos_x(doAction[entityId]["old_x"]);
}
if ("old_y" in doAction[entityId]) {
entity.y = pos_y(doAction[entityId]["old_y"]);
}
if ("oldComponent" in doAction[entityId]) {
entity.toggleComponent(
doAction[entityId]["newComponent"],
doAction[entityId]["oldComponent"]
);
}
}
doAction = {}; // Flush the "undo" actions
}
// ***********************************************
// game logic
// ***********************************************
function bindGlobalEvents() {
// global keyboard events
// one-direction control (one move at a time, needs key up before next movee,
// and no diagonal move allowed
Crafty.bind("KeyDown", function (e) {
if (e.key == Crafty.keys.U) {
stopped = false;
undoLastMove(); // undo
} else if (e.key == Crafty.keys.P) {
stopped = false;
solve(); // solve, see console output
} else if (e.key == Crafty.keys.R) {
stopped = false;
Crafty.scene("main"); // restart the scene
} else {
if (
e.key == Crafty.keys.LEFT_ARROW ||
e.key == Crafty.keys.RIGHT_ARROW ||
e.key == Crafty.keys.UP_ARROW ||
e.key == Crafty.keys.DOWN_ARROW ||
e.key == Crafty.keys.S ||
e.key == Crafty.keys.F ||
e.key == Crafty.keys.E ||
e.key == Crafty.keys.D
) {
arrowKeysPressed++;
}
// forbid diagonal moves with two arrows pressed
if (!stopped && arrowKeysPressed == 1) {
// By default no move request
moveRequest = MOVE_REQUEST.NONE;
switch (e.key) {
case Crafty.keys.LEFT_ARROW:
case Crafty.keys.S:
moveRequest = MOVE_REQUEST.LEFT;
break;
case Crafty.keys.RIGHT_ARROW:
case Crafty.keys.F:
moveRequest = MOVE_REQUEST.RIGHT;
break;
case Crafty.keys.UP_ARROW:
case Crafty.keys.E:
moveRequest = MOVE_REQUEST.UP;
break;
case Crafty.keys.DOWN_ARROW:
case Crafty.keys.D:
moveRequest = MOVE_REQUEST.DOWN;
break;
}
}
}
});
Crafty.bind("KeyUp", function (e) {
if (
e.key == Crafty.keys.LEFT_ARROW ||
e.key == Crafty.keys.RIGHT_ARROW ||
e.key == Crafty.keys.UP_ARROW ||
e.key == Crafty.keys.DOWN_ARROW ||
e.key == Crafty.keys.S ||
e.key == Crafty.keys.F ||
e.key == Crafty.keys.E ||
e.key == Crafty.keys.D
) {
if (arrowKeysPressed>0){
arrowKeysPressed--;
}
}
});
Crafty.bind("UpdateFrame", function () {
// If there is a move request: move all teeth
// Then check:
// - is one tooth game over? if yes replay this level
// - are all teeth saved? if yes go to next level
if (moveRequest != MOVE_REQUEST.NONE) {
numMoves++;
moveAllTeeth(moveRequest);
// consume the move request
moveRequest = MOVE_REQUEST.NONE;
if (isOneToothBroken()) {
// game over
levelNameText.text("Ouille! (appuie sur R ou ↷)");
// 'r' instead of SPACE because SPACE submits inputs in forms on the same HTML page
levelNameText.textColor("red");
stopped = true;
// score penalty
score -= SCORE_INCREMENT;
} else if (isAllTeethSaved()) {
// User wins!
levelNameText.text("Bravo! (appuie sur R ou ↷)");
levelNameText.textColor("green");
stopped = true;
// increase score
endTime = new Date().getTime(); // milliseconds
score += Math.round(
(SCORE_INCREMENT * SCORE_NOMINAL_TIME) / (endTime - startTime)
);
if (level < levels.length - 1) {
level++;
} else {
level = 0; // restart from first level
}
}
}
});
}
// "main" scene is the entry point from index.html
Crafty.scene("main", function () {
Crafty.viewport.scale(viewportScale);
if (doShowIntro){
doShowIntro = false;
Crafty.scene("intro");
} else {
if (doBindGlobalEvents){
doBindGlobalEvents = false;
bindGlobalEvents();
}
startTime = new Date().getTime();
doActions = []; // flush the undo action list
drawHeader();
drawBoard();
drawFooter();
}
});
Crafty.scene("intro", function () {
Crafty.viewport.scale(viewportScale);
Crafty.e("2D, Canvas, Image, Mouse, Keyboard")
.image("help.png")
.bind("Exit", function() {
this.destroy();
Crafty.scene("main");
})
.bind("Click", function () {
this.trigger("Exit");
})
.bind("KeyDown", function () {
this.trigger("Exit");
});
});