/
game.js
1354 lines (1190 loc) · 46 KB
/
game.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
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
///////////////////////////////////////////////////////////////
// //
// CONSTANT STATE //
var START_TIME = currentTime();
// BOARD DIMENSIONS & IMAGE DIMENSIONS
var TILE_SIZE = 76;
var BOARD_SIZE_X = 25;
var BOARD_SIZE_Y = 16;
var VICTORY_IMAGE_SIZE = 1000;
// PHYSICS/MOVEMENT CONSTANTS
var MAX_HORIZ_SPEED = 8;
var MAX_VERT_SPEED = 55;
var HORIZ_ACCEL = 1;
var GRAVITY = 2;
var JUMP_SPEED = -35;
// COLORS
var red = makeColor(1, 0, 0, 1);
var green = makeColor(0.2, 0.7, 0.2, 1);
var blue = makeColor(0, 0, 1, 1);
var transBlue = makeColor(0, 0, 1, 0.5); // transparent version of 'blue'
var purple = makeColor(0.5, 0, 1, 1);
var transPurple = makeColor(0.5, 0, 1, 0.5); // transparent version of 'purple'
var cyan = makeColor( 0, 0.7, 0.7, 1)
var white = makeColor(1, 1, 1, 1);
var black = makeColor(0, 0, 0, 1);
var gray = makeColor(0.7,0.7,0.7);
var DUDE_COLOR = cyan;
// LEVEL MAPS
/* each level is stored in an array of strings (and some other info, too). The first 16 lines represent
the tiles of the board, with each character standing for a different board element:
S = player spawn point (one only)
G = goal (one only)
X = wall
x = invisible wall
n, e, s, w = spikes facing in that direction
u, d = tiles that set gravity to up or down
a, b, c, f, h, i = locks
A, B, C, F, H, I = corresponding keys
Information stored after the end of the board:
17th line --> board message (displays at the top of the level)
18th line --> # of shifts
Positive or zero value --> that number of shifts allowed
Negative value --> infinite shifts
Non-numeric value (string, etc.) --> shifting disabled on this level
19th line --> put the value 1 in the 19th line to declare this level the final level
*/
// intro level
var level0 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X_______________________X",
"X__S____________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X____________X__________X",
"X____________X__________X",
"X______X____XX__________X",
"X______X____XX__________X",
"X______X____XX______G___X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"This should be easy! (L/R to move, F to jump)",
"z" ] //a non-numeric value here will disable shifting on a given level
// spikes intro
var level1 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X________wX_____________X",
"XS_______wX_____________X",
"XXXXXX___wX_____________X",
"X________wX_____________X",
"X_____XXXXX_____________X",
"X_______________XXX_____X",
"X______________XXXX_____X",
"X_____________XXXXX_____X",
"X____________XXXXXX_____X",
"XXXXXXX__XXXXXXXXXX____GX",
"X_____XnnX______________X",
"X_____XXXX______________X",
"X_______________________X",
"X________________nnnnnnnX",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Those look painful!",
"z" ]
// gravity intro
var level2 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X_______________X______GX",
"X_______________X_______X",
"X_______________X_______X",
"X_______________X__XXXXXX",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X__S____________________X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"How will I get all the way up there? (Hint: spacebar!)",
-1 ] //a negative value here sets #shifts = infinity
// more practice with gravity
var level3 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X_________X________X____X",
"X_________X________X____X",
"X____X____X____X___X____X",
"X____X____X____X___X____X",
"X____X____X____X___X____X",
"X____X_________X________X",
"XG___X_________X________X",
"XXXXXXXXXXXXXXXXXXXXXX__X",
"X_________X________X____X",
"X_________X________X____X",
"X____X____X____X___X____X",
"X____X____X____X___X____X",
"X____X_________X________X",
"XS___X_________X________X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Slolam!",
-1 ]
var level4 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXXXXsss___GX",
"XXXXXXXXXXXXXXsss_______X",
"XXXXXXXXXXXsss__________X",
"XXXXXXXXsss_____________X",
"XXXXXsss________________X",
"XXsss___________________X",
"X_______________________X",
"X_____________________nnX",
"X__________________nnnXXX",
"X______________nnnnXXXXXX",
"X___________nnnXXXXXXXXXX",
"X________nnnXXXXXXXXXXXXX",
"X___XXnnnXXXXXXXXXXXXXXXX",
"XS__XXXXXXXXXXXXXXXXXXXXX",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Now with 100% more danger!",
-1 ]
// introduce shift caps & kill key
var level5 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X_____XssXX_____________X",
"X_____X__XX_____________X",
"X_________X_____XXXXX___X",
"X_______________XG______X",
"X___________nnnnX_______X",
"X______XXXXXXXXXX_______X",
"X______sss______X_______X",
"X_______________X_______X",
"X_______________X_______X",
"XXXXX_________XXXXXXXXXXX",
"XXXXX___________________X",
"XXXXXnnX________________X",
"XXXXXXXXnnX_____X__S____X",
"XXXXXXXXXXX_____X_______X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Resources are scarce. (In an emergency, use 'esc'.)",
3 ]
// introduce keys & locks
var level6 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Xssssssss______________wX",
"X__________________C___wX",
"X______________________wX",
"X____A_________XXXXXXXXXX",
"X______________aXXXXXXXXX",
"X______________a__XXXXXXX",
"X______________a__b_XXXXX",
"X______________a__b__c__X",
"X______________a__b__c_GX",
"XS_______B_____a__b__c__X",
"X______________a__b_XXXXX",
"X______________a__XXXXXXX",
"X______________aXXXXXXXXX",
"X______________XXXXXXXXXX",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Unlock your full potential!",
-1 ]
var level7 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X____G_XssssssssssssssssX",
"X______X________________X",
"XXXXXaaa________________X",
"Xssss_________________A_X",
"X____________________XXXX",
"X_______________________X",
"X________S____XX__XX____X",
"X__________X____________X",
"X__________X____________X",
"X__________XXX__________X",
"X______________XX_______X",
"X_______________________X",
"X__________________XX___X",
"X_______________________X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Honey, have you seen my keys?",
4 ]
// introduce up-grav and down-grav tiles
var level8 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X________a______uuXXXXXXX",
"X________a______uuuXXXXXX",
"X________X______uuuuuXXXX",
"X________X______uuuuuuuXX",
"X___S____X______uuuuuuuXX",
"X________X_________XX___X",
"X________Xnnnnnnnn______X",
"X________XXXXXXXXX______X",
"X_________X__G__________X",
"X_________X_____________X",
"X______A__X_____________X",
"XdddddddddX_____________X",
"XdddddddddX_____________X",
"XdddddddddX_____________X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Wait, I'm a little... stuck.",
10 ]
var level9 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X_____uuuuX_____dddd_A__X",
"X_____uuuuX_____dddd____X",
"X_____uuuuX_____________X",
"X_______________________X",
"X_______________________X",
"X________wX_____________X",
"X________wX_____________X",
"X________wX_____________X",
"X________wX_____nnnn____X",
"X________wXXXXXXXXXXaaaXX",
"X_________ssssssssss____X",
"X_______________________X",
"X_________uuuuuuuuuu____X",
"X_S_______uuuuuuuuuu_G__X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Warning: persnickety gravity ahead!",
4 ]
var level10 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XGb_X_f_X_____________aSX",
"XXX_X_f_X_____________X_X",
"X___X_f_X_____________X_X",
"XccXX_X_X_____________X_X",
"X_____X_X_____________X_X",
"XXXXXXX_X_____________X_X",
"X______FX_____________XAX",
"XXXXhhhXX_____________XXX",
"X_____________________XXX",
"XuuuuuuuuuuuuuuuuuuuuuXXX",
"XuuuuuuXXXXuuuuuuuuuuuXXX",
"XuuuuuuuuuuuuuuuuuuCuuXXX",
"XuuBuuuuuuuuuuuuuuuuuuXXX",
"XuuuuuuuuHuunnnuuuuuuuXXX",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Boinnnng!",
4 ]
var level11 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X_________j__sssssssssssX",
"X_XXXXXXXXX_____________X",
"X_X___i___________J_____X",
"X_X_XXXXXXXX_C_________BX",
"X_X_X_b___X_____________X",
"X_X_X_XXX_X____F_____H__X",
"X_XhX_XGXcX_____________X",
"X_X_X_a_X_X_______I_____X",
"X_X_XXXXX_X_____________X",
"X_X___f___X_____________X",
"X_XXXXXXXXX_____________X",
"X________AX_____________X",
"X_________X_____________X",
"X____nnnnnX________S____X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Shift at your own risk.",
-1 ]
var level12 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XS___a___b_____F_____C__X",
"X____X___b______________X",
"X___AX___b__XXXXXXXXXXXXX",
"XXXXXX___X______________X",
"X________Xdddddddddd____X",
"X__B_____Xffffffffff____X",
"X________Xnnnnnnnnnn____X",
"X________XXXXXXXXXXX____X",
"X________Xssssssssss____X",
"X________Xhhhhhhhhhh____X",
"X________Xuuuuuuuuu___H_X",
"X________X____XXXXXXXXXXX",
"X________X___________c__X",
"XnnnnnnnnX___________c_GX",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Be careful what you wish for...",
2 ]
// invisible walls, just for fun!
var level13 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Xxxx__________x___S_____X",
"Xxxx_____x____x__xxxxxxxX",
"Xxxu_____x____x_________X",
"Xxxu_____x____xxxxxx____X",
"Xxuu_____x______________X",
"Xxuu_____xxxxxxxxxxxxaaaX",
"Xuuu_____x____x_________X",
"Xuuuxxxxxx___x__________X",
"X________x____x__x______X",
"X________x__G____x______X",
"X_A______xxxxxxxxx______X",
"X________x______________X",
"X________x______________X",
"X________x______________X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"How very puzzling!",
-1 ]
// the victory level! Yay!
var victory = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X______________________GX",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_____S_________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"X_______________________X",
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"Party time!",
-1,
1 ]
var allMaps = [ level0, level1, level2, level3, level4, level5, level6, level7,
level8, level9, level10, level11, level12, level13, victory ];
// IMAGES
var SOUND_ON_ICON = loadImage( "images/soundOn.png" );
var SOUND_OFF_ICON = loadImage( "images/soundOff.png" );
var NSPIKES = loadImage( "images/nSpikes.png" );
var ESPIKES = loadImage( "images/eSpikes.png" );
var SSPIKES = loadImage( "images/sSpikes.png" );
var WSPIKES = loadImage( "images/wSpikes.png" );
var UPARROW = loadImage( "images/upArrow.png" );
var DOWNARROW = loadImage( "images/downArrow.png" );
var KEY = loadImage( "images/key.png" );
var LOCK = loadImage( "images/lock.png" );
var OPENLOCKTRANS = loadImage( "images/openLockTrans.png" );
var VICTORY = loadImage( "images/balloonsConfetti.png" );
var PLAY_AGAIN = loadImage( "images/playagain.png" );
// SOUNDS
var UNLOCK_SOUND = loadSound( "sounds/unlock.mp3" );
var YAY_SOUND = loadSound( "sounds/yay.mp3" );
var SPIKE_SOUND = loadSound( "sounds/squish.mp3" );
var UP_SOUND = loadSound( "sounds/alarmup.wav" );
var DOWN_SOUND = loadSound( "sounds/alarmdown.wav" );
var NOSHIFT_SOUND = loadSound( "sounds/noShift.mp3 ");
var CHEERING_SOUND = loadSound( "sounds/cheering.mp3" );
var soundOn = true;
///////////////////////////////////////////////////////////////
// //
// MUTABLE STATE //
// BOARD VARIABLES
var board = [];
var walls = [];
var locks0 = [];
var locks0 = [];
var locks1 = [];
var locks2 = [];
var locks3 = [];
var locks4 = [];
var locks5 = [];
var locks6 = [];
var locks = [ locks0, locks1, locks2, locks3, locks4, locks5, locks6 ];
var spikes = [];
var upGrav = [];
var downGrav = [];
var keys = [];
var filledWalls = [];
var allLocks = [];
var boardMessage;
var happyMessage;
var sadMessage;
var dude;
var goal;
// GAME STATE VARIABLES
var leftDown = false;
var rightDown = false;
var dead = false;
var levelComplete = false;
var currentLevel = 0;
var gravityDown = true;
var shiftsRemaining = 0;
var inUpGrav = false;
var inDownGrav = false;
var victoryLevel = false;
// MOVEMENT VARIABLES
var horiz_velocity = 0;
var vert_velocity = 0;
var grav_accel = GRAVITY;
var jumping;
var nextPos;
///////////////////////////////////////////////////////////////
// //
// EVENT RULES //
// When setup happens... (gets called at the beginning of the game,
// and then at the beginning of each level)
function onSetup() {
// reset game state
levelComplete = false;
dead = false;
gravityDown = true;
victoryLevel = false;
var horiz_velocity = 0;
var vert_velocity = 0;
// make the dude and the goal anew
dude = makeDude( 0, 0, DUDE_COLOR );
goal = new Object();
goal.pos = new vec2( 0, 0 );
goal.color = green;
// clear board and make a new one
clearBoard();
makeBoard();
}
// When a key is pushed
function onKeyStart(key) {
if ( !dead && !levelComplete ){
if ( key == 37 ){ // left arrow --> move left
leftDown = true;
} else if ( key == 39 ){ // right arrow --> move right
rightDown = true;
} else if ( key == 70 ){ // 'f' key --> jump
if ( vert_velocity == 0 ){ // checks for double jumps
vert_velocity = jumping;
}
} else if ( key == 32 ){ // spacebar --> reverse gravity
if ( gravityDown ){
shiftGrav( "up" );
decrementShifts();
} else {
shiftGrav( "down" );
decrementShifts();
}
} else if ( key == 38 ){ // up arrow --> set gravity = "up"
if ( gravityDown ){
shiftGrav( "up" );
decrementShifts();
}
} else if ( key == 40 ){ // down arrow --> set gravity = "down"
if ( !gravityDown ){
shiftGrav( "down" );
decrementShifts();
}
} else if ( key == 27 ){ // escape --> kill dude to reset level
death();
}
}
}
function onKeyEnd(key) {
if ( key == 37 || key == 39 ){
leftDown = false;
rightDown = false;
horiz_velocity = 0;
}
}
// When the mouse is clicked (currently only relevant for turning sound on/off)
function onClick( x, y ){
// is the click in the tile with the sound icon? If so, toggle sound on/off.
if( inTile( x, y, soundTile ) ){
soundOn = !soundOn;
}
}
// Called 30 times or more per second
function onTick() {
simulate();
render();
}
function render() {
clearScreen();
// draw a black background
fillRectangle(0, 0, screenWidth, screenHeight, black );
// draw a white background where the board is
fillRectangle( board[0][0].corner.x, board[0][0].corner.y, TILE_SIZE * BOARD_SIZE_X, TILE_SIZE * BOARD_SIZE_Y, white );
// draw the goal
drawGoal();
// draw & fill tiles with stuff in 'em
setSoundIcon();
fillAllTiles();
drawAllTiles();
// draw the player
drawDude();
// draw gravity indicator
drawGravIndicator();
// draw grid (should be last thing drawn, except for text)
drawGrid();
// text on the board (level counter and board message)
drawBoardText();
}
function simulate(){
// wipe any messages off the screen, unless it's the last level
if ( !victoryLevel ){
happyMessage = null;
sadMessage = null;
}
// MOVEMENT
// depending on direction of gravity, adjust numerical values of vertical accel. and jumping
if ( gravityDown ){
jumping = JUMP_SPEED
grav_accel = GRAVITY
} else {
jumping = JUMP_SPEED * -1
grav_accel = GRAVITY * -1
}
// move left or right
if ( leftDown == true ){
moveHoriz( "left" );
} else if ( rightDown == true ){
moveHoriz( "right" );
}
// move vertically
moveVert();
// RESPOND TO THE TILE THE DUDE IS STANDING IN
// check if dude is in the goal; if so, win
if ( checkIntersection( getTile( goal.pos ), dude.pos ) ){
beatLevel();
}
// check if the dude is in spikes; if so, die
inSpikes = forAny( spikes, checkIntersection );
if ( inSpikes && !levelComplete){
death();
}
// check if the dude is in up-grav or down-grav square; if so, set gravity accordingly
inUpGrav = forAny( upGrav, checkIntersection );
inDownGrav = forAny( downGrav, checkIntersection );
if ( inUpGrav ){
gravityDown = false;
} else if ( inDownGrav ){
gravityDown = true;
}
// check to see if dude has gotten a key
inKey = forAny( keys, checkIntersection );
if ( inKey ){
keyIndex = forAnyIndex( keys, checkIntersection )
if ( keys[ keyIndex ].active == true ){
getKey( keyIndex );
}
}
// once a key has been gotten, can decrement lockDraw to fade out the lock
for ( var i = 0; i<allLocks.length; i++){
if ( !allLocks[i].active && allLocks[i].lockDraw > 0 ){
allLocks[i].lockDraw = allLocks[i].lockDraw - 1 ;
}
}
// RESPOND TO CHARACTER STATE
// when dude is dead...
if ( dead ){
timeSinceDeath = currentTime() - deathTime;
// disable motion
leftDown = false;
rightDown = false;
if ( timeSinceDeath > 0 && timeSinceDeath < 1.5 ){
// write a sad message on the screen
sadMessage = "Oh noes, you died! =( "
}
if ( timeSinceDeath > 1 && timeSinceDeath < 1.5 ){
// disappear
setPos( dude, new vec2( -100, -100 ) );
sadMessage = "Oh noes, you died! =( "
gravityDown = true;
} else if ( timeSinceDeath > 1.5 ){
reset(); //calls onSetup, which will reset the board (and the dude with it)
}
}
// if the level is complete...
if ( levelComplete ){
timeSinceWin = currentTime() - winTime;
// disable motion
leftDown = false;
rightDown = false;
// special case: if you just beat the last level, restart the game
if ( victoryLevel ){
if ( timeSinceWin > 1.5 ){
currentLevel = 0;
reset(); //calls onSetup, which will reset the board and draw the next level
}
} else {
if ( timeSinceWin > 0 && timeSinceWin < 1.5 ){
// happy message
happyMessage = "GREAT JOB!";
} else if ( timeSinceWin > 1.5 ){
// go to the next level
currentLevel++;
reset(); //calls onSetup, which will reset the board and draw the next level
}
}
}
// stop-gap bug fix: if you somehow ended up off-the board, reset the level
if ( offScreen() && !dead ){
death();
}
}
// calculate horizontal acceleration and uses it to change velocity
function accelerateHoriz( dir ){
// figure out relevant values (i.e. positive vs. negative) according to direction of motion
if ( dir == "left" ){
var mod = HORIZ_ACCEL * -1
var multiplier = -1
} else if ( dir == "right" ){
var mod = HORIZ_ACCEL
var multiplier = 1
}
// calculate new horiz_velocity, not exceeding max speed
if ( abs( horiz_velocity + mod ) <= MAX_HORIZ_SPEED ){
horiz_velocity = horiz_velocity + mod;
} else if ( abs( horiz_velocity + mod ) > MAX_HORIZ_SPEED && abs( horiz_velocity ) < MAX_HORIZ_SPEED ){
horiz_velocity = MAX_HORIZ_SPEED * multiplier
} else {
horiz_velocity = horiz_velocity;
}
}
// move horizontally
function moveHoriz( dir ){
// first, find current accel.
accelerateHoriz( dir );
nextPos = new vec2( dude.pos.x + horiz_velocity, dude.pos.y )
nextCollides = forAny( walls, function( tile ){ return checkIntersection( tile, nextPos, "horiz" ) } );
if( nextCollides ){
// if your full move results in hitting a wall, just go to the edge of your current tile
var curTile = getTile( dude.pos );
setPos( dude, vec2 ( curTile.center.x, dude.pos.y ) );
} else {
// otherwise, change your position by current velocity
setPos( dude, nextPos );
}
}
// set vertical velocity to maximum (in either pos. or neg. direction, depending on which way you're moving)
function maxVertVelocity(){
if ( vert_velocity > 0 ){
vert_velocity = MAX_VERT_SPEED;
} else if ( vert_velocity < 0 ){
vert_velocity = MAX_VERT_SPEED * -1;
}
}
// move vertically (i.e. apply gravity)
function moveVert( dir ){
// first, find current accel. (making sure not to, by some glitch, reach superspeed)
if ( abs(vert_velocity) > MAX_VERT_SPEED ){
// (if something has gone wrong and you're moving too fast, get speed back under maximum.))
maxVertVelocity();
} else if ( abs(vert_velocity) < MAX_VERT_SPEED ){
if ( abs(vert_velocity + grav_accel) < MAX_VERT_SPEED ){
vert_velocity = vert_velocity + grav_accel;
} else {
maxVertVelocity()
}
}
nextPos = new vec2( dude.pos.x, dude.pos.y + vert_velocity)
nextCollides = forAny( walls, function( tile ){ return checkIntersection( tile, nextPos ) } );
if( nextCollides ){
// if your full move results in hitting a wall, try moving half that distance
vert_velocity = vert_velocity/2
nextPos = new vec2( dude.pos.x, dude.pos.y + vert_velocity)
nextCollides = forAny( walls, function( tile ){ return checkIntersection( tile, nextPos ) } );
if ( nextCollides ){
// if you STILL hit a wall, check half THAT distance
vert_velocity = vert_velocity/2
nextPos = new vec2( dude.pos.x, dude.pos.y + vert_velocity)
nextCollides = forAny( walls, function( tile ){ return checkIntersection( tile, nextPos ) } );
if ( nextCollides ){
// if you STILL hit a wall, try half that distance, and if you still hit
//a wall, just stay still
vert_velocity = 0
} else {
// otherwise, move by that distance
setPos( dude, nextPos );
}
} else {
// otherwise, move by that distance
setPos( dude, nextPos );
}
} else {
// otherwise, move by that distance
setPos( dude, nextPos );
}
}
// what happens when you beat the level
function beatLevel(){
if ( !levelComplete && !dead ){
dude.color = purple;
levelComplete = true;
winTime = currentTime();
playSoundConditional( YAY_SOUND );
}
}
// what happens when you die
function death(){
if ( !dead ){
dude.color = red;
dead = true;
deathTime = currentTime();
playSoundConditional( SPIKE_SOUND );
}
}
// what happens when you get a key
function getKey( index ){
// key disappears
keys[ index ].image = null;
// key is deactivated so you can't collect it more than once
keys[ index ].active = false;
// open the corresponding lock
locks[ index ].forEach( openLock );
}
// opening a single tile of lock; will then be applied to each square of a macro-lock
function openLock( tile ){
// lock is deactivated so transparency will work
tile.active = false;
// change lock image to the open lock
tile.image = OPENLOCKTRANS;
// remove from the array of walls so it doesn't block movement
lockWallsIndex = indexOf( walls, tile );
removeAt( walls, lockWallsIndex );
playSoundConditional( UNLOCK_SOUND );
}
// shift gravity in specified direction, if there are shifts remaining
function shiftGrav( dir ){
if ( shiftsRemaining !== 0 && isNumber( shiftsRemaining ) ){
stopAllGravSounds();
if ( dir == "up" ){
if ( inDownGrav ){
playSoundConditional( NOSHIFT_SOUND );
} else {
gravityDown = false;
playSoundConditional( UP_SOUND );
}
} else if ( dir == "down" ){
if ( inUpGrav ){
playSoundConditional( NOSHIFT_SOUND );
} else {
gravityDown = true;
playSoundConditional( DOWN_SOUND );
}
} else {
alert( "Invalid gravity direction!")
}
} else if ( shiftsRemaining == 0 ){
playSoundConditional( NOSHIFT_SOUND );
}
}
///////////////////////////////////////////////////////////////
// //
// HELPER RULES //
// MAKIN' STUFF & DRAWIN' STUFF
// make the dude
function makeDude( xstart, ystart, startcolor ){
position = new vec2( xstart, ystart )
return { pos : position, startPos : position, radius : TILE_SIZE/2, color : startcolor };
}
// make the board
function makeBoard(){
// this will be used in a bit to make locks and keys
lockKeyIndexes = [ "a", "b", "c", "f", "h", "i", "j" ];
// select the appropriate map from the array of all maps, according to current level
currentMap = allMaps[ currentLevel ];
// Create an array of columns
for ( var x = 0; x < BOARD_SIZE_X; x++ ) {
// Create this column
board[x] = [];
for ( var y = 0; y < BOARD_SIZE_Y; y++ ){
tile = makeObject();
// Top left corner of the tile in pixels
cornerX = (screenWidth - TILE_SIZE * BOARD_SIZE_X) / 2 + x * TILE_SIZE;
cornerY = (screenHeight - TILE_SIZE * BOARD_SIZE_Y) / 2 + y * TILE_SIZE;
tile.corner = new vec2( cornerX, cornerY );
// Center of the tile in pixels
centerX = (screenWidth - TILE_SIZE * BOARD_SIZE_X) / 2 + (x + 0.5) * TILE_SIZE;
centerY = (screenHeight - TILE_SIZE * BOARD_SIZE_Y) / 2 + (y + 0.5) * TILE_SIZE;
tile.center = new vec2( centerX, centerY );
// coordinates of the tile according to grid squares
tile.coords = new vec2( x, y );
// TRANSLATE CHARACTERS IN MAP TO ELEMENTS ON GAMEBOARD
mapElement = substring( currentMap[y], x, x+1 )
// walls, spawn, goal
if ( mapElement == "X" ){
insertBack( walls, tile );
insertBack( filledWalls, tile );
} else if ( mapElement == "x" ){
insertBack( walls, tile );
} else if ( mapElement == "G" ){
goal.pos = tile.center;
} else if ( mapElement == "S" ){
dude.startPos = tile.center;
dude.pos = tile.center;
}
// spikes
else if ( mapElement == "n" ){
tile.image = NSPIKES
insertBack( spikes, tile );
} else if ( mapElement == "e" ){
tile.image = ESPIKES
insertBack( spikes, tile );
} else if ( mapElement == "s" ){
tile.image = SSPIKES
insertBack( spikes, tile );
} else if ( mapElement == "w" ){
tile.image = WSPIKES
insertBack( spikes, tile );
}
// gravity influence tiles
else if ( mapElement == "u" ){
tile.image = UPARROW;
insertBack( upGrav, tile );
} else if ( mapElement == "d" ){
tile.image = DOWNARROW;
insertBack( downGrav, tile );
}
//keys and locks
else if ( mapElement == "A" ||
mapElement == "B" ||
mapElement == "C" ||
mapElement == "F" ||
mapElement == "H" ||
mapElement == "I" ||
mapElement == "J" ){
tile.image = KEY;
tile.active = true;
insertionIndex = indexOf( lockKeyIndexes, mapElement.toLowerCase() );
insertAt( keys, insertionIndex, tile );
} else if ( mapElement == "a" ||
mapElement == "b" ||
mapElement == "c" ||
mapElement == "f" ||
mapElement == "h" ||
mapElement == "i" ||
mapElement == "j" ){
insertBack( walls, tile )
tile.image = LOCK;
tile.active = true;
tile.lockDraw = 100;
insertionIndex = indexOf( lockKeyIndexes, mapElement );
insertBack( locks[ insertionIndex ], tile );
insertBack( allLocks, tile )
}
board[x][y] = tile;
}
}
boardMessage = currentMap[ BOARD_SIZE_Y ];
shiftsRemaining = currentMap[ BOARD_SIZE_Y + 1 ];
if ( currentMap[ BOARD_SIZE_Y + 2 ] == 1 ){
// it's the victory level!
victoryLevel = true;
happyMessage = "YOU DID IT!";
playSoundConditional( CHEERING_SOUND );
}
// set the coordinates of the tile containing the sound on/off icon
soundTile = board[BOARD_SIZE_X - 1][0];
}
// clears the board so that the next level can be drawn
function clearBoard(){
board = [];
walls = [];
locks0 = [];
locks1 = [];
locks2 = [];
locks3 = [];
locks4 = [];
locks5 = [];
locks6 = [];
locks = [ locks0, locks1, locks2, locks3, locks4, locks5, locks6 ];
spikes = [];
upGrav = [];
downGrav = [];
keys = [];
filledWalls = [];
allLocks = [];
}