forked from Fulox/FullScreenMario-JSON
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FullScreenMario.js
8873 lines (7937 loc) · 316 KB
/
FullScreenMario.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
/**
* FullScreenMario
*
* A free HTML5 remake of Nintendo's original Super Mario Bros, expanded for the
* modern web. It includes the original 32 levels, a random map generator, a
* level editor, and over a dozen custom mods.
*
* @example
* // Creating a 15 x 14.5 blocks sized FullScreenMario object.
* var FSM = new FullScreenMario({
* "width": 480,
* "height": 464
* });
*
* @example
* // Creating a 15 x 14.5 blocks sized FullScreenMario object and logging the
* // amount of time each reset function took.
* var FSM = new FullScreenMario({
* "width": 480,
* "height": 464,
* "resetTimed": true
* });
* console.log(FSM.resetTimes);
*
* @example
* // Creating a full-screen FullScreenMario object with a few mods.
* var FSM = new FullScreenMario({
* "width": window.innerWidth,
* "height": window.innerHeight,
* "mods": {
* "Luigi": true,
* "HorizClouds": true,
* "High Speed": true,
* "Super Fireballs": true
* }
* });
*
* @example
* // Binding the FullScreenMario object controls to the body's mouse and key
* // events, and starting the game.
* window.FSM = new FullScreenMario({
* "width": window.innerWidth,
* "height": window.innerHeight
* });
*
* document.body.appendChild(FSM.container);
*
* FSM.proliferate(document.body, {
* "onkeydown": FSM.InputWriter.makePipe("onkeydown", "keyCode", true),
* "onkeyup": FSM.InputWriter.makePipe("onkeyup", "keyCode", true),
* "onmousedown": FSM.InputWriter.makePipe("onmousedown", "which", true)
* });
*
* FSM.gameStart();
*/
var FullScreenMario = (function(GameStartr) {
"use strict";
// Use a GameStartr as the class parent, with GameStartr's constructor
var GameStartrProto = new GameStartr(),
// Used for combining arrays from the prototype to this
proliferate = GameStartrProto.proliferate,
proliferateHard = GameStartrProto.proliferateHard;
// Subsequent settings will be stored in FullScreenMario.prototype.settings
GameStartrProto.settings = {};
/**
* Constructor for a new FullScreenMario game object.
* Static game settings are stored in the appropriate settings/*.js object
* as members of the FullScreenMario.prototype object.
* Dynamic game settings may be given as members of the "customs" argument.
* On typical machines, game startup time is approximately 500-700ms.
*
* @constructor
* @param {Number} width Width of the game viewport: at least 480.
* @param {Number} height Height of the game viewport: at least 464.
* @param {Boolean} [resetTimes] Whether the amount of time in of each
* reset function (in millseconds) should be
* stored as a member .resetTimes (by default,
* false).
* @param {Object} [style] Additional CSS styles to be given to the
* game's container <div> element.
* @return {FullScreenMario}
*/
function FullScreenMario(customs) {
// Call the parent GameStartr constructor to set the base settings and
// verify the prototype requirements
GameStartr.call(this, {
"customs": customs,
"constructor": FullScreenMario,
"requirements": {
"settings": {
"audio": "settings/audio.js",
"collisions": "settings/collisions.js",
"editor": "settings/editor.js",
"events": "settings/events.js",
"generator": "settings/generator.js",
"input": "settings/inpug.js",
"maps": "settings/maps.js",
"mods": "settings/mods.js",
"numbers": "settings/number.js",
"objects": "settings/objetcs.js",
"quadrants": "settings/quadrants.js",
"renderer": "settings/renderer.js",
"runner": "settings/runner.js",
"sprites": "settings/sprites.js",
"statistics": "settings/statistics.js",
"ui": "settings/ui.js",
}
},
"constants": [
"unitsize",
"scale",
"gravity",
"pointLevels",
"customTextMappings"
]
});
if (customs.resetTimed) {
this.resetTimes = this.resetTimed(this, customs);
} else {
this.reset(this, customs);
}
}
FullScreenMario.prototype = GameStartrProto;
// For the sake of reset functions, store constants as members of the actual
// FullScreenMario Function itself - this allows prototype setters to use
// them regardless of whether the prototype has been instantiated yet.
FullScreenMario.unitsize = 4;
FullScreenMario.scale = FullScreenMario.unitsize / 2;
// Gravity is always a function of unitsize (and about .48)
FullScreenMario.gravity = Math.round(12 * FullScreenMario.unitsize) / 100;
// Levels of points to award for hopping on / shelling enemies
FullScreenMario.pointLevels = [
100, 200, 400, 500, 800, 1000, 2000, 4000, 5000, 8000
];
// Useful for custom text, where "text!" cannot be a Function name
FullScreenMario.customTextMappings = {
" ": "Space",
".": "Period",
"!": "ExclamationMark",
":": "Colon",
"/": "Slash",
"©": "Copyright"
};
/* Resets
*/
/**
* Sets self.AudioPlayer.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): AudioPlayr (src/AudioPlayr/AudioPlayr.js)
* audio.js (settings/audio.js)
*/
function resetAudioPlayer(EightBitter, customs) {
GameStartr.prototype.resetAudioPlayer(EightBitter, customs);
EightBitter.AudioPlayer.setGetVolumeLocal(
EightBitter.getVolumeLocal.bind(EightBitter, EightBitter)
);
EightBitter.AudioPlayer.setGetThemeDefault(
EightBitter.getAudioThemeDefault.bind(EightBitter, EightBitter)
);
}
/**
* Sets self.ThingHitter.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): ThingHittr (src/ThingHittr/ThingHittr.js)
* collisions.js (settings/collisions.js)
*/
function resetThingHitter(EightBitter, customs) {
GameStartr.prototype.resetThingHitter(EightBitter, customs);
EightBitter.ThingHitter.cacheHitCheckGroup("Solid");
EightBitter.ThingHitter.cacheHitCheckGroup("Character");
}
/**
* Sets self.MapsHandler.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): MapsHandlr (src/MapsHandlr/MapsHandlr.js)
* maps.js (settings/maps.js)
*/
function resetMapsHandler(EightBitter, customs) {
EightBitter.MapsHandler = new MapsHandlr({
"MapsCreator": EightBitter.MapsCreator,
"MapScreener": EightBitter.MapScreener,
"screenAttributes": EightBitter.settings.maps.screenAttributes,
"onSpawn": EightBitter.settings.maps.onSpawn,
"stretchAdd": EightBitter.mapAddStretched.bind(EightBitter),
"onStretch": EightBitter.mapStretchThing,
"afterAdd": EightBitter.mapAddAfter.bind(EightBitter)
});
}
/**
* Resets self.StatsHolder via the parent GameStartr resetStatsHolder.
*
* If the screen isn't wide enough to fit the 'lives' display, it's hidden.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
*/
function resetStatsHolder(EightBitter, customs) {
GameStartr.prototype.resetStatsHolder(EightBitter, customs);
if (customs.width < 560) {
EightBitter.StatsHolder.getContainer().children[0].cells[4].style.display = "none";
}
}
/**
* Sets self.container via the parent GameStartr resetContaienr.
*
* The container is given the "Press Start" font, the PixelRender is told
* to draw the scenery, solid, character, and text groups, and the container
* width is set to the custom's width.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
*/
function resetContainer(EightBitter, customs) {
GameStartr.prototype.resetContainer(EightBitter, customs);
EightBitter.container.style.fontFamily = "Press Start";
EightBitter.container.className += " FullScreenMario";
EightBitter.PixelDrawer.setThingArrays([
EightBitter.GroupHolder.getSceneryGroup(),
EightBitter.GroupHolder.getSolidGroup(),
EightBitter.GroupHolder.getCharacterGroup(),
EightBitter.GroupHolder.getTextGroup()
]);
EightBitter.StatsHolder.getContainer().style.width = customs.width + "px";
EightBitter.container.appendChild(EightBitter.StatsHolder.getContainer());
}
/* Global manipulations
*/
/**
* Completely restarts the game. Lives are reset to 3, the map goes back
* to default, and the onGameStart mod trigger is fired.
*
* @this {EightBittr}
*/
function gameStart() {
var EightBitter = EightBittr.ensureCorrectCaller(this);
EightBitter.setMap(
EightBitter.settings.maps.mapDefault,
EightBitter.settings.maps.locationDefault
);
EightBitter.StatsHolder.set(
"lives", EightBitter.settings.statistics.values.lives.valueDefault
);
EightBitter.ModAttacher.fireEvent("onGameStart");
}
/**
* Completely ends the game. All Thing groups are clared, sounds are
* stopped, the screen goes to black, "GAME OVER" is displayed. After a
* while, the game restarts again via gameStart.
*/
function gameOver() {
var EightBitter = EightBittr.ensureCorrectCaller(this),
text = EightBitter.ObjectMaker.make("CustomText", {
"texts": [{
"text": "GAME OVER"
}]
}),
texts, textWidth, dx, i;
EightBitter.killNPCs();
EightBitter.AudioPlayer.clearAll();
EightBitter.AudioPlayer.play("Game Over");
EightBitter.GroupHolder.clearArrays();
EightBitter.StatsHolder.hideContainer();
EightBitter.TimeHandler.cancelAllEvents();
EightBitter.PixelDrawer.setBackground("black");
EightBitter.addThing(
text,
EightBitter.MapScreener.width / 2,
EightBitter.MapScreener.height / 2
);
texts = text.children;
textWidth = -(texts[texts.length - 1].right - texts[0].left) / 2;
for (i = 0; i < texts.length; i += 1) {
EightBitter.shiftHoriz(texts[i], textWidth);
}
EightBitter.TimeHandler.addEvent(function () {
EightBitter.gameStart();
EightBitter.StatsHolder.displayContainer();
}, 420);
EightBitter.ModAttacher.fireEvent("onGameOver");
}
/**
* Slight addition to the GameStartr thingProcess Function. The Thing's hit
* check type is cached immediately.
*
* @see GameStartr::thingProcess
*/
function thingProcess(thing, type, settings, defaults) {
GameStartr.prototype.thingProcess(thing, type, settings, defaults);
// ThingHittr becomes very non-performant if functions aren't generated
// for each Thing constructor (optimization does not respect prototypal
// inheritance, sadly).
thing.EightBitter.ThingHitter.cacheHitCheckType(
thing.title,
thing.groupType
);
}
/**
* Adds a Thing via addPreThing based on the specifications in a PreThing.
* This is done relative to MapScreener.left and MapScreener.floor.
*
* @param {PreThing} prething
*/
function addPreThing(prething) {
var thing = prething.thing,
position = prething.position || thing.position;
thing.EightBitter.addThing(
thing,
prething.left * thing.EightBitter.unitsize - thing.EightBitter.MapScreener.left,
(thing.EightBitter.MapScreener.floor - prething.top) * thing.EightBitter.unitsize
);
// Either the prething or thing, in that order, may request to be in the
// front or back of the container
if (position) {
thing.EightBitter.TimeHandler.addEvent(function () {
switch (position) {
case "beginning":
thing.EightBitter.arrayToBeginning(thing, thing.EightBitter.GroupHolder.getGroup(thing.groupType));
break;
case "end":
thing.EightBitter.arrayToEnd(thing, thing.EightBitter.GroupHolder.getGroup(thing.groupType));
break;
}
});
}
thing.EightBitter.ModAttacher.fireEvent("onAddPreThing", prething);
}
/**
* Adds a new Player Thing to the game and sets it as EightBitter.play. Any
* required additional settings (namely keys, power/size, and swimming) are
* applied here.
*
* @this {EightBittr}
* @param {Number} [left] A left coordinate to place the Thing at (by
* default, unitsize * 16).
* @param {Number} [bottom] A bottom coordinate to place the Thing upon
* (by default, unitsize * 16).
*/
function addPlayer(left, bottom) {
var EightBitter = EightBittr.ensureCorrectCaller(this),
player;
player = EightBitter.player = EightBitter.ObjectMaker.make("Player", {
"power": EightBitter.StatsHolder.get("power")
});
player.keys = player.getKeys();
EightBitter.InputWriter.setEventInformation(player);
if (EightBitter.MapScreener.underwater) {
player.swimming = true;
EightBitter.TimeHandler.addClassCycle(player, [
"swim1", "swim2"
], "swimming", 5);
EightBitter.TimeHandler.addEventInterval(
player.EightBitter.animatePlayerBubbling,
96, Infinity,
player
);
}
EightBitter.setPlayerSizeSmall(player);
if (player.power >= 2) {
EightBitter.playerGetsBig(player, true);
if (player.power === 3) {
EightBitter.playerGetsFire(player, true);
}
}
if (typeof left === "undefined") {
left = EightBitter.unitsize * 16;
}
if (typeof bottom === "undefined") {
bottom = EightBitter.unitsize * 16;
}
EightBitter.addThing(
player, left, bottom - player.height * EightBitter.unitsize
);
EightBitter.ModAttacher.fireEvent("onAddPlayer", player);
return player;
}
/**
* Shortcut to call scrollThing on the player.
*
* @this {EightBittr}
* @param {Number} dx
* @param {Number} dy
*/
function scrollPlayer(dx, dy) {
var EightBitter = EightBittr.ensureCorrectCaller(this);
EightBitter.scrollThing(EightBitter.player, dx, dy);
EightBitter.ModAttacher.fireEvent("onScrollPlayer", dx, dy);
}
/**
* Triggered Function for when the game is paused. Music stops, the pause
* bleep is played, and the mod event is fired.
*/
function onGamePause(EightBitter) {
EightBitter.AudioPlayer.pauseAll();
EightBitter.AudioPlayer.play("Pause");
EightBitter.ModAttacher.fireEvent("onGamePause");
}
/**
* Triggered Function for when the game is played or unpause. Music resumes
* and the mod event is fired.
*/
function onGamePlay(EightBitter) {
EightBitter.AudioPlayer.resumeAll();
EightBitter.ModAttacher.fireEvent("onGamePlay");
}
/* Input
*/
/**
* Reacts to the left key being pressed. keys.run and leftDown are marked
* and the mod event is fired.
*
* @param {Player} player
*/
function keyDownLeft(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
return;
}
player.keys.run = -1;
player.keys.leftDown = true; // independent of changes to keys.run
player.EightBitter.ModAttacher.fireEvent("onKeyDownLeft");
}
/**
* Reacts to the right key being pressed. keys.run and keys.rightDown are
* marked and the mod event is fired.
*
* @param {Player} player
*/
function keyDownRight(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
return;
}
player.keys.run = 1;
player.keys.rightDown = true; // independent of changes to keys.run
player.EightBitter.ModAttacher.fireEvent("onKeyDownRight");
event.preventDefault();
}
/**
* Reacts to the up key being pressed. If the player can jump, it does, and
* underwater paddling is checked. The mod event is fired.
*
* @param {Player} player
*/
function keyDownUp(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
return;
}
player.keys.up = true;
if (player.canjump && (
player.resting || player.EightBitter.MapScreener.underwater)
) {
player.keys.jump = 1;
player.canjump = player.keys.jumplev = 0;
if (player.power > 1) {
player.EightBitter.AudioPlayer.play("Jump Super");
} else {
player.EightBitter.AudioPlayer.play("Jump Small");
}
if (player.EightBitter.MapScreener.underwater) {
player.EightBitter.TimeHandler.addEvent(function () {
player.jumping = player.keys.jump = false;
}, 14);
}
}
player.EightBitter.ModAttacher.fireEvent("onKeyDownUp");
event.preventDefault();
}
/**
* Reacts to the down key being pressed. keys.crouch is marked and the mod
* event is fired.
*
* @param {Player} player
*/
function keyDownDown(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
return;
}
player.keys.crouch = true;
player.EightBitter.ModAttacher.fireEvent("onKeyDownDown");
event.preventDefault();
}
/**
* Reacts to the sprint key being pressed. Firing happens if the player is
* able, keys.spring is marked, and the mod event is fired.
*
* @param {Player} player
*/
function keyDownSprint(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
return;
}
if (player.power === 3 && player.keys.sprint === 0 && !player.crouch) {
player.fire(player);
}
player.keys.sprint = 1;
player.EightBitter.ModAttacher.fireEvent("onKeyDownSprint");
event.preventDefault();
}
/**
* Reacts to the pause key being pressed. Pausing happens almost immediately
* (the delay helps prevent accidental pauses) and the mod event fires.
*
* @param {Player} player
*/
function keyDownPause(player, event) {
if (!player.EightBitter.GamesRunner.getPaused()) {
player.EightBitter.TimeHandler.addEvent(
player.EightBitter.GamesRunner.pause, 7, true
);
}
player.EightBitter.ModAttacher.fireEvent("onKeyDownPause");
event.preventDefault();
}
/**
* Reacts to the mute key being lifted. Muting is toggled and the mod event
* is fired.
*
* @param {Player} player
*/
function keyDownMute(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
return;
}
player.EightBitter.AudioPlayer.toggleMuted();
player.EightBitter.ModAttacher.fireEvent("onKeyDownMute");
event.preventDefault();
}
/**
* Reacts to the left key being lifted. keys.run and keys.leftDown are
* marked and the mod event is fired.
*
* @param {Player} player
*/
function keyUpLeft(player, event) {
player.keys.run = player.keys.leftDown = 0;
player.EightBitter.ModAttacher.fireEvent("onKeyUpLeft");
event.preventDefault();
}
/**
* Reacts to the right key being lifted. keys.run and keys.rightDown are
* marked and the mod event is fired.
*
* @param {Player} player
*/
function keyUpRight(player, event) {
player.keys.run = player.keys.rightDown = 0;
player.EightBitter.ModAttacher.fireEvent("onKeyUpRight");
event.preventDefault();
}
/**
* Reacts to the up key being lifted. Jumping stops and the mod event is
* fired.
*
* @param {Player} player
*/
function keyUpUp(player, event) {
if (!player.EightBitter.MapScreener.underwater) {
player.keys.jump = player.keys.up = 0;
}
player.canjump = true;
player.EightBitter.ModAttacher.fireEvent("onKeyUpUp");
event.preventDefault();
}
/**
* Reacts to the down key being lifted. keys.crouch is marked, crouch
* removal happens if necessary, and the mod event is fired.
*
* @param {Player} player
*/
function keyUpDown(player, event) {
player.keys.crouch = 0;
if (!player.piping) {
player.EightBitter.animatePlayerRemoveCrouch(player);
}
player.EightBitter.ModAttacher.fireEvent("onKeyUpDown");
event.preventDefault();
}
/**
* Reacts to the spring key being lifted. keys.sprint is marked and the mod
* event is fired.
*
* @param {Player} player
*/
function keyUpSprint(player, event) {
player.keys.sprint = 0;
player.EightBitter.ModAttacher.fireEvent("onKeyUpSprint");
event.preventDefault();
}
/**
* Reacts to the pause key being lifted. The game is unpaused if necessary
* and the mod event is fired.
*
* @param {Player} player
*/
function keyUpPause(player, event) {
if (player.EightBitter.GamesRunner.getPaused()) {
player.EightBitter.GamesRunner.play();
}
player.EightBitter.ModAttacher.fireEvent("onKeyUpPause");
event.preventDefault();
}
/**
* Reacts to a right click being pressed. Pausing is toggled and the mod
* event is fired.
*
* @param {Player} player
*/
function mouseDownRight(player, event) {
player.EightBitter.GamesRunner.togglePause();
player.EightBitter.ModAttacher.fireEvent("onMouseDownRight");
event.preventDefault();
}
/**
* Reacts to a regularly caused device motion event. Acceleration is checked
* for changed tilt horizontally (to trigger left or right key statuses) or
* changed tilt vertically (jumping). The mod event is also fired.
*
* @param {Player} player
* @param {DeviceMotionEvent} event
*/
var deviceMotion = (function () {
var motionDown = false,
motionLeft = false,
motionRight = false,
x, y,
dy;
return function deviceMotion(player, event) {
player.EightBitter.ModAttacher.fireEvent("onDeviceMotion", event);
var acceleration = event.accelerationIncludingGravity;
if (y !== undefined) {
dy = acceleration.y - y;
if (dy > 0.21) {
player.EightBitter.keyDownUp(player);
} else if (dy < -0.14) {
player.EightBitter.keyUpUp(player);
}
}
x = acceleration.x;
y = acceleration.y;
if (x > 2.1) {
if (!motionLeft) {
player.EightBitter.keyDownLeft(player);
motionLeft = true;
}
} else if (x < -2.1) {
if (!motionRight) {
player.EightBitter.keyDownRight(player);
motionRight = true;
}
} else {
if (motionLeft) {
player.EightBitter.keyUpLeft(player);
motionLeft = false;
}
if (motionRight) {
player.EightBitter.keyUpRight(player);
motionRight = false;
}
}
};
})();
/**
* Checks whether inputs can be fired, which is equivalent to the status of
* the MapScreener's nokeys variable (an inverse value).
*
* @param {EightBittr} EightBitter
*/
function canInputsTrigger(EightBitter) {
return !EightBitter.MapScreener.nokeys;
}
/* Upkeep maintenence
*/
/**
* Regular maintenance Function called on the Solids group every upkeep.
* Things are checked for being alive and to the right of QuadsKeeper.left;
* if they aren't, they are removed. Each Thing is also allowed a movement
* Function.
*
* @param {EightBittr} EightBitter
* @param {Solid[]} solids EightBittr's GroupHolder's Solid group.
*/
function maintainSolids(EightBitter, solids) {
var delx = EightBitter.QuadsKeeper.left,
solid, i;
EightBitter.QuadsKeeper.determineAllQuadrants("Solid", solids);
for (i = 0; i < solids.length; ++i) {
solid = solids[i];
if (solid.alive && solid.right > delx) {
if (solid.movement) {
solid.movement(solid);
}
} else {
EightBitter.arrayDeleteThing(solid, solids, i);
i -= 1;
}
}
}
/**
* Regular maintenance Function called on the Characters group every upkeep.
* Things have gravity and y-velocities, collision detection, and resting
* checks applied before they're checked for being alive. If they are, they
* are allowed a movement Function; if not, they are removed.
*
* @param {EightBittr} EightBitter
* @param {Character[]} characters EightBittr's GroupHolder's Characters
* group.
*/
function maintainCharacters(EightBitter, characters) {
var delx = EightBitter.QuadsKeeper.right,
character, i;
for (i = 0; i < characters.length; ++i) {
character = characters[i];
// Gravity
if (character.resting) {
character.yvel = 0;
} else {
if (!character.nofall) {
character.yvel += character.gravity || EightBitter.MapScreener.gravity;
}
character.yvel = Math.min(character.yvel, EightBitter.MapScreener.maxyvel);
}
// Position updating and collision detection
character.under = character.undermid = false;
EightBitter.updatePosition(character);
EightBitter.QuadsKeeper.determineThingQuadrants(character);
EightBitter.ThingHitter.checkHitsOf[character.title](character);
// Overlaps
if (character.overlaps && character.overlaps.length) {
EightBitter.maintainOverlaps(character);
}
// Resting tests
if (character.resting) {
if (!EightBitter.isCharacterOnResting(character, character.resting)) {
if (character.onRestingOff) {
character.onRestingOff(character, character.resting);
} else {
// Necessary for moving platforms
character.resting = undefined;
}
} else {
character.yvel = 0;
EightBitter.setBottom(character, character.resting.top);
}
}
// Movement or deletion
// To do: rethink this...
if (character.alive) {
if (!character.player &&
(character.numquads === 0 || character.left > delx) &&
(!character.outerok || (
character.right < EightBitter.MapScreener.width - delx
))) {
EightBitter.arrayDeleteThing(character, characters, i);
} else {
if (!character.nomove && character.movement) {
character.movement(character);
}
}
} else {
EightBitter.arrayDeleteThing(character, characters, i);
i -= 1;
}
}
}
/**
* Maintenance Function only triggered for Things that are known to have
* overlapping Solids stored in their overlaps attribute. This will slide
* the offending Thing away from the midpoint of those overlaps once a call
* until it's past the boundary (and check for those boundaries if not
* already set).
*
* @param {Thing} thing
*/
function maintainOverlaps(thing) {
// If checkOverlaps is still true, this is the first maintain call
if (thing.checkOverlaps) {
if (!thing.EightBitter.setOverlapBoundaries(thing)) {
return;
}
}
thing.EightBitter.slideToX(
thing,
thing.overlapGoal,
thing.EightBitter.unitsize
);
// Goal to the right: has the thing gone far enough to the right?
if (thing.overlapGoRight) {
if (thing.left >= thing.overlapCheck) {
thing.EightBitter.setLeft(thing, thing.overlapCheck);
} else {
return;
}
}
// Goal to the left: has the thing gone far enough to the left?
else {
if (thing.right <= thing.overlapCheck) {
thing.EightBitter.setRight(thing, thing.overlapCheck);
} else {
return;
}
}
// A check above didn't fail into a return, so overlapping is solved
thing.overlaps.length = 0;
thing.checkOverlaps = true;
}
/**
* Sets the overlapping properties of a Thing when it is first detected as
* overlapping in maintainOverlaps. All solids in its overlaps Array are
* checked to find the leftmost and rightmost extremes and midpoint.
* Then, the Thing is checked for being to the left or right of the
* midpoint, and the goal set to move it away from the midpoint.
*
* @param {Thing} thing
* @return {Boolean} Whether the Thing's overlaps were successfully
* recorded (if there was only one, not so).
*/
function setOverlapBoundaries(thing) {
// Only having one overlap means nothing should be done
if (thing.overlaps.length === 1) {
thing.overlaps.length = 0;
return false;
}
var rightX = -Infinity,
leftX = Infinity,
overlaps = thing.overlaps,
other, leftThing, rightThing,
midpoint, i;
for (i = 0; i < overlaps.length; i += 1) {
other = overlaps[i];
if (other.right > rightX) {
rightThing = other;
}
if (other.left < leftX) {
leftThing = other;
}
}
midpoint = (leftX + rightX) / 2;
if (thing.EightBitter.getMidX(thing) >= midpoint) {
thing.overlapGoal = Infinity;
thing.overlapGoRight = true;
thing.overlapCheck = rightThing.right;
} else {
thing.overlapGoal = -Infinity;
thing.overlapGoRight = false;
thing.overlapCheck = leftThing.left;
}
thing.checkOverlaps = false;
return true;
}
/**
* Regular maintenance Function called on the player every upkeep. A barrage
* of tests are applied, namely falling/jumping, dying, x- and y-velocities,
* running, and scrolling. This is separate from the movePlayer movement
* Function that will be called in maintainCharacters.
*
* @param {EightBittr} EightBitter
*/
function maintainPlayer(EightBitter) {
var player = EightBitter.player;
if (!EightBitter.isThingAlive(player)) {
return;
}
// Player is falling
if (player.yvel > 0) {
if (!EightBitter.MapScreener.underwater) {
player.keys.jump = 0;