-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.js
2778 lines (2357 loc) · 82 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
/* Global variables. */
/* This is the state machine for the complete engine. */
var STATE_IDLE = 0; /* Nothing to do right now. */
var STATE_LOADING = 1; /* Loading the level data. */
var STATE_INITIALIZING = 2; /* Converting the level to the internal format. */
var STATE_PLAYING = 3; /* Playing. */
var STATE_PAUSE = 4; /* Playing but paused. */
var STATE_GAME_OVER = 5; /* Game over. */
var STATE_FINISH = 6; /* Reached the goal. */
var tState = STATE_IDLE;
/* This is the state for the player. */
var PLAYERSTATE_INACTIVE = 0; /* The player is currently not on the screen. */
var PLAYERSTATE_WALKING = 1; /* The player is walking on solid ground. */
var PLAYERSTATE_FALLING = 2; /* The player falls down. */
var PLAYERSTATE_JUMPING = 3; /* The player is jumping. */
var PLAYERSTATE_LIFELOST = 4; /* The player lost a life. */
var PLAYERJUMPSTATE_UP = 0;
var PLAYERJUMPSTATE_DOWN = 1;
var PLAYERANIMATION_STAND_LEFT = 0;
var PLAYERANIMATION_STAND_RIGHT = 1;
var PLAYERANIMATION_WALK_LEFT = 2;
var PLAYERANIMATION_WALK_RIGHT = 3;
var PLAYERANIMATION_JUMP_LEFT = 4;
var PLAYERANIMATION_JUMP_RIGHT = 5;
var PLAYERANIMATION_CLIMBING = 6;
var PLAYERANIMATION_DEAD = 7;
var tPlayer = [];
tPlayer.uiNumberOfLifesLeft = 4;
tPlayer.tState = PLAYERSTATE_INACTIVE;
tPlayer.ulFallingHeight = 0; /* This is the number of pixels the player fell down. */
tPlayer.ulMaxFallingHeight = 128; /* This is the maximum number of pixels the player can fall down without dying. */
tPlayer.ulCurrentSpeed = 0;
tPlayer.ulSpeedMax_JumpX = 4;
tPlayer.ulSpeedMax_Walk = 4;
tPlayer.ulSpeedMax_Climb = 2;
tPlayer.ulSpeedMax_FallDown = 8;
tPlayer.ulMaxStepUp = 8;
tPlayer.tCollisionBox = []
tPlayer.tCollisionBox.x0 = 9;
tPlayer.tCollisionBox.y0 = 2;
tPlayer.tCollisionBox.x1 = 23;
tPlayer.tCollisionBox.y1 = 32;
tPlayer.tJumpState = PLAYERJUMPSTATE_UP;
tPlayer.ulJumpBasePositionY = -1;
tPlayer.iJumpSpeedX = 0;
tPlayer.uiJumpYCnt = 0;
tPlayer.uiJumpYMax = 0;
tPlayer.uiJumpYOffset = 0;
tPlayer.atAnimation = [];
tPlayer.atAnimation.auiStandLeft = null;
tPlayer.atAnimation.auiStandRight = null;
tPlayer.atAnimation.auiJumpLeft = null;
tPlayer.atAnimation.auiJumpRight = null;
tPlayer.atAnimation.auiWalkLeft = null;
tPlayer.atAnimation.auiWalkRight = null;
tPlayer.atAnimation.auiClimbing = null;
tPlayer.atAnimation.auiDead = null;
/* These are the possible collision test modes. */
var COLLISIONTEST_WALK = 0; /* Check if walking is stopped by a collision. */
var COLLISIONTEST_FALL = 1; /* Check if falling is stopped by a collision. */
var COLLISIONTEST_JUMP = 2; /* Check if jumping is stopped by a collision. */
var COLLISIONTEST_STAND = 3; /* Check if the player can stand on a tile. */
/* Size of one tile in pixels. */
var sizTileXPixels;
var sizTileYPixels;
/* Size of the screen and the buffer in tiles. */
var sizScreenSizeXTiles;
var sizScreenSizeYTiles;
var sizBufferSizeXTiles;
var sizBufferSizeYTiles;
/* This is the request object for loading various data from the server. */
var tLoader = [];
tLoader.tRequestObject = null;
tLoader.atRequestList = [];
tLoader.ulRequestPosition = 0;
tLoader.pfnFinal = null;
/* This is the parsed map. */
var tMapParsed;
/*
* Rendering
*/
/* This is the array of layers. */
var atLayers;
/* This is the array of rooms. */
var atRooms;
var atRoomsActive = [];
/* This is the array of tiles. */
var atTiles;
/* This array contains all animations. */
var atAnimationsAll;
/* This array contains all global animations. They are always active and all tiles with this animation on the screen have the same animation step. */
var atAnimationsGlobal;
/* This array contains the local animations. They are special for one single tile and animate independently from all other tiles and their animations. */
var atAnimationsLocal;
/* This array contains all named tiles. */
var atNamedTiles;
/* This is the list of all sprites in all layers. */
var atSprites;
/* This is the player sprite. */
var tSpritePlayer;
/* This is the game-over sprite. It is the player as an angel. */
var tSpritePlayerAngel;
/* This is the on-screen canvas. */
var tScreenCanvas;
/* This is the context for the on-screen canvas. */
var tScreenCtx;
/* This is the mask canvas and context. */
var tMask;
/* This is the rendering stack. */
var atRenderStack;
/* This is a view of the complete level in buffers of 8*8 tiles. */
var atLevelBuffers;
var uiLevelBuffersX;
var uiLevelBuffersY;
/* This is the array of 'redraw requested' bits for the complete buffer.
* Note that the buffer is larger than the screen to enable scrolling.
*/
var aucReRe;
/* This is the camera position in pixels. */
var ulCameraX;
var ulCameraY;
/* This is a notification from an asyncronous handler like the "onblur" event that a pause of the game is requested. */
var fRequestPause;
var ulLastRenderTime;
/* This function decodes one character of a base64 encoded string to a 6 bit
* value.
*/
function base64_decode_char(ucChar)
{
var ucData = 0;
/* A..Z is 0..25 . */
if( ucChar>=65 && ucChar<=90 )
{
ucData = ucChar - 65;
}
/* a..z is 26..51 . */
else if( ucChar>=97 && ucChar<=122 )
{
ucData = ucChar - 71;
}
/* 0..9 is 52..61 . */
else if( ucChar>=48 && ucChar<=57 )
{
ucData = ucChar + 4;
}
/* + is 62 . */
else if( ucChar==43 )
{
ucData = 62;
}
/* / is 63 . */
else if( ucChar==47 )
{
ucData = 63;
}
return ucData;
}
function base64_to_uint8array(strBase64)
{
var sizBytesOut = strBase64.length * 3 / 4;
/* Count the '=' at the end of the encoded string. */
if( strBase64.charAt(strBase64.length-1)=='=' )
{
sizBytesOut -= 1;
if( strBase64.charAt(strBase64.length-2)=='=' )
{
sizBytesOut -= 1;
}
}
var aData = new Uint8ClampedArray(sizBytesOut);
/* Loop over all output bytes. */
for(var uiPosIn=0,uiPosOut=0; uiPosOut<sizBytesOut; uiPosIn+=4,uiPosOut+=3)
{
var ul0 = base64_decode_char(strBase64.charCodeAt(uiPosIn));
var ul1 = base64_decode_char(strBase64.charCodeAt(uiPosIn+1));
var ul2 = base64_decode_char(strBase64.charCodeAt(uiPosIn+2));
var ul3 = base64_decode_char(strBase64.charCodeAt(uiPosIn+3));
aData[uiPosOut] = ((ul0 << 2) | (ul1 >>> 4)) & 0xff;
aData[uiPosOut+1] = ((ul1 << 4) | (ul2 >>> 2)) & 0xff;
aData[uiPosOut+2] = ((ul2 << 6) | ul3) & 0xff;
}
return aData;
}
function continue_animation(tAnim, dTickFraction)
{
var dTickFractionsLeft = dTickFraction;
if( tAnim.fIsRunning==true )
{
while( dTickFractionsLeft>0.0 )
{
var dTickFractionChunk = dTickFractionsLeft;
/* Is a delay present? */
if( tAnim.dCurrentDelay>0.0 )
{
/* Limit the tick fraktion chunk by the delay. */
if( dTickFractionChunk>tAnim.dCurrentDelay )
{
dTickFractionChunk = tAnim.dCurrentDelay;
}
/* Decrement the delay. */
tAnim.dCurrentDelay -= dTickFractionChunk;
}
dTickFractionsLeft -= dTickFractionChunk;
/* Is a delay left? */
if( tAnim.dCurrentDelay==0.0 )
{
/* No -> move on to the next animation step. */
/* Get the current step value. */
var tStep = tAnim.atSteps[tAnim.uiCurrentStep];
/* Set the new tile for this step. */
tAnim.uiCurrentTileIndex = tStep.uiTileIndex;
/* Set the new delay for this step. */
tAnim.dCurrentDelay = tStep.uiDelay;
/* Increase the posititon. */
tAnim.uiCurrentStep += 1;
/* Reached the end of the array? */
if( tAnim.uiCurrentStep>=tAnim.atSteps.length )
{
/* Yes. Restart? */
if( tAnim.fIsOneshot==false )
{
/* Yes, restart! */
tAnim.uiCurrentStep = 0;
}
else
{
/* No, this is a one-shot animation. Stop processing this animation. */
tAnim.fIsRunning = false;
/* Do not process the rest of the time. */
dTickFractionsLeft = 0.0;
/* Is there a callback? */
if( tAnim.fnCallbackEnd!=null )
{
tAnim.fnCallbackEnd(tAnim);
}
}
}
}
}
}
}
function animations_step(dTickFraction)
{
/* Loop over all global animations. */
var sizAnimationsGlobal = atAnimationsGlobal.length;
for(var uiIdx=0; uiIdx<sizAnimationsGlobal; uiIdx++)
{
var tAnimation = atAnimationsGlobal[uiIdx];
continue_animation(tAnimation, dTickFraction);
}
/* Loop over all local animations. */
var sizAnimationsLocal = atAnimationsLocal.length;
for(var uiIdx=0; uiIdx<sizAnimationsLocal; uiIdx++)
{
var tAnimation = atAnimationsLocal[uiIdx];
continue_animation(tAnimation, dTickFraction);
}
}
function animation_clone(tExistingAnimation, uiLayer, ulTileReference)
{
/* Create the new animation object. */
var tClonedAnimation = {};
/* Make a link to the steps. */
tClonedAnimation.atSteps = tExistingAnimation.atSteps;
/* Initialize the animation position. */
tClonedAnimation.uiCurrentTileIndex = tClonedAnimation.atSteps[0].uiTileIndex;
tClonedAnimation.dCurrentDelay = 0.0;
tClonedAnimation.uiCurrentStep = 0;
/* Copy all other elements. */
tClonedAnimation.fIsOneshot = tExistingAnimation.fIsOneshot;
tClonedAnimation.fnCallbackEnd = tExistingAnimation.fnCallbackEnd;
/* Add the layer and tile index as a reference. */
tClonedAnimation.uiLayer = uiLayer;
tClonedAnimation.ulTileReference = ulTileReference;
/* Set the animation to "running" by default. */
tClonedAnimation.fIsRunning = true;
return tClonedAnimation;
}
function animation_search_clone(atList, ulTileReference)
{
/* Nothing found yet. */
var uiFoundIdx = null;
/* Loop over all elements in the list. */
var sizList = atList.length;
for(var uiCnt=0; uiCnt<sizList; uiCnt++)
{
if( atList[uiCnt].ulTileReference==ulTileReference )
{
uiFoundIdx = uiCnt;
break;
}
}
return uiFoundIdx;
}
function animation_remove_all_clones(atList, ulTileReference)
{
/* This is the start index. */
var uiIdx = 0;
var sizList = atList.length;
while(uiIdx<sizList)
{
/* Does the current element match the criteria? */
if(atList[uiIdx].ulTileReference==ulTileReference)
{
/* Yes -> delete the item. */
atList.splice(uiIdx, 1);
/* Update the size of the list. */
--sizList;
/* NOTE: do not move to the next entry here. */
}
else
{
/* Move to the next entry. */
++uiIdx;
}
}
}
function animation_end_mushroom_switch_activated(tAnimation)
{
var uiTilePos = tAnimation.ulTileReference;
var uiLayer = tAnimation.uiLayer;
/* Remove the animation from the local list. */
animation_remove_all_clones(atAnimationsLocal, uiTilePos);
/* Remove the animation from the tile. */
atRenderStack[uiLayer].atAnimations[uiTilePos] = null;
/* Activate the switch again. */
var ulTileIdx = atNamedTiles['mushroom_switch_finished'];
atRenderStack[uiLayer].atTiles[uiTilePos] = ulTileIdx;
/* Request a redraw of the tile. */
aucReRe[uiTilePos>>>3] |= 1<<(uiTilePos&7);
/* Search the switch in the position table. */
var uiTarget0Pos = tMapParsed.atSwitch2Door[uiTilePos];
var uiTarget1Pos = uiTarget0Pos + tMapParsed.map_size_x;
/* Get the door animations. */
var ulTile0Idx = atNamedTiles['door_up'];
var tAnim0 = atAnimationsAll[ulTile0Idx];
var ulTile1Idx = atNamedTiles['door_down'];
var tAnim1 = atAnimationsAll[ulTile1Idx];
/* Clone the animations. */
var tClonedAnimation0 = animation_clone(tAnim0, uiLayer, uiTarget0Pos);
var tClonedAnimation1 = animation_clone(tAnim1, uiLayer, uiTarget1Pos);
/* Attach the new animation to the tile and add it to the local animations. */
atRenderStack[uiLayer].atAnimations[uiTarget0Pos] = tClonedAnimation0;
atRenderStack[uiLayer].atAnimations[uiTarget1Pos] = tClonedAnimation1;
atAnimationsLocal.push(tClonedAnimation0);
atAnimationsLocal.push(tClonedAnimation1);
/* Set the first element of the new animation. */
atRenderStack[uiLayer].atTiles[uiTarget0Pos] = ulTile0Idx;
atRenderStack[uiLayer].atTiles[uiTarget1Pos] = ulTile1Idx;
}
function animation_end_door_open(tAnimation)
{
var uiTilePos = tAnimation.ulTileReference;
var uiLayer = tAnimation.uiLayer;
/* Remove the animation from the local list. */
animation_remove_all_clones(atAnimationsLocal, uiTilePos);
/* Remove the animation from the tile. */
atRenderStack[uiLayer].atAnimations[uiTilePos] = null;
/* Clear the field. */
atRenderStack[uiLayer].atTiles[uiTilePos] = 0;
/* Request a redraw of the tile. */
aucReRe[uiTilePos>>>3] |= 1<<(uiTilePos&7);
}
var atAnimationEndFunctions = {
'mushroom_switch_activated': animation_end_mushroom_switch_activated,
'door_open': animation_end_door_open
};
function redraw_tile(x, y)
{
/* This is the position in the map data. */
var uiTilePos = x + tMapParsed.map_size_x * y;
var fRedraw = ( (aucReRe[uiTilePos>>>3] & (1<<(uiTilePos&7)))!=0 );
/* Check all layers for animation. */
var sizRenderStack = atRenderStack.length;
for(var uiCnt=0; uiCnt<sizRenderStack; uiCnt++)
{
var tAnim = atRenderStack[uiCnt].atAnimations[uiTilePos];
if( tAnim!=null && atRenderStack[uiCnt].atTiles[uiTilePos]!=tAnim.uiCurrentTileIndex )
{
atRenderStack[uiCnt].atTiles[uiTilePos] = tAnim.uiCurrentTileIndex;
fRedraw = true;
}
}
if( fRedraw==true )
{
/* Get the image data for all layers. */
var i0 = atTiles[ atRenderStack[0].atTiles[uiTilePos] ];
var i1 = atTiles[ atRenderStack[1].atTiles[uiTilePos] ];
/* This is the position on the level buffers. */
var uiBufferPos = Math.floor(x/sizBufferSizeXTiles) + uiLevelBuffersX*Math.floor(y/sizBufferSizeYTiles);
var uiBufferXOffs = x % sizBufferSizeXTiles;
var uiBufferYOffs = y % sizBufferSizeYTiles;
/* Get the buffers. */
var b0 = atLevelBuffers[uiBufferPos][0][1];
var b1 = atLevelBuffers[uiBufferPos][1][1];
/* Draw the tiles. */
b0.putImageData(i0,uiBufferXOffs*sizTileXPixels,uiBufferYOffs*sizTileYPixels);
b1.putImageData(i1,uiBufferXOffs*sizTileXPixels,uiBufferYOffs*sizTileYPixels);
aucReRe[uiTilePos>>>3] &= ~(1<<(uiTilePos&7));
}
}
/*
* Draw the buffers on the screen.
*/
function draw_map()
{
var ulBufXStart = Math.floor(ulCameraX / (sizTileXPixels*sizBufferSizeXTiles));
var ulBufYStart = Math.floor(ulCameraY / (sizTileYPixels*sizBufferSizeYTiles));
var ulBufXEnd = Math.ceil((ulCameraX+sizScreenSizeXTiles*sizTileXPixels) / (sizTileXPixels*sizBufferSizeXTiles));
var ulBufYEnd = Math.ceil((ulCameraY+sizScreenSizeYTiles*sizTileYPixels) / (sizTileYPixels*sizBufferSizeYTiles));
var uiOffsetX = ulCameraX % (sizTileXPixels*sizBufferSizeXTiles);
var uiOffsetY = ulCameraY % (sizTileYPixels*sizBufferSizeYTiles);
var ulScreenSizeXPixels = sizScreenSizeXTiles * sizTileXPixels;
var ulScreenSizeYPixels = sizScreenSizeYTiles * sizTileYPixels;
var ulBufferXPixels = sizBufferSizeXTiles * sizTileXPixels;
var ulBufferYPixels = sizBufferSizeYTiles * sizTileYPixels;
var sx;
var sy;
var sw;
var sh;
var dx;
var dy;
/* Draw with the identity matrix. */
/* tScreenCtx.resetTransform(); */
tScreenCtx.setTransform(1, 0, 0, 1, 0, 0);
var sizRenderStack = atRenderStack.length;
for(var uiLayerCnt=0; uiLayerCnt<sizRenderStack; uiLayerCnt++)
{
var tRenderLayer = atRenderStack[uiLayerCnt];
sy = uiOffsetY;
sh = ulBufferYPixels-sy;
dy = 0;
for(var y=ulBufYStart; y<ulBufYEnd; y++)
{
sx = uiOffsetX;
sw = ulBufferXPixels - sx;
dx = 0;
for(var x=ulBufXStart; x<ulBufXEnd; x++)
{
/* Get the buffers. */
var uiBufferPos = x + uiLevelBuffersX*y;
var i = atLevelBuffers[uiBufferPos][uiLayerCnt][0];
tScreenCtx.drawImage(i, sx, sy, sw, sh, dx, dy, sw, sh);
dx += sw;
sx = 0;
sw = ulBufferXPixels;
if( (dx+sw)>ulScreenSizeXPixels )
{
sw = ulScreenSizeXPixels - dx;
}
}
dy += sh;
sy = 0;
sh = ulBufferYPixels;
if( (dy+sh)>ulScreenSizeYPixels )
{
sh = ulScreenSizeYPixels - dy;
}
}
/* Now display the sprites. */
var atSprites = tRenderLayer.atSprites;
var sizSprites = atSprites.length;
for(var uiSpriteCnt=0; uiSpriteCnt<sizSprites; uiSpriteCnt++)
{
var tSprite = atSprites[uiSpriteCnt];
/* Is this sprite visible? */
if( tSprite.fActive==true )
{
/* Yes, the sprite is visible. */
var posX = tSprite.uiPosX-ulCameraX-tPlayer.tCollisionBox.x0-(tPlayer.tCollisionBox.x1-tPlayer.tCollisionBox.x0)/2;
var posY = tSprite.uiPosY-ulCameraY-tPlayer.tCollisionBox.y0-(tPlayer.tCollisionBox.y1-tPlayer.tCollisionBox.y0)/2;
tScreenCtx.drawImage(tSprite.tCanvas, posX, posY);
}
}
}
/* Display the mask on top of all layers. */
tScreenCtx.translate(-ulCameraX, -ulCameraY);
tScreenCtx.scale(sizTileXPixels, sizTileYPixels);
tScreenCtx.drawImage(tMask.tCanvas, 0, 0);
tScreenCtx.setTransform(1, 0, 0, 1, 0, 0);
}
function camera_update_position(ulCameraNewX, ulCameraNewY)
{
var ulScreenSizeXPixel = sizScreenSizeXTiles * sizTileXPixels;
var ulScreenSizeYPixel = sizScreenSizeYTiles * sizTileYPixels;
/* Did the position change at all? */
if( ulCameraNewX!=ulCameraX || ulCameraNewY!=ulCameraY )
{
var ulCurrentBufferX = Math.floor(ulCameraX / (sizBufferSizeXTiles*sizTileXPixels));
var ulCurrentBufferY = Math.floor(ulCameraY / (sizBufferSizeYTiles*sizTileYPixels));
var ulNewBufferX = Math.floor(ulCameraNewX / (sizBufferSizeXTiles*sizTileXPixels));
var ulNewBufferY = Math.floor(ulCameraNewY / (sizBufferSizeYTiles*sizTileYPixels));
var ulNewBufferXEnd = ulNewBufferX + Math.ceil((sizScreenSizeXTiles+1) / sizBufferSizeXTiles);
var ulNewBufferYEnd = ulNewBufferY + Math.ceil((sizScreenSizeYTiles+1) / sizBufferSizeYTiles);
/* Did the tile position change? */
if( ulCurrentBufferX!=ulNewBufferX || ulCurrentBufferY!=ulNewBufferY )
{
/* Collect all buffers which are currently off-screen. */
var aFree = [];
var aNeed = [];
for(var y=0; y<uiLevelBuffersX; y++)
{
for(var x=0; x<uiLevelBuffersX; x++)
{
/* Does this position have a buffer? */
if( atLevelBuffers[x + y*uiLevelBuffersX]!=null )
{
if( x<ulNewBufferX || x>=ulNewBufferXEnd || y<ulNewBufferY || y>=ulNewBufferYEnd )
{
/* This buffer is completely off-screen. */
aFree.push([x,y]);
}
}
else
{
/* This position does not have a buffer.
* Is it inside the new buffered area?
*/
if( x>=ulNewBufferX && x<ulNewBufferXEnd && y>=ulNewBufferY && y<ulNewBufferYEnd )
{
aNeed.push([x,y]);
}
}
}
}
/* Now the number of free buffers must match the wanted. */
if( aFree.length==aNeed.length )
{
var tBuf;
var uiSrc;
var uiDst;
var xs, ys, xe, ye, uiTilePos;
for(var uiCnt=0; uiCnt<aFree.length; uiCnt++)
{
uiSrc = aFree[uiCnt][0] + aFree[uiCnt][1]*uiLevelBuffersX;
uiDst = aNeed[uiCnt][0] + aNeed[uiCnt][1]*uiLevelBuffersX;
atLevelBuffers[uiDst] = atLevelBuffers[uiSrc];
atLevelBuffers[uiSrc] = null;
/* Invalidate the ReRe for the buffer. */
xs = aNeed[uiCnt][0]*sizBufferSizeXTiles;
xe = xs + sizBufferSizeXTiles;
ys = aNeed[uiCnt][1]*sizBufferSizeYTiles;
ye = ys + sizBufferSizeYTiles;
for(var y=ys; y<ye; y++)
{
for(var x=xs; x<xe; x++)
{
uiTilePos = x + tMapParsed.map_size_x * y;
aucReRe[uiTilePos>>>3] |= 1<<(uiTilePos&7);
}
}
}
}
}
ulCameraX = ulCameraNewX;
ulCameraY = ulCameraNewY;
}
}
function camera_move_center_player()
{
/* There should be several modes in the future, like a fixed position for a special event in a small room.
* For now there is just one mode: center around the player.
*/
/* Get the player position. */
var ulPosX = tSpritePlayer.uiPosX;
var ulPosY = tSpritePlayer.uiPosY;
if( tPlayer.tState==PLAYERSTATE_JUMPING )
{
ulPosY = tPlayer.ulJumpBasePositionY;
}
else if( tPlayer.tState==PLAYERSTATE_FALLING )
{
/* A jump is currently going on if the player is above the baseline. */
if( ulPosY<=tPlayer.ulJumpBasePositionY )
{
ulPosY = tPlayer.ulJumpBasePositionY;
}
}
/* Try to center the player. */
var ulCameraNewX = ulPosX - (sizScreenSizeXTiles*sizTileXPixels / 2);
var ulCameraNewY = ulPosY - (sizScreenSizeYTiles*sizTileYPixels / 2);
var ulCameraMaxX = (tMapParsed.map_size_x-sizScreenSizeXTiles) * sizTileXPixels - 1;
var ulCameraMaxY = (tMapParsed.map_size_y-sizScreenSizeYTiles) * sizTileYPixels - 1;
if( ulCameraNewX<0 )
{
ulCameraNewX = 0;
}
else if( ulCameraNewX>ulCameraMaxX )
{
ulCameraNewX = ulCameraMaxX;
}
if( ulCameraNewY<0 )
{
ulCameraNewY = 0;
}
else if( ulCameraNewY>ulCameraMaxY )
{
ulCameraNewY = ulCameraMaxY;
}
/* Return the new camera position. */
return [ulCameraNewX, ulCameraNewY];
}
function camera_move()
{
/* There should be several modes in the future, like a fixed position for a special event in a small room.
* For now there is just one mode: center around the player.
*/
/* Try to center the player. */
var ulCameraNew = camera_move_center_player();
/* Do not move to the new position at one, but keep the moving speed at a maximum - except it is larger than one complete screen. */
var iDeltaX = ulCameraNew[0] - ulCameraX;
var iDeltaY = ulCameraNew[1] - ulCameraY;
var ulScreenSizeXPixels = sizScreenSizeXTiles * sizTileXPixels;
var ulScreenSizeYPixels = sizScreenSizeYTiles * sizTileYPixels;
var ulCameraNewX;
var ulCameraNewY;
var ulCameraMaxMovement = 8;
if( iDeltaX>ulScreenSizeXPixels || iDeltaY>ulScreenSizeYPixels )
{
/* The distance is too far, just warp there. */
ulCameraNewX = ulCameraNew[0];
ulCameraNewY = ulCameraNew[1];
}
else
{
/* Move there in slow movement. */
if( iDeltaX<(-ulCameraMaxMovement) )
{
iDeltaX = -ulCameraMaxMovement;
}
else if( iDeltaX>ulCameraMaxMovement )
{
iDeltaX = ulCameraMaxMovement;
}
if( iDeltaY<(-ulCameraMaxMovement) )
{
iDeltaY = -ulCameraMaxMovement;
}
else if( iDeltaY>ulCameraMaxMovement )
{
iDeltaY = ulCameraMaxMovement;
}
ulCameraNewX = ulCameraX + iDeltaX;
ulCameraNewY = ulCameraY + iDeltaY;
}
/* Set the new camera position. */
camera_update_position(ulCameraNewX, ulCameraNewY);
}
function refresh_map()
{
/*
* Refresh all buffers.
*/
var ulTileXStart = Math.floor(ulCameraX/sizTileXPixels);
var ulTileYStart = Math.floor(ulCameraY/sizTileYPixels);
var ulTileXEnd = ulTileXStart + sizScreenSizeXTiles + 1;
var ulTileYEnd = ulTileYStart + sizScreenSizeYTiles + 1;
for(var y=ulTileYStart; y<ulTileYEnd; y++)
{
for(var x=ulTileXStart; x<ulTileXEnd; x++)
{
redraw_tile(x,y);
}
}
draw_map();
}
function player_looses_life()
{
/* Make the angel sprite visible. */
tSpritePlayerAngel.fActive = true;
/* Start moving at the same position as the player. */
tSpritePlayerAngel.uiPosX = tSpritePlayer.uiPosX;
tSpritePlayerAngel.uiPosY = tSpritePlayer.uiPosY;
/* Show the "dead" animation. */
tSpritePlayer.uiAnimationTableIndex = 0;
tPlayer.tCurrentAnimation = PLAYERANIMATION_DEAD;
tSpritePlayer.auiAnimation = tPlayer.atAnimation.auiDead;
tSpritePlayer.uiAnimationStepDelay = 1;
tPlayer.tState = PLAYERSTATE_LIFELOST;
}
function collision_action_gold(tCollisionTestMode, uiLayer, uiTilePos)
{
var fCollision;
switch( tCollisionTestMode )
{
case COLLISIONTEST_WALK:
case COLLISIONTEST_FALL:
case COLLISIONTEST_JUMP:
/* Remove the gold bar from the layer. */
atRenderStack[uiLayer].atTiles[uiTilePos] = 0;
/* Request a redraw of the tile. */
aucReRe[uiTilePos>>>3] |= 1<<(uiTilePos&7);
/* Search the gold in the position table. */
var uiTargetPos = tMapParsed.atGold2Target[uiTilePos];
/* Get the stair tile. */
var ulTileIdx = atNamedTiles['gold_target'];
/* Place the new stair. */
atRenderStack[uiLayer].atTiles[uiTargetPos] = ulTileIdx;
/* Request a redraw of the tile. */
aucReRe[uiTargetPos>>>3] |= 1<<(uiTargetPos&7);
/* No collision. */
fCollision = 0;
break;
case COLLISIONTEST_STAND:
/* The player can not stand on gold. */
fCollision = 0;
break;
}
return fCollision;
}
function collision_action_snake(tCollisionTestMode, uiLayer, uiTilePos)
{
var fCollision;
switch( tCollisionTestMode )
{
case COLLISIONTEST_WALK:
case COLLISIONTEST_FALL:
case COLLISIONTEST_JUMP:
player_looses_life();
/* No collision. */
fCollision = 0;
/* TODO: Remove the snake? */
break;
case COLLISIONTEST_STAND:
/* The player can not stand on a snake. */
fCollision = 0;
break;
}
return fCollision;
}
function collision_action_spears(tCollisionTestMode, uiLayer, uiTilePos)
{
var fCollision;
switch( tCollisionTestMode )
{
case COLLISIONTEST_WALK:
case COLLISIONTEST_FALL:
case COLLISIONTEST_JUMP:
player_looses_life();
/* No collision. */
fCollision = 0;
break;
case COLLISIONTEST_STAND:
/* The player can not stand on spears. */
fCollision = 0;
break;
}
return fCollision;
}
function collision_action_mushroom_switch_activate(tCollisionTestMode, uiLayer, uiTilePos)
{
var fCollision;
/* Check if the players position is above the mushroom.
* Otherwise walking into the mushroom would also trigger the switch.
* This can be done by checking the jump state 'tPlayer.tJumpState'.
* If it is PLAYERJUMPSTATE_DOWN, the jump is going down.
*/
if( (tPlayer.tState==PLAYERSTATE_JUMPING && tPlayer.tJumpState==PLAYERJUMPSTATE_DOWN) || tPlayer.tState==PLAYERSTATE_FALLING )
{
/* Get the mushroom animation. */
var ulTileIdx = atNamedTiles['mushroom_switch_animation'];
var tAnimation = atAnimationsAll[ulTileIdx];
/* Clone the mushroom animation. */
var tClonedAnimation = animation_clone(tAnimation, uiLayer, uiTilePos);
/* Attach the new animation to the tile and add it to the local animations. */
atRenderStack[uiLayer].atAnimations[uiTilePos] = tClonedAnimation;
atAnimationsLocal.push(tClonedAnimation);
/* Set the first element of the new animation to prevent multiple switch activations. */
atRenderStack[uiLayer].atTiles[uiTilePos] = ulTileIdx;
}
switch( tCollisionTestMode )
{
/* The player can walk and jump through mushrooms. */
case COLLISIONTEST_WALK:
case COLLISIONTEST_JUMP:
fCollision = 0;
break;
/* The player can not fall through mushrooms. */
case COLLISIONTEST_FALL:
/* The player can stand on mushrooms. */
case COLLISIONTEST_STAND:
fCollision = 1;
break;
}
return fCollision;
}
/* This array maps the name of the collision action to the corresponding handler function. */
var atCollisionFunctions = {
'gold': collision_action_gold,
'snake': collision_action_snake,
'spears': collision_action_spears,
'mushroom_switch_activate': collision_action_mushroom_switch_activate
};
/* ulPlayerRadiusX = player collision box radius x
* ulPlayerRadiusY = player collision box radius y
* atPlayerPositions = an array of x/y pairs of the current movement to test. This is the center position.
* tCollisionTestMode = one collision mode to test
*/
function collision_test(ulPlayerRadiusX, ulPlayerRadiusY, atPlayerPositions, tCollisionTestMode)
{
/* Collect the collision information in this array. */
var ulNotCollidingEntries = 0;
var fCollision = 0;
/* Loop over all player position. */
for(var uiPositionCnt=0; uiPositionCnt<atPlayerPositions.length; uiPositionCnt++)
{
var tPosition = atPlayerPositions[uiPositionCnt];
/* Loop over all tiles which are covered by the collision box. */
var ulTileXStart = Math.floor((tPosition[0]-ulPlayerRadiusX)/sizTileXPixels);
var ulTileXEnd = Math.ceil( (tPosition[0]+ulPlayerRadiusX)/sizTileXPixels);
var ulTileYStart = Math.floor((tPosition[1]-ulPlayerRadiusY)/sizTileYPixels);
var ulTileYEnd = Math.ceil( (tPosition[1]+ulPlayerRadiusY)/sizTileYPixels);
for(var uiY=ulTileYStart; uiY<ulTileYEnd; uiY++)
{
for(var uiX=ulTileXStart; uiX<ulTileXEnd; uiX++)
{
/* Loop over all layers at this position. */
var sizRenderStack = atRenderStack.length;