This repository was archived by the owner on Jan 17, 2021. It is now read-only.
forked from Fulox/FullScreenMario-JSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEightBittr.js
More file actions
791 lines (706 loc) · 26.1 KB
/
EightBittr.js
File metadata and controls
791 lines (706 loc) · 26.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/**
* EightBittr.js
*
* An abstract class used exclusively as the parent of GameStartr. EightBittr
* contains useful functions for manipulating Things that are independent of
* the required GameStartr modules.
*
* @author "Josh Goldberg" <josh@fullscreenmario.com>
*/
var EightBittr = (function () {
"use strict";
/**
* EightBittr constructor. Settings arguments are used to initialize
* "constant" values and check for requirements.
*
* @constructor
* @param {Object} [settings] An Object containing all other arguments.
* @param {String[]} [constants] The names of attributes that should exist
* in both the EightBittr constructor and
* child instances.
* @param {Object} [requirements] A mapping of required settings that must
* exist globally and/or locally.
*/
function EightBittr(settings) {
var EightBitter = EightBittr.ensureCorrectCaller(this),
constants, requirements, i;
settings = settings || {};
EightBitter.constructor = settings.constructor || EightBittr,
// Constants, such as unitsize and scale, are always copied first
constants = settings.constants;
if (constants) {
for (i = 0; i < constants.length; i += 1) {
EightBitter[constants[i]] = EightBitter.constructor[constants[i]];
}
}
requirements = settings.requirements;
if (requirements) {
if (requirements.global) {
checkRequirements(window, requirements.global, "global");
}
if (requirements.self) {
checkRequirements(EightBitter, requirements.self, "self");
}
}
}
/**
* Given an associate array of requirement names to the files that should
* include them, this makes sure each of those requirements is a property of
* the given Object.
*
* @param {Mixed} self Generally either the window (for global checks,
* such as utility classes) or an EightBitter.
* @param {Object} requirements An associative array of properties to
* check for under self.
* @param {String} name The name referring to self, printed out in an
* Error if needed.
*/
function checkRequirements(self, requirements, name) {
var fails = [],
requirement;
// For each requirement in the given object, if it isn't visible as a
// member of self (evaluates to falsy), complain
for (requirement in requirements) {
if (requirements.hasOwnProperty(requirement) && !self[requirement]) {
fails.push(requirement);
}
}
// If there was at least one failure added to the fails array, throw
// an error with each fail split by endlines
if (fails.length) {
throw new Error("Missing " + fails.length + " requirement(s) "
+ "in " + name + ".\n"
+ fails.map(function (requirement, i) {
return i + ". " + requirement + ": is the '"
+ requirements[requirement] + "' file included?";
}).join("\n"));
}
}
/**
* Resets the EightBittr by calling all of the named reset member Functions
* on itself.
*
* @param {EightBittr} EightBitter
* @param {String[]} resets The ordered Array of reset Functions to be
* called.
* @param {Object} [customs] Additional arguments to pass to all reset
* Functions.
*/
function reset(EightBitter, resets, customs) {
var i;
for (i = 0; i < resets.length; i += 1) {
EightBitter[resets[i]](EightBitter, customs)
}
EightBitter.customs = customs;
}
/**
* Resets the EightBittr in a timed manner by calling all the named reset
* member Functions on itself and adding the time (in milliseconds) along
* along with the total process time to an Array, which is then returned.
*
* @param {EightBittr} EightBitter
* @param {String[]} resets The ordered Array of reset Functions to be
* called.
* @param {Object} [customs] Additional arguments to pass to all reset
* Functions.
* @return {String[]}
*/
function resetTimed(EightBitter, resets, customs) {
var timeStart = performance.now(),
times = [],
timeEach, i;
for (i = 0; i < resets.length; i += 1) {
timeEach = performance.now();
EightBitter[resets[i]](EightBitter, customs);
times.push({
"name": resets[i],
"time": performance.now() - timeEach
});
}
times.push({
"name": "resetTimed",
"time": performance.now() - timeStart
});
return times;
}
/**
* EightBittr.get is provided as a shortcut function to make binding member
* functions, particularily those using "this.unitsize" (where this needs to
* be an EightBitter, not an external calling object). At the very simplest,
* this.get(func) acts as a shortcut to this.bind(this, func).
* In addition, if the name is given as "a.b", EightBitter.followPath will
* be used on "a.b".split('.') (so EightBitter.prototype[a][b] is returned).
*
* @this {EightBittr}
* @param {Mixed} name Either the function itself, or a string of the path
* to the function (after ".prototype.").
* @return {Function} A function, bound to set "this" to the calling
* EightBitter
*/
EightBittr.prototype.get = function(name) {
var EightBitter = EightBittr.prototype.ensureCorrectCaller.call(this),
func;
// If name is a string, turn it into a function path, and follow it
if (name.constructor === String) {
func = followPathHard(EightBitter, name.split('.'), 0);
}
// If it's already a path (array), follow it
else if (name instanceof Array) {
func = followPathHard(EightBitter, name, 0);
}
// Otherwise func is just name
else {
func = name;
}
// Don't allow func to be undefined or some non-function object
if (typeof(func) !== "function") {
throw new Error(name + " is not defined in this EightBitter", self);
}
// Bind the function to this
return func.bind(EightBitter);
};
/* HTML functions
*/
/**
* Creates and returns a new HTML <canvas> element, with an optional scaling
* multiplier. Image smoothing is disabled.
*
* @param {Number} width How wide the canvas should be.
* @param {Number} height How tall the canvas should be.
* @param {Number} [scaling] How much to scale the style of the canvas (by
* default, 1 for not at all).
* @return {HTMLCanvasElement}
*/
function createCanvas(width, height, scaling) {
var canvas = document.createElement("canvas"),
context = canvas.getContext("2d");
canvas.width = width;
canvas.height = height;
scaling = scaling || 1;
// Scaling 1 by default, but may be different (e.g. unitsize)
canvas.style.width = (width * scaling) + "px";
canvas.style.height = (height * scaling) + "px";
// For speed's sake, disable image smoothing in all browsers
context.imageSmoothingEnabled = false;
context.webkitImageSmoothingEnabled = false;
context.mozImageSmoothingEnabled = false;
context.msImageSmoothingEnabled = false;
context.oImageSmoothingEnabled = false;
return canvas;
}
/* Physics functions
*/
/**
* Shifts a Thing vertically by changing its top and bottom attributes.
*
* @param {Thing} thing
* @param {Number} dy
*/
function shiftVert(thing, dy) {
thing.top += dy;
thing.bottom += dy;
}
/**
* Shifts a Thing horizontally by changing its top and bottom attributes.
*
* @param {Thing} thing
* @param {Number} dy
*/
function shiftHoriz(thing, dx) {
thing.left += dx;
thing.right += dx;
}
/**
* Sets the top of a Thing to a set number, changing the bottom based on its
* height and the EightBittr's unisize.
*
* @param {Thing} thing
* @param {Number} top
*/
function setTop(thing, top) {
thing.top = top;
thing.bottom = thing.top + thing.height * thing.EightBitter.unitsize;
}
/**
* Sets the right of a Thing to a set number, changing the left based on its
* width and the EightBittr's unisize.
*
* @param {Thing} thing
* @param {Number} right
*/
function setRight(thing, right) {
thing.right = right;
thing.left = thing.right - thing.width * thing.EightBitter.unitsize;
}
/**
* Sets the bottom of a Thing to a set number, changing the top based on its
* height and the EightBittr's unisize.
*
* @param {Thing} thing
* @param {Number} bottom
*/
function setBottom(thing, bottom) {
thing.bottom = bottom;
thing.top = thing.bottom - thing.height * thing.EightBitter.unitsize;
}
/**
* Sets the left of a Thing to a set number, changing the right based on its
* width and the EightBittr's unisize.
*
* @param {Thing} thing
* @param {Number} left
*/
function setLeft(thing, left) {
thing.left = left;
thing.right = thing.left + thing.width * thing.EightBitter.unitsize;
}
/**
* Shifts a Thing so that it is centered on the given x and y.
*
* @param {Thing} thing
* @param {Number} x
* @param {Number} y
*/
function setMid(thing, x, y) {
thing.EightBitter.setMidX(thing, x);
thing.EightBitter.setMidY(thing, y);
}
/**
* Shifts a Thing so that it is horizontally centered on the given x.
*
* @param {Thing} thing
* @param {Number} x
*/
function setMidX(thing, x) {
thing.EightBitter.setLeft(
thing,
x + thing.width * thing.EightBitter.unitsize / 2
);
}
/**
* Shifts a Thing so that it is vertically centered on the given y.
*
* @param {Thing} thing
* @param {Number} y
*/
function setMidY(thing, y) {
thing.EightBitter.setTop(
thing,
y + thing.height * thing.EightBitter.unitsize / 2
);
}
/**
* @param {Thing} thing
* @return {Number} The horizontal midpoint of the Thing.
*/
function getMidX(thing) {
return thing.left + thing.width * thing.EightBitter.unitsize / 2;
}
/**
* @param {Thing} thing
* @return {Number} The vertical midpoint of the Thing.
*/
function getMidY(thing) {
return thing.top + thing.height * thing.EightBitter.unitsize / 2;
}
/**
* Shifts a Thing so that its midpoint is centered on the midpoint of the
* other Thing.
*
* @param {Thing} thing The Thing to be shifted.
* @param {Thing} other The Thing whose midpoint is referenced.
*/
function setMidObj(thing, other) {
thing.EightBitter.setMidXObj(thing, other);
thing.EightBitter.setMidYObj(thing, other);
}
/**
* Shifts a Thing so that its horizontal midpoint is centered on the
* midpoint of the other Thing.
*
* @param {Thing} thing The Thing to be shifted.
* @param {Thing} other The Thing whose midpoint is referenced.
*/
function setMidXObj(thing, other) {
thing.EightBitter.setLeft(
thing,
thing.EightBitter.getMidX(other)
- (thing.width * thing.EightBitter.unitsize / 2)
);
}
/**
* Shifts a Thing so that its vertical midpoint is centered on the
* midpoint of the other Thing.
*
* @param {Thing} thing The Thing to be shifted.
* @param {Thing} other The Thing whose midpoint is referenced.
*/
function setMidYObj(thing, other) {
thing.EightBitter.setTop(
thing,
thing.EightBitter.getMidY(other)
- (thing.height * thing.EightBitter.unitsize / 2)
);
}
/**
* @param {Thing} thing
* @param {Thing} other
* @return {Boolean} Whether the first Thing's midpoing is to the left of
* the other's.
*/
function objectToLeft(thing, other) {
return (
thing.EightBitter.getMidX(thing) < thing.EightBitter.getMidX(other)
);
}
/**
* Shifts a Thing's top up, then sets the bottom (similar to a shiftVert and
* a setTop combined).
*
* @param {Thing} thing
* @param {Number} dy
*/
function updateTop(thing, dy) {
// If a dy is provided, move the thing's top that much
thing.top += dy || 0;
// Make the thing's bottom dependent on the top
thing.bottom = thing.top + thing.height * thing.EightBitter.unitsize;
}
/**
* Shifts a Thing's right, then sets the left (similar to a shiftHoriz and a
* setRight combined).
*
* @param {Thing} thing
* @param {Number} dx
*/
function updateRight(thing, dx) {
// If a dx is provided, move the thing's right that much
thing.right += dx || 0;
// Make the thing's left dependent on the right
thing.left = thing.right - thing.width * thing.EightBitter.unitsize;
}
/**
* Shifts a Thing's bottom down, then sets the bottom (similar to a
* shiftVert and a setBottom combined).
*
* @param {Thing} thing
* @param {Number} dy
*/
function updateBottom(thing, dy) {
// If a dy is provided, move the thing's bottom that much
thing.bottom += dy || 0;
// Make the thing's top dependent on the top
thing.top = thing.bottom - thing.height * thing.EightBitter.unitsize;
}
/**
* Shifts a Thing's left, then sets the right (similar to a shiftHoriz and a
* setLeft combined).
*
* @param {Thing} thing
* @param {Number} dy
*/
function updateLeft(thing, dx) {
// If a dx is provided, move the thing's left that much
thing.left += dx || 0;
// Make the thing's right dependent on the left
thing.right = thing.left + thing.width * thing.EightBitter.unitsize;
}
/**
* Shifts a Thing toward a target x, but limits the total distance allowed.
* Distance is computed as from the Thing's horizontal midpoint.
*
* @param {Thing} thing
* @param {Number} x
* @param {Number} maxSpeed
*/
function slideToX(thing, x, maxSpeed) {
var midx = thing.EightBitter.getMidX(thing);
// If no maxSpeed is provided, assume Infinity (so it doesn't matter)
maxSpeed = maxSpeed || Infinity;
// Thing to the left? Slide to the right.
if (midx < x) {
thing.EightBitter.shiftHoriz(thing, Math.min(maxSpeed, (x - midx)));
}
// Thing to the right? Slide to the left.
else {
thing.EightBitter.shiftHoriz(thing, Math.max(-maxSpeed, (x - midx)));
}
}
/**
* Shifts a Thing toward a target y, but limits the total distance allowed.
* Distance is computed as from the Thing's vertical midpoint.
*
* @param {Thing} thing
* @param {Number} y
* @param {Number} maxSpeed
*/
function slideToY(thing, y, maxSpeed) {
var midy = thing.EightBitter.getMidY(thing);
// If no maxSpeed is provided, assume Infinity (so it doesn't matter)
maxSpeed = maxSpeed || Infinity;
// Thing above? slide down.
if (midy < y) {
thing.EightBitter.shiftVert(thing, Math.min(maxSpeed, (y - midy)));
}
// Thing below? Slide up.
else {
thing.EightBitter.shiftVert(thing, Math.max(-maxSpeed, (y - midy)));
}
}
/* EightBittr utilities
*/
/**
* Ensures the current object is an EightBittr by throwing an error if it
* is not. This should be used for functions in any EightBittr descendants
* that have to call 'this' to ensure their caller is what the programmer
* expected it to be.
*
* @param {Mixed} current
*/
function ensureCorrectCaller(current) {
if (!current instanceof EightBittr) {
throw new Error("A function requires the caller ('this') to be the "
+ "manipulated EightBittr object. Unfortunately, 'this' is a "
+ typeof(this) + ".");
}
return current;
}
/* General utilities
*/
/**
* "Proliferates" all properties of a donor onto a recipient by copying each
* of them and recursing onto child Objects. This is a deep copy.
*
* @param {Mixed} recipient
* @param {Mixed} donor
* @param {Boolean} [noOverride] Whether pre-existing properties of the
* recipient should be skipped (defaults to
* false).
*/
function proliferate(recipient, donor, noOverride) {
var setting, i;
// For each attribute of the donor:
for (i in donor) {
if (donor.hasOwnProperty(i)) {
// If noOverride, don't override already existing properties
if (noOverride && recipient.hasOwnProperty(i)) {
continue;
}
// If it's an object, recurse on a new version of it
setting = donor[i];
if (typeof setting === "object") {
if (!recipient.hasOwnProperty(i)) {
recipient[i] = new setting.constructor();
}
proliferate(recipient[i], setting, noOverride);
}
// Regular primitives are easy to copy otherwise
else {
recipient[i] = setting;
}
}
}
return recipient;
}
/**
* Identical to proliferate, but instead of checking whether the recipient
* hasOwnProperty on properties, it just checks if they're truthy
*
* @param {Mixed} recipient
* @param {Mixed} donor
* @param {Boolean} [noOverride] Whether pre-existing properties of the
* recipient should be skipped (defaults to
* false).
* @remarks This may not be good with JSLint, but it works for prototypal
* inheritance, since hasOwnProperty only is for the current class
*/
function proliferateHard(recipient, donor, noOverride) {
var setting, i;
// For each attribute of the donor:
for (i in donor) {
if (donor.hasOwnProperty(i)) {
// If noOverride, don't override already existing properties
if (noOverride && recipient[i]) {
continue;
}
// If it's an object, recurse on a new version of it
setting = donor[i];
if (typeof setting === "object") {
if (!recipient[i]) {
recipient[i] = new setting.constructor();
}
proliferate(recipient[i], setting, noOverride);
}
// Regular primitives are easy to copy otherwise
else {
recipient[i] = setting;
}
}
}
return recipient;
}
/**
* Identical to proliferate, but tailored for HTML elements because many
* element attributes don't play nicely with JavaScript Array standards.
* Looking at you, HTMLCollection!
*
* @param {Element} recipient
* @param {Any} donor
* @param {Boolean} [noOverride]
* @return {Element}
*/
function proliferateElement(recipient, donor, noOverride) {
var setting, i, j;
// For each attribute of the donor:
for (i in donor) {
if (donor.hasOwnProperty(i)) {
// If noOverride, don't override already existing properties
if (noOverride && recipient.hasOwnProperty(i)) {
continue;
}
setting = donor[i];
// Special cases for HTML elements
switch (i) {
// Children: just append all of them directly
case "children":
if (typeof(setting) !== "undefined") {
for (var j = 0; j < setting.length; j += 1) {
recipient.appendChild(setting[j]);
}
}
break;
// By default, use the normal proliferate logic
default:
// If it's an object, recurse on a new version of it
if (typeof setting === "object") {
if (!recipient.hasOwnProperty(i)) {
recipient[i] = new setting.constructor();
}
proliferate(recipient[i], setting, noOverride);
}
// Regular primitives are easy to copy otherwise
else {
recipient[i] = setting;
}
break;
}
}
}
return recipient;
}
/**
* Creates and returns an HTMLElement of the specified type. Any additional
* settings Objects may be given to be proliferated onto the Element via
* proliferateElement.
*
* @param {String} type The tag of the Element to be created.
* @param {Object} [settings] Additional settings for the Element, such as
* className or style.
* @return {HTMLElement}
*/
function createElement(type) {
var element = document.createElement(type || "div"),
i;
// For each provided object, add those settings to the element
for (i = 1; i < arguments.length; i += 1) {
proliferateElement(element, arguments[i]);
}
return element;
}
/**
* Follows a path inside an Object recursively, based on a given path.
*
* @param {Mixed} object
* @param {String[]} path The ordered names of attributes to descend into.
* @param {Number} [num] The starting index in path (by default, 0).
* @return {Mixed}
* @remarks This may not be good with JSLint, but it works for prototypal
* inheritance, since hasOwnProperty only is for the current class
*/
function followPathHard(object, path, num) {
for (var i = num || 0; i < path.length; i += 1) {
if (typeof object[path[i]] === "undefined") {
return undefined;
}
else {
object = object[path[i]];
}
}
return object;
}
/**
* Switches a Thing from one Array to Another using splice and push.
*
* @param {Thing} thing
* @param {Array} arrayOld
* @param {Array} arrayNew
*/
function arraySwitch(thing, arrayOld, arrayNew) {
arrayOld.splice(arrayOld.indexOf(thing), 1);
arrayNew.push(thing);
}
/**
* Sets a Thing's position within an Array to the front by splicing and then
* unshifting it.
*
* @param {Thing} thing
* @param {Array} array
*/
function arrayToBeginning(thing, array) {
array.splice(array.indexOf(thing), 1);
array.unshift(thing);
}
/**
* Sets a Thing's position within an Array to the front by splicing and then
* pushing it.
*
* @param {Thing} thing
* @param {Array} array
*/
function arrayToEnd(thing, array) {
array.splice(array.indexOf(thing), 1);
array.push(thing);
}
proliferateHard(EightBittr.prototype, {
// Setup
"reset": reset,
"resetTimed": resetTimed,
// HTML
"createCanvas": createCanvas,
// Physics
"shiftVert": shiftVert,
"shiftHoriz": shiftHoriz,
"setTop": setTop,
"setRight": setRight,
"setBottom": setBottom,
"setLeft": setLeft,
"setLeftOld": setLeft,
"setMid": setMid,
"setMidY": setMidY,
"setMidX": setMidX,
"getMidY": getMidY,
"getMidX": getMidX,
"setMidObj": setMidObj,
"setMidXObj": setMidXObj,
"setMidYObj": setMidYObj,
"objectToLeft": objectToLeft,
"updateTop": updateTop,
"updateRight": updateRight,
"updateBottom": updateBottom,
"updateLeft": updateLeft,
"slideToY": slideToY,
"slideToX": slideToX,
// EightBittr utilities
"ensureCorrectCaller": ensureCorrectCaller,
// General utilities
"proliferate": proliferate,
"proliferateHard": proliferateHard,
"proliferateElement": proliferateElement,
"createElement": createElement,
"arraySwitch": arraySwitch,
"arrayToBeginning": arrayToBeginning,
"arrayToEnd": arrayToEnd
});
EightBittr.ensureCorrectCaller = ensureCorrectCaller;
return EightBittr;
})();