forked from Fulox/FullScreenMario-JSON
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGameStartr.js
1520 lines (1370 loc) · 51.1 KB
/
GameStartr.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
/**
* GameStartr.js
*
* An abstract class used as a base for creating sprite-based 2D games. Utility
* functions and an expansive skeleton are available for a child class to make
* us of, particular with physics manipulations, graphics rendering, and reset
* functions based around the 19 required modules.
*
* Descendent classes of GameStartr must put their settings in their prototype
* under settings. Reset Functions below list their required file names.
*
* The following member attributes of child classes are required:
* gameStart {Function}
* onGamePlay {Function}
* onGamePause {Function}
* canInputsTrigger {Function}
* setMap {Function}
* setLocation {Function}
*/
var GameStartr = (function (EightBittr) {
"use strict";
// Use an EightBittr as the class parent, with EightBittr's constructor
var EightBitterProto = new EightBittr(),
// Used for combining arrays from the prototype to this
proliferate = EightBitterProto.proliferate,
proliferateHard = EightBitterProto.proliferateHard;
/**
*
*/
function GameStartr(customs) {
if (typeof (customs) === "undefined") {
customs = {};
}
EightBittr.call(this, {
"constants": customs.constants,
"constructor": customs.constructor || GameStartr,
"customs": customs,
"requirements": {
"global": {
"AudioPlayr": "src/AudioPlayr/AudioPlayr.js",
"ChangeLinr": "src/ChangeLinr/ChangeLinr.js",
"FPSAnalyzr": "src/FPSAnalyzr/FPSAnalyzr.js",
"GamesRunnr": "src/GamesRunnr/GamesRunnr.js",
"GroupHoldr": "src/GroupHoldr/GroupHoldr.js",
"InputWritr": "src/InputWritr/InputWritr.js",
"LevelEditr": "src/LevelEditr/LevelEditr.js",
"NumberMakr": "src/NumberMakr/NumberMakr.js",
"MapScreenr": "src/MapScreenr/MapScreenr.js",
"MapsHandlr": "src/MapsHandlr/MapsHandlr.js",
"ModAttachr": "src/ModAttachr/ModAttachr.js",
"ObjectMakr": "src/ObjectMakr/ObjectMakr.js",
"PixelDrawr": "src/PixelDrawr/PixelDrawr.js",
"PixelRendr": "src/PixelRendr/PixelRendr.js",
"QuadsKeepr": "src/QuadsKeepr/QuadsKeepr.js",
"StatsHoldr": "src/StatsHoldr/StatsHoldr.js",
"StringFilr": "src/StringFilr/StringFilr.js",
"ThingHittr": "src/ThingHittr/ThingHittr.js",
"TimeHandlr": "src/TimeHandlr/TimeHandlr.js"
}
}
});
}
GameStartr.prototype = EightBitterProto;
// Subsequent settings will be stored in GameStartr.prototype.settings
EightBitterProto.settings = {};
EightBitterProto.resets = [
"resetObjectMaker",
"resetPixelRender",
"resetTimeHandler",
"resetAudioPlayer",
"resetQuadsKeeper",
"resetGamesRunner",
"resetStatsHolder",
"resetGroupHolder",
"resetThingHitter",
"resetMapScreener",
"resetPixelDrawer",
"resetNumberMaker",
"resetMapsCreator",
"resetMapsHandler",
"resetInputWriter",
"resetLevelEditor",
"resetWorldSeeder",
"resetModAttacher",
"startModAttacher",
"resetContainer"
];
/* Resets
*/
/**
* Resets the EightBittr by calling the parent EightBittr.prototype.reset.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
*/
function reset(EightBitter, customs) {
EightBittr.prototype.reset(EightBitter, EightBitter.resets, customs);
};
/**
* Resets the EightBittr and records the time by calling the parent
* EightBittr.prototype.resetTimed.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @return {Array} How long each reset Function took followed by the entire
* operation, in milliseconds.
*/
function resetTimed(EightBitter, customs) {
return EightBittr.prototype.resetTimed(
EightBitter, EightBitter.resets, customs
);
};
/**
* Sets self.ObjectMaker.
*
* Because many Thing functions require access to other FSM modules, each is
* given a reference to this container FSM via properties.Thing.EightBitter.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): ObjectMakr (src/ObjectMakr/ObjectMakr.js)
* objects.js (settings/objects.js)
*/
function resetObjectMaker(EightBitter, customs) {
EightBitter.ObjectMaker = new ObjectMakr(proliferate({
"properties": {
"Quadrant": {
"EightBitter": EightBitter
},
"Thing": {
"EightBitter": EightBitter
}
}
}, EightBitter.settings.objects));
}
/**
* Sets self.QuadsKeeper.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): QuadsKeepr (src/QuadsKeepr/QuadsKeepr.js)
* quadrants.js (settings/quadrants.js)
*/
function resetQuadsKeeper(EightBitter, customs) {
var quadrantWidth = customs.width / (EightBitter.settings.quadrants.numCols - 3),
quadrantHeight = customs.height / (EightBitter.settings.quadrants.numRows - 2);
EightBitter.QuadsKeeper = new QuadsKeepr(proliferate({
"ObjectMaker": EightBitter.ObjectMaker,
"createCanvas": EightBitter.createCanvas,
"quadrantWidth": quadrantWidth,
"quadrantHeight": quadrantHeight,
"startLeft": -quadrantWidth,
"startHeight": -quadrantHeight,
"onAdd": EightBitter.onAreaSpawn.bind(EightBitter, EightBitter),
"onRemove": EightBitter.onAreaUnspawn.bind(EightBitter, EightBitter),
}, EightBitter.settings.quadrants));
}
/**
* Sets self.PixelRender.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): PixelRendr (src/PixelRendr/PixelRendr.js)
* sprites.js (settings/sprites.js)
*/
function resetPixelRender(EightBitter, customs) {
EightBitter.PixelRender = new PixelRendr(proliferate({
"QuadsKeeper": EightBitter.QuadsKeeper,
"unitsize": EightBitter.unitsize,
"scale": EightBitter.scale
}, EightBitter.settings.sprites));
}
/**
* Sets self.PixelDrawer.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): PixelDrawr (src/PixelDrawr/PixelDrawr.js)
* renderer.js (settings/renderer.js)
*/
function resetPixelDrawer(EightBitter, customs) {
EightBitter.PixelDrawer = new PixelDrawr(proliferate({
"PixelRender": EightBitter.PixelRender,
"MapScreener": EightBitter.MapScreener,
"createCanvas": EightBitter.createCanvas,
"unitsize": EightBitter.unitsize,
"innerWidth": customs.width,
"generateObjectKey": EightBitter.generateObjectKey
}, EightBitter.settings.renderer));
}
/**
* Sets EightBitter.TimeHandler.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): TimeHandlr (src/TimeHandlr/TimeHandlr.js)
* events.js (settings/events.js)
*/
function resetTimeHandler(EightBitter, customs) {
EightBitter.TimeHandler = new TimeHandlr(proliferate({
"classAdd": EightBitter.addClass,
"classRemove": EightBitter.removeClass
}, EightBitter.settings.events));
}
/**
* 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) {
EightBitter.AudioPlayer = new AudioPlayr(proliferate({
"statistics": {
"proliferate": EightBitter.proliferate
}
}, EightBitter.settings.audio));
}
/**
* Sets self.GamesRunner.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): GamesRunnr (src/GamesRunnr/GamesRunnr.js)
* runner.js (settings/runner.js)
*/
function resetGamesRunner(EightBitter, customs) {
EightBitter.GamesRunner = new GamesRunnr(proliferate({
"scope": EightBitter,
"onPlay": EightBitter.onGamePlay.bind(EightBitter, EightBitter),
"onPause": EightBitter.onGamePause.bind(EightBitter, EightBitter)
}, EightBitter.settings.runner));
}
/**
* Sets self.StatsHolder.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): StatsHoldr (src/StatsHoldr/StatsHoldr.js)
* statistics.js (settings/statistics.js)
*/
function resetStatsHolder(EightBitter, customs) {
EightBitter.StatsHolder = new StatsHoldr(proliferate({
"callbackArgs": [EightBitter],
"proliferate": EightBitter.proliferate,
"createElement": EightBitter.createElement
}, EightBitter.settings.statistics));
}
/**
* Sets self.GroupHolder.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): GroupHoldr (src/ThingHittr/GroupHoldr.js)
* groups.js (settings/groups.js)
*/
function resetGroupHolder(EightBitter, customs) {
EightBitter.GroupHolder = new GroupHoldr(EightBitter.settings.groups);
}
/**
* 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) {
EightBitter.ThingHitter = new ThingHittr(proliferate({
"scope": EightBitter
}, EightBitter.settings.collisions));
}
/**
* Sets self.MapScreener.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): MapScreenr (src/MapScreenr/MapScreenr.js)
* maps.js (settings/maps.js)
*/
function resetMapScreener(EightBitter, customs) {
EightBitter.MapScreener = new MapScreenr({
"EightBitter": EightBitter,
"unitsize": EightBitter.unitsize,
"width": customs.width,
"height": customs.height,
"variableArgs": [EightBitter],
"variables": EightBitter.settings.maps.screenVariables
});
}
/**
* Sets self.NumberMaker.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): NumberMaker (src/NumberMakr/NumberMakr.js)
*/
function resetNumberMaker(EightBitter, customs) {
EightBitter.NumberMaker = new NumberMakr();
}
/**
* Sets self.MapCreator.
*
* @param {EightBittr} EightBitter
* @remarks Requirement(s): MapCreatr (src/MapCreatr/MapCreatr.js)
* maps.js (settings/maps.js)
*/
function resetMapsCreator(EightBitter, customs) {
EightBitter.MapsCreator = new MapsCreatr({
"ObjectMaker": EightBitter.ObjectMaker,
"groupTypes": EightBitter.settings.maps.groupTypes,
"macros": EightBitter.settings.maps.macros,
"entrances": EightBitter.settings.maps.entrances,
"maps": EightBitter.settings.maps.library,
"scope": EightBitter
});
}
/**
* 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
});
}
/**
* Sets self.InputWriter.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): InputWritr (src/InputWritr/InputWritr.js)
* input.js (settings/input.js)
*/
function resetInputWriter(EightBitter, customs) {
EightBitter.InputWriter = new InputWritr(proliferate({
"canTrigger": EightBitter.canInputsTrigger.bind(EightBitter, EightBitter)
}, EightBitter.settings.input.InputWritrArgs));
}
/**
* Sets self.LevelEditor.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): LevelEditr (src/LevelEditr/LevelEditr.js)
* editor.js (settings/editor.js)
*/
function resetLevelEditor(EightBitter, customs) {
EightBitter.LevelEditor = new LevelEditr(proliferate({
"GameStarter": EightBitter,
"beautifier": js_beautify // Eventually there will be a custom beautifier... maybe
}, EightBitter.settings.editor));
}
/**
* Sets self.WorldSeeder.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): WorldSeedr (src/WorldSeedr/WorldSeedr.js)
* generator.js (settings/generator.js)
*/
function resetWorldSeeder(EightBitter, customs) {
EightBitter.WorldSeeder = new WorldSeedr(proliferate({
"random": EightBitter.NumberMaker.random,
"onPlacement": EightBitter.mapPlaceRandomCommands.bind(EightBitter, EightBitter)
}, EightBitter.settings.generator));
}
/**
* Sets self.ModAttacher.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
* @remarks Requirement(s): ModAttachr (src/ModAttachr/ModAttachr.js)
* mods.js (settings/mods.js)
*/
function resetModAttacher(EightBitter, customs) {
EightBitter.ModAttacher = new ModAttachr(proliferate({
"scopeDefault": EightBitter,
"StatsHoldr": StatsHoldr,
"proliferate": EightBitter.proliferate,
"createElement": EightBitter.createElement
}, EightBitter.settings.mods));
}
/**
* Starts self.ModAttacher. All mods are enabled, and the "onReady" trigger
* is fired.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
*/
function startModAttacher(EightBitter, customs) {
var mods = customs.mods,
i;
if (mods) {
for (i in mods) {
if (mods[i]) {
EightBitter.ModAttacher.enableMod(i);
}
}
}
EightBitter.ModAttacher.fireEvent("onReady", EightBitter, EightBitter);
}
/**
* Resets the parent HTML container. Width and height are set by customs,
* and canvas and StatsHolder container elements are added.
*
* @param {EightBittr} EightBitter
* @param {Object} [customs]
*/
function resetContainer(EightBitter, customs) {
EightBitter.container = EightBitter.createElement("div", {
"className": "EightBitter",
"style": EightBitter.proliferate({
"position": "relative",
"width": customs.width + "px",
"height": customs.height + "px"
}, customs.style)
});
EightBitter.canvas = EightBitter.createCanvas(customs.width, customs.height);
EightBitter.PixelDrawer.setCanvas(EightBitter.canvas);
EightBitter.container.appendChild(EightBitter.canvas);
}
/* Global manipulations
*/
/**
* Scrolls the game window by shifting all Things and checking for quadrant
* refreshes.
*
* @this {EightBittr}
* @param {Number} dx How far to scroll horizontally.
* @param {Number} [dy] How far to scroll vertically.
*/
function scrollWindow(dx, dy) {
var EightBitter = EightBittr.ensureCorrectCaller(this);
dx = dx | 0;
dy = dy | 0;
if (!dx && !dy) {
return;
}
EightBitter.MapScreener.shift(dx, dy);
EightBitter.shiftAll(-dx, -dy);
EightBitter.QuadsKeeper.shiftQuadrants(-dx, -dy);
}
/**
* Scrolls everything but a single Thing.
*
*
* @this {EightBittr}
* @param {Thing} thing
* @param {Number} dx How far to scroll horizontally.
* @param {Number} [dy] How far to scroll vertically.
*/
function scrollThing(thing, dx, dy) {
var saveleft = thing.left,
savetop = thing.top;
thing.EightBitter.scrollWindow(dx, dy);
thing.EightBitter.setLeft(thing, saveleft);
thing.EightBitter.setTop(thing, savetop);
}
/**
* Spawns all Things within a given area that should be there.
*
* @param {EightBittr} EightBitter
* @param {String} direction
* @param {Number} top
* @param {Number} right
* @param {Number} bottom
* @param {Number} left
* @remarks This is generally called by a QuadsKeepr during a screen update.
*/
function onAreaSpawn(EightBitter, direction, top, right, bottom, left) {
EightBitter.MapsHandler.spawnMap(
direction,
(top + EightBitter.MapScreener.top) / EightBitter.unitsize,
(right + EightBitter.MapScreener.left) / EightBitter.unitsize,
(bottom + EightBitter.MapScreener.top) / EightBitter.unitsize,
(left + EightBitter.MapScreener.left) / EightBitter.unitsize
);
}
/**
* "Unspawns" all Things within a given area that should be gone by marking
* their PreThings as not in game.
*
* @param {EightBittr} EightBitter
* @param {String} direction
* @param {Number} top
* @param {Number} right
* @param {Number} bottom
* @param {Number} left
* @remarks This is generally called by a QuadsKeepr during a screen update.
*/
function onAreaUnspawn(EightBitter, direction, top, right, bottom, left) {
EightBitter.MapsHandler.unspawnMap(
direction,
(top + EightBitter.MapScreener.top) / EightBitter.unitsize,
(right + EightBitter.MapScreener.left) / EightBitter.unitsize,
(bottom + EightBitter.MapScreener.top) / EightBitter.unitsize,
(left + EightBitter.MapScreener.left) / EightBitter.unitsize
);
}
/**
* Adds a new Thing to the game at a given position, relative to the top
* left corner of the screen.
*
* @param {Mixed} thing What type of Thing to add. This may be a String of
* the class title, an Array containing the String
* and an Object of settings, or an actual Thing.
* @param {Number} [left] Defaults to 0.
* @param {Number} [top] Defaults to 0.
*/
function addThing(thing, left, top) {
if (typeof (thing) === "string" || thing instanceof String) {
thing = this.ObjectMaker.make(thing);
} else if (thing.constructor === Array) {
thing = this.ObjectMaker.make.apply(this.ObjectMaker, thing);
}
if (arguments.length > 2) {
thing.EightBitter.setLeft(thing, left);
thing.EightBitter.setTop(thing, top);
} else if (arguments.length > 1) {
thing.EightBitter.setLeft(thing, left);
}
thing.EightBitter.updateSize(thing);
thing.EightBitter.GroupHolder.getFunctions().add[thing.groupType](thing);
thing.placed = true;
// This will typically be a TimeHandler.cycleClass call
if (thing.onThingAdd) {
thing.onThingAdd(thing);
}
thing.EightBitter.PixelDrawer.setThingSprite(thing);
// This will typically be a spawn* call
if (thing.onThingAdded) {
thing.onThingAdded(thing);
}
thing.EightBitter.ModAttacher.fireEvent("onAddThing", thing, left, top);
return thing;
}
/**
* Processes a Thing so that it is ready to be placed in gameplay. There are
* a lot of steps here: width and height must be set with defaults and given
* to spritewidth and spriteheight, a quadrants Array must be given, the
* sprite must be set, attributes and onThingMake called upon, and initial
* class cycles and flipping set.
*
* @param {Thing} thing
* @param {String} type What type Thing this is (the name of the class).
* @param {Object} [settings] Additional settings to be given to the
* Thing.
* @param {Object} defaults The default settings for the Thing's class.
* @remarks This is generally called as the onMake call in an ObjectMakr.
*/
function thingProcess(thing, type, settings, defaults) {
// If the Thing doesn't specify its own title, use the type by default
thing.title = thing.title || type;
// If a width/height is provided but no spritewidth/height,
// use the default spritewidth/height
if (thing.width && !thing.spritewidth) {
thing.spritewidth = defaults.spritewidth || defaults.width;
}
if (thing.height && !thing.spriteheight) {
thing.spriteheight = defaults.spriteheight || defaults.height;
}
// "Infinity" height refers to objects that reach exactly to the bottom
if (thing.height === "Infinity") {
thing.height = thing.EightBitter.getAbsoluteHeight(thing.y) / thing.EightBitter.unitsize;
}
// Each thing has at least 4 maximum quadrants (for the QuadsKeepr)
var maxquads = 4,
num;
num = Math.floor(
thing.width * (
thing.EightBitter.unitsize / thing.EightBitter.QuadsKeeper.getQuadrantWidth()
)
);
if (num > 0) {
maxquads += ((num + 1) * maxquads / 2);
}
num = Math.floor(
thing.height * (
thing.EightBitter.unitsize / thing.EightBitter.QuadsKeeper.getQuadrantHeight()
)
);
if (num > 0) {
maxquads += ((num + 1) * maxquads / 2);
}
thing.maxquads = maxquads;
thing.quadrants = new Array(maxquads);
// Basic sprite information
thing.spritewidth = thing.spritewidth || thing.width;
thing.spriteheight = thing.spriteheight || thing.height;
// Sprite sizing
thing.spritewidthpixels = thing.spritewidth * thing.EightBitter.unitsize;
thing.spriteheightpixels = thing.spriteheight * thing.EightBitter.unitsize;
// Canvas, context, imageData
thing.canvas = thing.EightBitter.createCanvas(
thing.spritewidthpixels, thing.spriteheightpixels
);
thing.context = thing.canvas.getContext("2d");
thing.imageData = thing.context.getImageData(
0, 0, thing.spritewidthpixels, thing.spriteheightpixels
);
if (thing.opacity !== 1) {
thing.EightBitter.setOpacity(thing, thing.opacity);
}
// Attributes, such as Koopa.smart
if (thing.attributes) {
thingProcessAttributes(thing, thing.attributes, settings);
}
// Important custom functions
if (thing.onThingMake) {
thing.onThingMake(thing, settings);
}
// Initial class / sprite setting
thing.EightBitter.setSize(thing, thing.width, thing.height);
thing.EightBitter.setClassInitial(thing, thing.name || thing.title);
// Sprite cycles
var cycle;
if (cycle = thing.spriteCycle) {
thing.EightBitter.TimeHandler.addClassCycle(
thing, cycle[0], cycle[1] || null, cycle[2] || null
);
}
if (cycle = thing.spriteCycleSynched) {
thing.EightBitter.TimeHandler.addClassCycleSynched(
thing, cycle[0], cycle[1] || null, cycle[2] || null
);
}
// flipHoriz and flipVert initially
if (thing.flipHoriz) {
thing.EightBitter.flipHoriz(thing);
}
if (thing.flipVert) {
thing.EightBitter.flipVert(thing);
}
// Mods!
thing.EightBitter.ModAttacher.fireEvent(
"onThingMake", thing.EightBitter, thing, type, settings, defaults
);
}
/**
* Processes additional Thing attributes. For each attribute the Thing's
* class says it may have, if it has it, the attribute's key is appeneded to
* the Thing's name and the attribute value proliferated onto the Thing.
*
* @param {Thing} thing
* @param {Object} attributes
*/
function thingProcessAttributes(thing, attributes) {
var attribute;
// For each listing in the attributes...
for (attribute in attributes) {
// If the thing has that attribute as true:
if (thing[attribute]) {
// Add the extra options
proliferate(thing, attributes[attribute]);
// Also add a marking to the name, which will go into the className
if (thing.name) {
thing.name += ' ' + attribute;
} else {
thing.name = thing.title + ' ' + attribute;
}
}
}
}
/**
* Runs through commands generated by a WorldSeedr and evaluates all of
* to create PreThings via MapsCreator.analyzePreSwitch.
*
* @param {EightBittr} EightBitter
* @param {Object[]} generatedCommands The commands generated by a
* WorldSeedr.generateFull call.
*/
function mapPlaceRandomCommands(EightBitter, generatedCommands) {
var MapsCreator = EightBitter.MapsCreator,
MapsHandler = EightBitter.MapsHandler,
prethings = MapsHandler.getPreThings(),
area = MapsHandler.getArea(),
map = MapsHandler.getMap(),
command, output, i;
for (i = 0; i < generatedCommands.length; i += 1) {
command = generatedCommands[i];
output = {
"thing": command.title,
"x": command.left,
"y": command.top
};
if (command.arguments) {
EightBitter.proliferateHard(output, command.arguments, true);
}
MapsCreator.analyzePreSwitch(output, prethings, area, map);
}
}
/* Physics & similar
*/
/**
* Sets a Thing's "changed" flag to true, which indicates to the PixelDrawr
* to redraw the Thing and its quadrant.
*
* @param {Thing} thing
*/
function markChanged(thing) {
thing.changed = true;
}
/**
* Shifts a Thing vertically using the EightBittr utility, and marks the
* Thing as having a changed appearance.
*
* @param {Thing} thing
* @param {Number} dy
* @param {Boolean} [notChanged] Whether to skip marking the Thing as
* changed (by default, false).
*/
function shiftVert(thing, dy, notChanged) {
EightBittr.prototype.shiftVert(thing, dy);
if (!notChanged) {
thing.EightBitter.markChanged(thing);
}
}
/**
* Shifts a Thing horizontally using the EightBittr utility, and marks the
* Thing as having a changed appearance.
*
* @param {Thing} thing
* @param {Number} dx
* @param {Boolean} [notChanged] Whether to skip marking the Thing as
* changed (by default, false).
*/
function shiftHoriz(thing, dx, notChanged) {
EightBittr.prototype.shiftHoriz(thing, dx);
if (!notChanged) {
thing.EightBitter.markChanged(thing);
}
}
/**
* Sets a Thing's top using the EightBittr utility, and marks the Thing as
* having a changed appearance.
*
* @param {Thing} thing
* @param {Number} top
*/
function setTop(thing, top) {
EightBittr.prototype.setTop(thing, top);
thing.EightBitter.markChanged(thing);
}
/**
* Sets a Thing's right using the EightBittr utility, and marks the Thing as
* having a changed appearance.
*
* @param {Thing} thing
* @param {Number} right
*/
function setRight(thing, right) {
EightBittr.prototype.setRight(thing, right);
thing.EightBitter.markChanged(thing);
}
/**
* Sets a Thing's bottom using the EightBittr utility, and marks the Thing
* as having a changed appearance.
*
* @param {Thing} thing
* @param {Number} bottom
*/
function setBottom(thing, bottom) {
EightBittr.prototype.setBottom(thing, bottom);
thing.EightBitter.markChanged(thing);
}
/**
* Sets a Thing's left using the EightBittr utility, and marks the Thing
* as having a changed appearance.
*
* @param {Thing} thing
* @param {Number} left
*/
function setLeft(thing, left) {
EightBittr.prototype.setLeft(thing, left);
thing.EightBitter.markChanged(thing);
}
/**
* Shifts a thing both horizontally and vertically. If the Thing marks
* itself as having a parallax effect (parallaxHoriz or parallaxVert), that
* proportion of movement is respected (.5 = half, etc.).
*
* @param {Thing} thing
* @param {Number} dx
* @param {Number} dy
* @param {Boolean} [notChanged] Whether to skip marking the Thing as
* changed (by default, false).
*/
function shiftBoth(thing, dx, dy, notChanged) {
dx = dx || 0;
dy = dy || 0;
if (!thing.noshiftx) {
if (thing.parallaxHoriz) {
thing.EightBitter.shiftHoriz(
thing, thing.parallaxHoriz * dx, notChanged
);
} else {
thing.EightBitter.shiftHoriz(thing, dx, notChanged);
}
}
if (!thing.noshifty) {
if (thing.parallaxVert) {
thing.EightBitter.shiftVert(
thing, thing.parallaxVert * dy, notChanged
);
} else {
thing.EightBitter.shiftVert(thing, dy, notChanged);
}
}
}
/**
* Calls shiftBoth on all members of an Array.
*
* @param {Number} dx
* @param {Number} dy
* @param {Boolean} [notChanged] Whether to skip marking the Thing as
* changed (by default, false).
*/
function shiftThings(things, dx, dy, notChanged) {
for (var i = things.length - 1; i >= 0; i -= 1) {
things[i].EightBitter.shiftBoth(things[i], dx, dy, notChanged);
}
}
/**
* Calls shiftBoth on all groups in the calling EightBittr's GroupHoldr.
*
* @this {EightBittr}
* @param {Number} dx
* @param {Number} dy
*/
function shiftAll(dx, dy) {
var EightBitter = EightBittr.ensureCorrectCaller(this);
EightBitter.GroupHolder.callAll(
EightBitter, EightBitter.shiftThings, dx, dy, true
);
}
/**
* Sets the width and unitwidth of a Thing, and optionally updates the
* Thing's spritewidth and spritewidth pixels, and/or calls updateSize.
* The thing is marked as having changed appearance.
*
* @param {Thing} thing
* @param {Number} width
* @param {Boolean} [updateSprite] Whether to update the Thing's
* spritewidth and spritewidthpixels (by
* default, false).
* @param {Boolean} [updateSize] Whether to call updateSize on the Thing
* (by default, false).
*/
function setWidth(thing, width, updateSprite, updateSize) {
thing.width = width;
thing.unitwidth = width * thing.EightBitter.unitsize;
if (updateSprite) {
thing.spritewidth = width;
thing.spritewidthpixels = width * thing.EightBitter.unitsize;
}
if (updateSize) {
thing.EightBitter.updateSize(thing);
}
thing.EightBitter.markChanged(thing);
}
/**
* Sets the height and unitheight of a Thing, and optionally updates the
* Thing's spriteheight and spriteheight pixels, and/or calls updateSize.
* The thing is marked as having changed appearance.
*
* @param {Thing} thing
* @param {Number} height
* @param {Boolean} [updateSprite] Whether to update the Thing's
* spriteheight and spriteheightpixels (by
* default, false).
* @param {Boolean} [updateSize] Whether to call updateSize on the Thing
* (by default, false).
*/
function setHeight(thing, height, updateSprite, updateSize) {
thing.height = height;
thing.unitheight = height * thing.EightBitter.unitsize;
if (updateSprite) {
thing.spriteheight = height;
thing.spriteheightpixels = height * thing.EightBitter.unitsize;
}
if (updateSize) {
thing.EightBitter.updateSize(thing);
}
thing.EightBitter.markChanged(thing);
}
/**
* Utility to call both setWidth and setHeight on a Thing.
*
* @param {Thing} thing
* @param {Number} width