-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
1609 lines (1365 loc) · 43.9 KB
/
main.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
// Player/enemy state
function st(elem)
{
this.e=(elem||null); // DOM element
this.keystate=0; // keyboard bitfield [action][down][right][up][left]
this.padstate=0; // gamepad bitfield [action][down][right][up][left]
this.x=0; // x position
this.y=0; // y position
this.px=0; // previous x position
this.py=0; // previous y position
this.sx=0; // start x position
this.sy=0; // start y position
this.w=0; // width
this.h=0; // height
this.vs=0; // current vertical speed
this.hs=0; // current horizontal speed
this.j=false; // jumping
this.f=false; // falling
this.d=false; // ducking
this.htime=0; // hurt following an enemy collision
this.dir=0; // direction (-1=left, 0=none, 1=right)
this.hsp=5; // max horizontal speed
this.vsp=15; // max vertical speed
this.speed=5; // walking speed
this.jumpspeed=15; // jumping speed
this.c=0; // coyote timer (time after leaving ground where you can still jump)
this.lf=100; // remaining "life force"
}
// Game state
var gs={
// animation frame of reference
step:(1/60), // target step time @ 60 fps
acc:0, // accumulated time since last frame
lasttime:0, // time of last frame
// control state
gamepad:-1,
gamepadbuttons:[],
gamepadaxes:[],
gamepadassignbutton:-1,
gamepadlastbutton:-1,
// physics in pixels per frame @ 60fps
gravity:0.5,
terminalvelocity:25,
friction:1,
// entities
player:new st(),
enemies:[],
// level related
level:0,
tiles:[],
tilerows:0,
tilecolumns:0,
tilewidth:64,
tileheight:64,
things:[], // collectables
score:0,
scale:1,
// audio related
dialler:new dtmf_dial(),
music:new gen_music(),
randoms:new randomizer(),
writer:new textwriter(),
timeline:new timelineobj(),
state:0 // state machine, 0=intro, 1=menu, 2=playing, 3=complete
};
// Clear both keyboard and gamepad input state
function clearinputstate(character)
{
character.keystate=0;
character.padstate=0;
}
// Check if an input is set in either keyboard or gamepad input state
function ispressed(character, keybit)
{
return (((character.keystate&keybit)!=0) || ((character.padstate&keybit)!=0));
}
// Scan for any connected gamepads
function gamepadscan()
{
var gamepads=navigator.getGamepads();
var found=0;
var i=0;
var gleft=false;
var gright=false;
var gup=false;
var gdown=false;
var gjump=false;
// Find active pads
for (var padid=0; padid<gamepads.length; padid++)
{
// Only support first found gamepad
if ((found==0) && (gamepads[padid] && gamepads[padid].connected))
{
found++;
// If we don't already have this one, add mapping for it
if (gs.gamepad!=padid)
{
console.log("Found new gamepad "+padid+" '"+gamepads[padid].id+"'");
gs.gamepad=padid;
if (gamepads[padid].mapping==="standard")
{
// Browser supported "standard" gamepad
gs.gamepadbuttons[0]=14; // left (left) d-left
gs.gamepadbuttons[1]=15; // right (left) d-right
gs.gamepadbuttons[2]=12; // top (left) d-up
gs.gamepadbuttons[3]=13; // bottom (left) d-down
gs.gamepadbuttons[4]=0; // bottom button (right) x
gs.gamepadaxes[0]=0; // left/right axis
gs.gamepadaxes[1]=1; // up/down axis
gs.gamepadaxes[2]=2; // cam left/right axis
gs.gamepadaxes[3]=3; // cam up/down axis
}
else
if (gamepads[padid].id=="054c-0268-Sony PLAYSTATION(R)3 Controller")
{
// PS3 DualShock 3
gs.gamepadbuttons[0]=15; // left (left) d-left
gs.gamepadbuttons[1]=16; // right (left) d-right
gs.gamepadbuttons[2]=13; // top (left) d-up
gs.gamepadbuttons[3]=14; // bottom (left) d-down
gs.gamepadbuttons[4]=0; // bottom button (right) x
gs.gamepadaxes[0]=0; // left/right axis
gs.gamepadaxes[1]=1; // up/down axis
gs.gamepadaxes[2]=3; // cam left/right axis
gs.gamepadaxes[3]=4; // cam up/down axis
}
else
if (gamepads[padid].id=="045e-028e-Microsoft X-Box 360 pad")
{
// XBOX 360
// 8Bitdo GBros. Adapter (XInput mode)
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=0; // bottom button (right) x
gs.gamepadaxes[0]=6; // left/right axis
gs.gamepadaxes[1]=7; // up/down axis
gs.gamepadaxes[2]=3; // cam left/right axis
gs.gamepadaxes[3]=4; // cam up/down axis
}
else
if (gamepads[padid].id=="0f0d-00c1- Switch Controller")
{
// Nintendo Switch
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=1; // bottom button (right) x
gs.gamepadaxes[0]=4; // left/right axis
gs.gamepadaxes[1]=5; // up/down axis
gs.gamepadaxes[2]=2; // cam left/right axis
gs.gamepadaxes[3]=3; // cam up/down axis
}
else
if ((gamepads[padid].id=="054c-05c4-Sony Computer Entertainment Wireless Controller") || (gamepads[padid].id=="045e-02e0-8Bitdo SF30 Pro") || (gamepads[padid].id=="045e-02e0-8BitDo GBros Adapter"))
{
// PS4 DualShock 4
// 8Bitdo SF30 Pro GamePad (XInput mode)
// 8Bitdo GBros. Adapter (XInput mode)
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=0; // bottom button (right) x
gs.gamepadaxes[0]=0; // left/right axis
gs.gamepadaxes[1]=1; // up/down axis
gs.gamepadaxes[2]=3; // cam left/right axis
gs.gamepadaxes[3]=4; // cam up/down axis
}
else
if ((gamepads[padid].id=="054c-0ce6-Sony Interactive Entertainment Wireless Controller") || (gamepads[padid].id=="054c-0ce6-Wireless Controller"))
{
// PS5 DualSense
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=1; // bottom button (right) x
gs.gamepadaxes[0]=0; // left/right axis
gs.gamepadaxes[1]=1; // up/down axis
gs.gamepadaxes[2]=2; // cam left/right axis
gs.gamepadaxes[3]=5; // cam up/down axis
}
else
if ((gamepads[padid].id=="057e-2009-Pro Controller") || (gamepads[padid].id=="18d1-9400-Google Inc. Stadia Controller") || (gamepads[padid].id=="18d1-9400-Google LLC Stadia Controller rev. A") || (gamepads[padid].id.match("/^18d1-9400-Stadia/i")))
{
// Nintendo Switch Pro Controller
// 8Bitdo SF30 Pro GamePad (Switch mode)
// 8Bitdo GBros. Adapter (Switch mode)
// Google Stadia Controller (Wired and Bluetooth)
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=0; // bottom button (right) x (a on Stadia)
gs.gamepadaxes[0]=0; // left/right axis
gs.gamepadaxes[1]=1; // up/down axis
gs.gamepadaxes[2]=2; // cam left/right axis
gs.gamepadaxes[3]=3; // cam up/down axis
}
else
if (gamepads[padid].id=="2dc8-6100-8Bitdo SF30 Pro")
{
// 8Bitdo SF30 Pro GamePad (DInput mode)
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=1; // bottom button (right) x
gs.gamepadaxes[0]=0; // left/right axis
gs.gamepadaxes[1]=1; // up/down axis
gs.gamepadaxes[2]=2; // cam left/right axis
gs.gamepadaxes[3]=3; // cam up/down axis
}
else
{
// Unknown non-"standard" mapping
gs.gamepadbuttons[0]=-1; // left (left) d-left
gs.gamepadbuttons[1]=-1; // right (left) d-right
gs.gamepadbuttons[2]=-1; // top (left) d-up
gs.gamepadbuttons[3]=-1; // bottom (left) d-down
gs.gamepadbuttons[4]=-1; // bottom button (right) x
gs.gamepadaxes[0]=-1; // left/right axis
gs.gamepadaxes[1]=-1; // up/down axis
gs.gamepadaxes[2]=-1; // cam left/right axis
gs.gamepadaxes[3]=-1; // cam up/down axis
}
}
// Check analog axes
for (i=0; i<gamepads[padid].axes.length; i++)
{
var val=gamepads[padid].axes[i];
if (i==gs.gamepadaxes[0])
{
if (val<-0.5) // Left
gleft=true;
if (val>0.5) // Right
gright=true;
}
if (i==gs.gamepadaxes[1])
{
if (val<-0.5) // Up
gup=true;
if (val>0.5) // Down
gdown=true;
}
}
// Check buttons
for (i=0; i<gamepads[padid].buttons.length; i++)
{
var val=gamepads[padid].buttons[i];
var pressed=val==1.0;
if (typeof(val)=="object")
{
pressed=val.pressed;
val=val.value;
}
if (pressed)
{
switch (i)
{
case gs.gamepadbuttons[0]: gleft=true; break;
case gs.gamepadbuttons[1]: gright=true; break;
case gs.gamepadbuttons[2]: gup=true; break;
case gs.gamepadbuttons[3]: gdown=true; break;
case gs.gamepadbuttons[4]: gjump=true; break;
default: break;
}
}
}
// Update padstate
if (gup)
gs.player.padstate|=2;
else
gs.player.padstate&=~2;
if (gdown)
gs.player.padstate|=8;
else
gs.player.padstate&=~8;
if (gleft)
gs.player.padstate|=1;
else
gs.player.padstate&=~1;
if (gright)
gs.player.padstate|=4;
else
gs.player.padstate&=~4;
if (gjump)
{
gs.player.padstate|=16;
// If in menu start playing
if (gs.state==1)
{
hide_screen();
gs.state=2;
launchgame(0);
}
// If in intro, skip to menu
if (gs.state==0)
{
gs.timeline.end();
hide_screen();
gs.state=1;
show_title();
start_music();
}
}
else
gs.player.padstate&=~16;
// Output button debug for unknown gamepads
if ((gs.gamepadbuttons[0]==-1) && (gs.gamepadaxes[0]==-1))
{
if (pressed)
console.log("Pressed "+i);
}
}
}
// Detect disconnect
if ((found==0) && (gs.gamepad!=-1))
{
console.log("Disconnected gamepad "+padid);
gs.gamepad=-1;
}
window.requestAnimationFrame(gamepadscan);
}
// Has this level been completed?
function levelcomplete()
{
// Defined as - all enemies defeated and all things collected
if ((gs.enemies.length==0) && (gs.things.length==0))
return true;
return false;
}
// Redraw the game world
function redraw()
{
// Move the player
gs.player.e.style.left=gs.player.x+"px";
gs.player.e.style.top=gs.player.y+"px";
// Move all the enemies
for (var i=0; i<gs.enemies.length; i++)
{
gs.enemies[i].e.style.left=gs.enemies[i].x+"px";
gs.enemies[i].e.style.top=gs.enemies[i].y+"px";
}
// Scroll the screen to keep the player in view
if ((gs.player.x!=gs.player.px) || (gs.player.y!=gs.player.py))
{
try
{
window.scrollTo({left:(gs.player.x*gs.scale)-(window.innerWidth/2), top:(gs.player.y*gs.scale)-(window.innerHeight/2), behaviour:"smooth"});
}
catch (e)
{
// Fallback to 2 parameters for older browsers
window.scrollTo((gs.player.x*gs.scale)-(window.innerWidth/2), (gs.player.y*gs.scale)-(window.innerHeight/2));
}
}
// Update previous positions
gs.player.px=gs.player.x;
gs.player.py=gs.player.y;
}
// Does DOM element a overlap with element b
function overlap(a, b)
{
// Check horiz
if (a.offsetLeft<b.offsetLeft)
if ((a.offsetLeft+a.clientWidth)<b.offsetLeft) return false;
if (a.offsetLeft>b.offsetLeft)
if ((b.offsetLeft+b.clientWidth)<a.offsetLeft) return false;
// Check vert
if (a.offsetTop<b.offsetTop)
if ((a.offsetTop+a.clientHeight)<b.offsetTop) return false;
if (a.offsetTop>b.offsetTop)
if ((b.offsetTop+b.clientHeight)<a.offsetTop) return false;
return true;
}
// Check if character collides with a tile
function collide(character, x, y)
{
// Make a collision box for the character in the centre/bottom of their sprite
// 1/2 the width and 1/2 the height to allow for overlaps
var pos={
offsetLeft:x+(character.w/3),
offsetTop:y+(character.h/2),
clientWidth:(character.w/4),
clientHeight:(character.h/2)
};
// look through all tiles for a collision
for (var index=0; index<gs.tiles.length; index++)
{
// does this tile overlap with character?
if (overlap(gs.tiles[index], pos))
return true;
}
return false;
}
// Move character by up to horizontal/vertical speeds, stopping when a collision occurs
function collisioncheck(character)
{
// check for horizontal collisions
if (collide(character, character.x+character.hs, character.y))
{
// A collision occured, so move the character until it hits
while (!collide(character, character.x+(character.hs>0?1:-1), character.y))
character.x+=(character.hs>0?1:-1);
// Stop horizontal movement
character.hs=0;
}
character.x+=character.hs;
/*
// Climb stairs, TODO - revisit this if time allowing
if ((character==gs.player) // it's the player
&& ((character.keystate!=0) || (character.padstate!=0)) // key still pressed
&& (character.dir!=0) // was moving
&& (character.hs==0) // horizontal collision occured
&& (!collide(character, character.x, character.y-character.h)) // nothing above us
&& (!collide(character, character.x+(character.w*character.dir), character.y-character.h))) // nothing above and to right
{
character.j=true;
character.vs=-(character.jumpspeed/4);
}
*/
// check for vertical collisions
if (collide(character, character.x, character.y+character.vs))
{
// A collision occured, so move the character until it hits
while (!collide(character, character.x, character.y+(character.vs>0?1:-1)))
character.y+=(character.vs>0?1:-1);
// Stop vertical movement
character.vs=0;
}
character.y+=character.vs;
}
// If the player has moved "off" the map, then put them back at a start position
// this "shouldn't" happen with the border surrounding the level
function offmapcheck(character)
{
if ((character.x<0) || (character.y>levels[gs.level].height*levels[gs.level].tileheight))
{
character.x=character.sx;
character.y=character.sy;
}
}
// Check for player being on the ground
function groundcheck(character)
{
// Check for coyote time
if (character.c>0)
character.c--;
// Check we are on the ground
if (collide(character, character.x, character.y+1))
{
character.vs=0;
character.j=false;
character.f=false;
character.c=15;
// Check for jump pressed, when not ducking
if ((ispressed(character, 16)) && (!character.d))
{
character.j=true;
character.vs=-character.jumpspeed;
}
}
else
{
// Check for jump pressed, when not ducking, and coyote time not expired
if ((ispressed(character, 16)) && (!character.d) && (character.j==false) && (character.c>0))
{
character.j=true;
character.vs=-character.jumpspeed;
}
// We're in the air, increase falling speed until we're at terminal velocity
if (character.vs<gs.terminalvelocity)
character.vs+=gs.gravity;
// Set falling flag when vertical speed is positive
if (character.vs>0)
character.f=true;
}
}
// Check for mid jump when the player is now falling
function jumpcheck(character)
{
// When jumping ..
if (character.j)
{
// Check if loosing altitude
if (character.vs>=0)
{
character.j=false;
character.f=true;
}
}
}
// Handle ducking and slowing player down by friction
function standcheck(character)
{
// Check for ducking, or injured
if ((ispressed(character, 8)) || (character.htime>0))
character.d=true;
else
character.d=false;
// When no horizontal movement pressed, slow down by friction
if (((!ispressed(character, 1)) && (!ispressed(character, 4))) ||
((ispressed(character, 1)) && (ispressed(character, 4))))
{
// Going left
if (character.dir==-1)
{
if (character.hs<0)
{
character.hs+=gs.friction;
}
else
{
character.hs=0;
character.dir=0;
}
}
// Going right
if (character.dir==1)
{
if (character.hs>0)
{
character.hs-=gs.friction;
}
else
{
character.hs=0;
character.dir=0;
}
}
}
}
// Process all enemies and simulate keypresses for basic AI when they can move
function updateenemyai(character)
{
// Check we are on the ground
if (collide(character, character.x, character.y+1))
{
var tmpstate=0;
// If we're not moving left/right, then start moving
if (character.dir==0)
{
// If nothing to our right, then move right so long as there is no drop
if ((!collide(character, character.x+1, character.y))
&& ((collide(character, character.x+(character.w/2), character.y+character.h))))
tmpstate|=4;
// try left
if ((tmpstate==0)
&& (!collide(character, character.x-1, character.y))
&& (collide(character, character.x-(character.w/2), character.y+character.h)))
tmpstate|=1;
character.keystate|=tmpstate;
}
else // if moving right
if (character.dir==1)
{
if ((collide(character, character.x+1, character.y))
|| (!collide(character, character.x+(character.w/2), character.y+character.h)))
clearinputstate(character);
}
else // if moving left
if (character.dir==-1)
{
if ((collide(character, character.x-1, character.y))
|| (!collide(character, character.x-(character.w/2), character.y+character.h)))
clearinputstate(character);
}
}
}
// Update the animation state of players/enemies
// this is so that the CSS animations or poses are actioned
function updateanimation(character)
{
switch (character.dir)
{
case -1: // Left
character.e.classList.add("left");
character.e.classList.remove("right");
break;
case 0: // Not moving
character.e.classList.remove("left");
character.e.classList.remove("right");
break;
case 1: // Right
character.e.classList.remove("left");
character.e.classList.add("right");
break;
default:
break;
}
// Jumping
if (character.j)
character.e.classList.add("jump");
else
character.e.classList.remove("jump");
// Falling
if (character.f)
character.e.classList.add("fall");
else
character.e.classList.remove("fall");
// Ducking
if ((character.d) || (character.htime>0))
character.e.classList.add("duck");
else
character.e.classList.remove("duck");
// Hurt
if (character.htime>0)
character.e.classList.add("hurt");
else
character.e.classList.remove("hurt");
// Not moving
if ((character.dir==0) && (character.hs==0) && (character.vs==0))
character.e.classList.add("idle");
else
character.e.classList.remove("idle");
// Walking
if ((character.dir!=0) && (!character.j) && (!character.f))
character.e.classList.add("walk");
else
character.e.classList.remove("walk");
}
// Update the position of players/enemies
function updatemovements(character)
{
// Check if player has left the map
offmapcheck(character);
// Check if player on the ground or falling
groundcheck(character);
// Process jumping
jumpcheck(character);
// Move player by appropriate amount, up to a collision
collisioncheck(character);
// If no input detected, slow the player using friction
standcheck(character);
// Move player when a key is pressed
if ((character.keystate!=0) || (character.padstate!=0))
{
// Left key
if ((ispressed(character, 1)) && (!ispressed(character, 4)))
{
character.hs=character.htime==0?-character.speed:-2;
character.dir=-1;
}
// Right key
if ((ispressed(character, 4)) && (!ispressed(character, 1)))
{
character.hs=character.htime==0?character.speed:2;
character.dir=1;
}
}
// Decrease hurt timer
if (character.htime>0) character.htime--;
// Apply CSS rules to match character state
updateanimation(character);
}
// Remove all tiles which match given id
function removetilebyid(id)
{
var removed;
do
{
removed=0;
for (var i=0; i<gs.tiles.length; i++)
{
if (gs.tiles[i].id==id)
{
gs.tiles[i].e.remove(); // remove from DOM
gs.tiles.splice(i, 1); // remove from tile list
removed++;
break;
}
}
} while (removed>0);
}
// Determine distance (Hypotenuse) between two lengths in 2d space (using Pythagoras)
function calcHypotenuse(a, b)
{
return(Math.sqrt((a * a) + (b * b)));
}
// Remove the neareset tile matching a given id to an x,y position
function removenearesttilebyid(x, y, id)
{
var nearest=-1;
var neardelta=-1;
for (var i=0; i<gs.tiles.length; i++)
{
if (gs.tiles[i].id==id)
{
var delta=calcHypotenuse(Math.abs(x-gs.tiles[i].offsetLeft), Math.abs(y-gs.tiles[i].offsetTop));
if ((neardelta==-1) || (delta<neardelta))
{
nearest=i;
neardelta=delta;
}
}
}
// If a tile was found, then remove it
if (nearest!=-1)
{
gs.tiles[nearest].e.remove(); // remove from DOM
gs.tiles.splice(nearest, 1); // remove from tile list
}
}
// Clear set of items from DOM and array
function clearobjects(items)
{
for (var i=0; i<items.length; i++)
items[i].e.remove(); // remove from DOM
items.splice(0, items.length); // clear array
}
// Check if player collides with a collectable item
function checkplayercollectable(character)
{
// Make a collision box for the character in the centre/bottom of their sprite
// 1/2 the width and 1/2 the height to allow for overlaps
var ppos={
offsetLeft:character.x+(character.w/4),
offsetTop:character.y+(character.h/2),
clientWidth:(character.w/2),
clientHeight:(character.h/2)
};
// look through all enemies for a collision
for (var i=0; i<gs.things.length; i++)
{
var tpos={
offsetLeft:gs.things[i].x,
offsetTop:gs.things[i].y,
clientWidth:gs.things[i].w,
clientHeight:gs.things[i].h
};
// does this thing overlap with character?
if (overlap(tpos, ppos))
{
switch (gs.things[i].id)
{
case 21: // cube
gs.score+=5;
gs.music.play_collect(0);
break;
case 22: // red key
removenearesttilebyid(gs.things[i].x, gs.things[i].y, 6);
gs.music.play_collect(1);
break;
case 23: // green key
removenearesttilebyid(gs.things[i].x, gs.things[i].y, 7);
gs.music.play_collect(1);
break;
default:
break;
}
// Remove thing that was collected
gs.things[i].e.remove();
gs.things.splice(i, 1);
return;
}
}
}
// Check for collision between player and an enemy
function checkplayerenemy(character)
{
// Make a collision box for the character in the centre/bottom of their sprite
// 1/2 the width and 1/2 the height to allow for overlaps
var ppos={
offsetLeft:character.x+(character.w/4),
offsetTop:character.y+(character.h/2),
clientWidth:(character.w/2),
clientHeight:(character.h/2)
};
// look through all enemies for a collision
for (var i=0; i<gs.enemies.length; i++)
{
var epos={
offsetLeft:gs.enemies[i].x+(gs.enemies[i].w/4),
offsetTop:gs.enemies[i].y+(gs.enemies[i].h/2),
clientWidth:(gs.enemies[i].w/2),
clientHeight:(gs.enemies[i].h/2)
};
// does this enemy overlap with character?
if (overlap(epos, ppos))
{
// Remove enemy if hit from above whilst player falling
if (((ppos.offsetTop+(ppos.clientHeight/3))<epos.offsetTop) && (character.f))
{
gs.enemies[i].e.remove();
gs.enemies.splice(i, 1);
character.j=true;
character.f=false;
character.vs=-(character.jumpspeed/2);
}
else
{
// Loose health (if not already hurt)
if (character.htime==0)
{
character.lf-=(character.d==true?5:10);
showhealth();
// Check for game over
if (character.lf<=0)
{
gs.state=1;
// Clear the playfield
clearplayfield();
// Clear player
document.getElementById("player").innerHTML="";
show_title();
}
character.htime=60;
character.d=true;
}
}
return;
}
}
}
// Create a <style> element for text
function buildalphablockstyle(pixelsize)
{
return "<style>.alphablock { font-size:0px; display:inline-block; margin-bottom: "+(pixelsize/3)+"px; } .block { display:inline-block; width:"+pixelsize+"px; height:"+pixelsize+"px; border-top-left-radius:"+(pixelsize/2)+"px; border-bottom-right-radius:"+(pixelsize/2)+"px; } .filled { background-color:#00ff00; background: linear-gradient(to bottom, rgba(0,255,0,0) 0%,rgba(0,255,0,1) 33%,rgba(0,255,0,1) 66%,rgba(0,255,0,0) 100%); }</style>";
}
// Update the game state prior to rendering
function update()
{
// Apply keystate/physics to player
updatemovements(gs.player);
// Apply keystate/physics to enemies
for (var i=0; i<gs.enemies.length; i++)
{
updateenemyai(gs.enemies[i]);
updatemovements(gs.enemies[i]);
}
// Check for player/enemy collision
checkplayerenemy(gs.player);
// Check for player/collectable collision
checkplayercollectable(gs.player);
}
// Request animation frame callback
function rafcallback(timestamp)
{
// First time round, just save epoch
if (gs.lasttime>0)
{
// Determine accumulated time since last call
gs.acc+=((timestamp-gs.lasttime) / 1000);
// If it's more than 15 seconds since last call, reset
if ((gs.acc>gs.step) && ((gs.acc/gs.step)>(60*15)))
gs.acc=gs.step*2;
// Process "steps" since last call
while (gs.acc>gs.step)
{
update();
gs.acc-=gs.step;
}
// If the update took us out of play state then stop now
if (gs.state!=2)
return;
// Check for level complete
if (levelcomplete())
{
var level=gs.level+1;