/
Oversimplified.js
1909 lines (1675 loc) · 72.5 KB
/
Oversimplified.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
/*OversimplifiedJS
Author: Robbie Antenesse
Description: A lightweight, modular 2-D game engine for HTML5 canvas & JavaScript designed to try to make it as simple as possible to get your game made.
*/
var Oversimplified = {};
var OS = Oversimplified; // Handy-dandy alias for shortening code.
// Engine variables
Oversimplified.canvas = null;
Oversimplified.context = null;
Oversimplified.nextID = 0;
Oversimplified.loadingScripts = [];
Oversimplified.loadedScripts = [];
Oversimplified.emptyImage = new Image();
Oversimplified.emptyImage.src = "data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=";
Oversimplified.emptyImage.width = 1;
Oversimplified.emptyImage.height = 1;
// Settings Namespace
Oversimplified.Settings = {
defaultStep : 1/30,
numberOfScriptsToLoad : 0,
soundVolume : 0.75,
musicVolume : 0.75,
preventRightClick : true
}
// Convenient alias for Settings.
Oversimplified.S = Oversimplified.Settings;
// Time variables
Oversimplified.timestamp = function() {
return window.performance && window.performance.now ? window.performance.now() : new Date().getTime();
}
Oversimplified.now = null;
Oversimplified.dateTime = 0;
Oversimplified.lastFrame = Oversimplified.timestamp();
Oversimplified.step = Oversimplified.Settings.defaultStep; //seconds per frame, i.e. one 30th of a second passes each frame.
// Camera Object
Oversimplified.camera = {
x: 0,
y: 0,
width: 640,
height: 480,
hBorder: 64,
vBorder: 64,
following: "",
// Set the object for the camera to follow.
Follow: function (object) {
this.following = object.name;
}
}
/* Set up the camera.
It is important that this is done first at the time the game is loaded because this determines the size of the HTML5 canvas.
Be sure that the objectToFollow has already been created in the current room. Can be referenced with a variable.
objectToFollow, hBorder, and vBorder are optional arguments, but if you want to set hBorder and vBorder, there must be an objectToFollow.
*/
Oversimplified.SetCamera = function (options) {
Oversimplified.camera.width = typeof options.width !== 'undefined' ? options.width : Oversimplified.camera.width;
Oversimplified.camera.height = typeof options.height !== 'undefined' ? options.height : Oversimplified.camera.height;
Oversimplified.SetCanvasToCameraSize();
Oversimplified.camera.x = typeof options.x !== 'undefined' ? options.x : Oversimplified.camera.x;
Oversimplified.camera.y = typeof options.y !== 'undefined' ? options.y : Oversimplified.camera.y;
if (typeof options.objectToFollow !== 'undefined') {
if (options.objectToFollow.name) {
Oversimplified.camera.Follow(options.objectToFollow);
} else {
if (Oversimplified.DEBUG.showMessages) console.log("Oversimplified.Settings.SetCamera()'s objectToFollow argument must be a Oversimplified.GameObject.");
}
}
Oversimplified.camera.hBorder = (typeof options.hBorder !== 'undefined') ? options.hBorder : Oversimplified.camera.hBorder;
Oversimplified.camera.vBorder = (typeof options.vBorder !== 'undefined') ? options.vBorder : Oversimplified.camera.vBorder;
}
// Mouse Object
Oversimplified.mouse = {
x: 0,
y: 0,
leftCode: IsInternetExplorer() ? 1 : 0,
middleCode: IsInternetExplorer() ? 4 : 1,
rightCode: 2,
leftDown: false,
left: false,
leftUp: false,
middleDown: false,
middle: false,
middleUp: false,
rightDown: false,
right: false,
rightUp: false,
wheel: 0
}
// Lists of Detected Keys
Oversimplified.heldKeys = [];
Oversimplified.pressedKeys = [];
Oversimplified.releasedKeys = [];
/* Key definitions
Get Key name based on keycode.
*/
Oversimplified.Key = {
37: "left arrow",
38: "up arrow",
39: "right arrow",
40: "down arrow",
45: "insert",
46: "delete",
8: "backspace",
9: "tab",
13: "enter",
16: "shift",
17: "ctrl",
18: "alt",
19: "pause",
20: "caps lock",
27: "escape",
32: "space",
33: "page up",
34: "page down",
35: "end",
91: "left win/special key",
92: "right win/special key",
93: "select key",
96: "numpad 0",
97: "numpad 1",
98: "numpad 2",
99: "numpad 3",
100: "numpad 4",
101: "numpad 5",
102: "numpad 6",
103: "numpad 7",
104: "numpad 8",
105: "numpad 9",
106: "numpad asterisk",
107: "numpad plus",
109: "numpad dash",
110: "numpad period",
111: "numpad slash",
112: "f1",
113: "f2",
114: "f3",
115: "f4",
116: "f5",
117: "f6",
118: "f7",
119: "f8",
120: "f9",
121: "f10",
122: "f11",
123: "f12",
144: "num lock",
145: "scroll lock",
186: "semicolon",
187: "equal",
188: "comma",
189: "dash",
190: "period",
191: "slash",
192: "grave accent",
219: "open bracket",
220: "backslash",
221: "close bracket",
222: "quote"
};
// Get Keycode based on key name
Oversimplified.Keycode = {
backspace: 8,
tab: 9,
enter: 13,
shift: 16,
ctrl: 17,
alt: 18,
pausebreak: 19,
capslock: 20,
escape: 27,
space: 32,
pageup: 33,
pagedown: 34,
end: 35,
home: 36,
left: 37,
up: 38,
right: 39,
down: 40,
insert: 45,
del: 46,
zero: 48,
one: 49,
two: 50,
three: 51,
four: 52,
five: 53,
six: 54,
seven: 55,
eight: 56,
nine: 57,
a: 65,
b: 66,
c: 67,
d: 68,
e: 69,
f: 70,
g: 71,
h: 72,
i: 73,
j: 74,
k: 75,
l: 76,
m: 77,
n: 78,
o: 79,
p: 80,
q: 81,
r: 82,
s: 83,
t: 84,
u: 85,
v: 86,
w: 87,
x: 88,
y: 89,
z: 90,
leftwinkey: 91,
rightwinkey: 92,
selectkey: 93,
numpad_0: 96,
numpad_1: 97,
numpad_2: 98,
numpad_3: 99,
numpad_4: 100,
numpad_5: 101,
numpad_6: 102,
numpad_7: 103,
numpad_8: 104,
numpad_9: 105,
numpad_asterisk: 106,
numpad_plus: 107,
numpad_dash: 109,
numpad_period: 110,
numpad_slash: 111,
f1: 112,
f2: 113,
f3: 114,
f4: 115,
f5: 116,
f6: 117,
f7: 118,
f8: 119,
f9: 120,
f10: 121,
f11: 122,
f12: 123,
numlock: 144,
scrolllock: 145,
semicolon: 186,
equal: 187,
comma: 188,
dash: 189,
period: 190,
slash: 191,
grave: 192,
openbracket: 219,
backslash: 220,
closebraket: 221,
quote: 222
}
// Controls Namespace
Oversimplified.Controls = {};
// Add a control to the collection of Controls.
Oversimplified.Controls.Add = function(name, positiveKeycode, negativeKeycode) {
if (typeof negativeKeycode !== 'undefined') {
Oversimplified.Controls[name] = new Oversimplified.Axis(positiveKeycode, negativeKeycode);
} else {
Oversimplified.Controls[name] = new Oversimplified.Control(positiveKeycode);
}
return Oversimplified.Controls[name];
};
// Alias for OS.Controls.Add()
Oversimplified.Controls.New = Oversimplified.Controls.Add;
// Checks each control every frame for presses/releases/holds
Oversimplified.Controls.CheckAll = function () {
for (var control in Oversimplified.Controls) {
if (typeof Oversimplified.Controls[control].Check !== 'undefined') {
Oversimplified.Controls[control].Check();
}
}
};
// Convenient alias for Controls
Oversimplified.C = Oversimplified.Controls;
// Control Class
Oversimplified.Control = function (keycode) {
var self = this;
this.keyCode = keycode;
this.keyName = Oversimplified.Key[keycode];
this.down = this.pressed = false;
this.held = false;
this.up = this.released = false;
}
Oversimplified.Control.prototype.type = "Control";
Oversimplified.Control.prototype.Check = function () {
if (Oversimplified.heldKeys.indexOf(this.keyCode) != -1) {
this.held = true;
} else {
this.held = false;
}
if (Oversimplified.pressedKeys.indexOf(this.keyCode) != -1) {
this.down = this.pressed = true;
} else {
this.down = this.pressed = false;
}
if (Oversimplified.releasedKeys.indexOf(this.keyCode) != -1) {
this.up = this.released = true;
} else {
this.up = this.released = false;
}
}
//Axis Class
Oversimplified.Axis = function (positiveKeycode, negativeKeycode) {
//Keeps track of a direction, either -1, 0, or 1
var self = this;
this.positiveKeycode = positiveKeycode;
this.positiveKeyName = Oversimplified.Key[positiveKeycode];
this.negativeKeycode = negativeKeycode;
this.negativeKeyName = Oversimplified.Key[negativeKeycode];
this.direction = 0;
}
Oversimplified.Axis.prototype.type = "Axis";
Oversimplified.Axis.prototype.Check = function () {
if (Oversimplified.heldKeys.indexOf(this.positiveKeycode) != -1
&& Oversimplified.heldKeys.indexOf(this.negativeKeycode) == -1)
{
this.direction = 1;
}
if (Oversimplified.heldKeys.indexOf(this.negativeKeycode) != -1
&& Oversimplified.heldKeys.indexOf(this.positiveKeycode) == -1)
{
this.direction = -1;
}
if ( (Oversimplified.heldKeys.indexOf(this.negativeKeycode) == -1 //If neither are held
&& Oversimplified.heldKeys.indexOf(this.positiveKeycode) == -1)
|| (Oversimplified.heldKeys.indexOf(this.negativeKeycode) != -1 //or both are held
&& Oversimplified.heldKeys.indexOf(this.positiveKeycode) != -1) )
{
this.direction = 0;
}
}
//Rooms Namespace
Oversimplified.Rooms = {
currentRoom: "Default",
AllBeforeDo: function () {},
AllDo: function () {},
AllAfterDo: function () {}
}
// Add a Room to the collection of Rooms
Oversimplified.Rooms.Add = function (name, options) {
if (typeof Oversimplified.Rooms[name] === 'undefined') {
Oversimplified.Rooms[name] = new Oversimplified.Room(name, options);
return Oversimplified.Rooms[name];
} else {
if (Oversimplified.DEBUG.showMessages) console.log("A Room with the name \"" + name + "\" already exists!");
return false;
}
}
// Alias for Rooms.Add
Oversimplified.Rooms.New = Oversimplified.Rooms.Add;
// Convenient alias for Rooms
Oversimplified.R = Oversimplified.Rooms;
// Convenient way to access the objects within the current room.
Oversimplified.O = null;
/* Room Class
name (required) : The unique identifier of the room. Used to locate the room within the Rooms namespace.
options (optional) : An object with extra parameters for the Room. Options include:
width : The width of the room. The camera will not travel beyond this to the right. If it is larger than the camera's width and there is an object being followed by the camera, the camera can scroll to the farther portions of the room. If it is smaller than the camera's width, it will be set to the camera's width. -- default = Oversimplified.camera.width
height : The height of the room. The camera will not travel beyond this to the bottom. If it is larger than the camera's height and there is an object being followed by the camera, the camera can scroll to the farther portions of the room. If it is smaller than the camera's height, it will be set to the camera's height. -- default = Oversimplified.camera.height
backgroundSrc : The image that will be displayed as the room's background. If excluded or set to empty string (""), no background will show.
stepSpeed : The step speed for the Room. If excluded or set to 0, the default is used. -- default = Oversimplified.Settings.defaultStep
backgroundSize : Sets the room size to whatever the backgroundSrc image size is, regardless of what is entered as width and height!
backgroundColor : Any hex color value. Sets the far background color (behind the background image, visible only if transparent or excluded). A JavaScript alternative to setting the HTML5 canvas's background color CSS.
foreground : Path to any image file, though .png or .gif file with transparency is ideal. Sets the foreground image that displays over the background and all objects in the room. Appears below the Room's DrawAbove() function but above any GameObject's DrawAbove() function.
*/
Oversimplified.Room = function (name, options) {
this.id = Oversimplified.nextID++;
var self = this;
this.name = name;
options = typeof options !== 'undefined' ? options : {};
options.width = (typeof options.width !== 'undefined' && options.width >= Oversimplified.camera.width) ? options.width : Oversimplified.camera.width;
options.height = (typeof options.height !== 'undefined' && options.height >= Oversimplified.camera.height) ? options.height : Oversimplified.camera.height;
options.stepSpeed = (typeof options.stepSpeed !== 'undefined' && options.stepSpeed > 0) ? options.stepSpeed : Oversimplified.Settings.defaultStep;
this.width = options.width;
this.height = options.height;
if (typeof options.backgroundSrc !== 'undefined' && options.backgroundSrc != "") {
this.background = new Image();
this.background.src = options.backgroundSrc;
} else {
// If options.backgroundSrc is excluded or an empty string, instead use Oversimplified.emptyImage instead.
this.background = Oversimplified.emptyImage;
}
this.background.loaded = false;
if (this.background != Oversimplified.emptyImage) {
this.background.onload = function () {
this.loaded = true;
// If options "backgroundSize" is set to true, then make room the size of the background image.
if (options.backgroundSize == true) {
self.width = this.width;
self.height = this.height;
}
}
}
this.stepSpeed = options.stepSpeed;
this.objects = {};
this.O = this.objects;
if (typeof options.backgroundColor !== 'undefined') {
self.background.color = options.backgroundColor;
}
if (typeof options.foreground !== 'undefined') {
self.foreground = new Image();
self.foreground.loaded = false;
self.foreground.src = options.foreground;
self.foreground.onload = function () {this.loaded = true;}
}
// Set any extra properties from Options.
for (var property in options) {
if (typeof this[property] === 'undefined') {
this[property] = options[property];
}
}
this.drawOrder = [];
this.DoFirst = function () {};
this.BeforeDo = function () {};
this.Do = function () {};
this.AfterDo = function () {};
this.DoLast = function () {};
this.DrawBelow = function () {};
this.DrawAbove = function () {};
}
Oversimplified.Room.prototype.type = "Room";
Oversimplified.Room.prototype.Start = function () {
this.DoFirst();
if (this.name === Oversimplified.Rooms.currentRoom) {
for (var object in this.objects) {
this.objects[object].Start();
}
}
}
Oversimplified.Room.prototype.Update = function () {
if (Oversimplified.step != this.stepSpeed) {
Oversimplified.step = this.stepSpeed;
}
this.drawOrder = []; //Determine draw order every frame
for (var object in this.objects) {
if (this.objects[object].type == 'GameObject') {
if (this.drawOrder.length <= 0) { //If this is the first object checked,
this.drawOrder = [object]; //Add it to the array
continue; //And move to the next object without sorting
}
var depth = this.objects[object].depth;
for (var i = 0; i < this.drawOrder.length; i++) { //Loop through the objects already in array
if (depth < this.objects[this.drawOrder[i]].depth) { //if the object's depth is less than the object being checked,
this.drawOrder.splice(i, 0, object); //insert the object before it in the array
break; //and stop looking in the array
}
}
if (this.drawOrder.indexOf(object) < 0) { //if it gets through the loop and the depth is not less than any object,
this.drawOrder.push(object); //put it at the end
}
}
}
this.BeforeDo();
this.Do();
if (this.name === Oversimplified.Rooms.currentRoom) {
for (var object in this.objects) {
this.objects[object].Update();
}
}
this.AfterDo();
}
Oversimplified.Room.prototype.End = function () {
this.DoLast();
}
Oversimplified.Room.prototype.Draw = function () {
var self = this;
//Always draw background first if there is one
if (typeof this.background.color !== 'undefined') {
var tmp = Oversimplified.context.fillStyle;
Oversimplified.context.fillStyle = this.background.color;
Oversimplified.context.fillRect(0, 0, Oversimplified.camera.width, Oversimplified.camera.height);
Oversimplified.context.fillStyle = tmp;
}
if (this.background.loaded) {
Oversimplified.context.drawImage(self.background, Oversimplified.camera.x, Oversimplified.camera.y, (Oversimplified.camera.width <= self.background.width) ? Oversimplified.camera.width : self.background.width, (Oversimplified.camera.height <= self.background.height) ? Oversimplified.camera.height : self.background.height, 0, 0, self.background.width, self.background.height);
}
this.DrawBelow(); //Draw this before any objects are drawn
// if (this.name === Oversimplified.Rooms.currentRoom) {
for (var i = 0; i < this.drawOrder.length; i++) {
if (typeof this.objects[this.drawOrder[i]] !== 'undefined') {
this.objects[this.drawOrder[i]].Draw();
}
}
// }
// If there is a foreground, draw it.
if (typeof this.foreground !== 'undefined') {
if (this.foreground.loaded) {
Oversimplified.context.drawImage(self.foreground, Oversimplified.camera.x, Oversimplified.camera.y, Oversimplified.camera.width, Oversimplified.camera.height, 0, 0, self.foreground.width, self.foreground.height);
}
}
this.DrawAbove(); //Draw this after all other drawing is done
}
// Add a GameObject or PremadeObject to the room.
Oversimplified.Room.prototype.AddObject = function (objectOrNewName, objectOptions) {
objectOptions = (typeof objectOptions !== 'undefined') ? objectOptions : {};
var self = this;
if (objectOrNewName.type == "GameObject") { //Create from prefabricated object
// Overwrite manual id or name, if entered.
objectOptions.id = Oversimplified.nextID++;
objectOptions.name = objectOrNewName.name + objectOptions.id.toString();
// console.log(objectOptions.name);
self.objects[objectOptions.name] = Oversimplified.CopyObject(objectOrNewName, objectOptions);
return self.objects[objectOptions.name];
}
else {
if (self.objects[objectOrNewName]) {
if (Oversimplified.DEBUG.showMessages) console.log("Object with name \"" + objectOrNewName + "\" already exists in current room!");
return false;
}
self.objects[objectOrNewName] = new Oversimplified.GameObject(objectOrNewName, objectOptions);
return self.objects[objectOrNewName];
}
}
// Create an object in the current room.
Oversimplified.Create = function (objectOrNewName, objectOptions) {
return Oversimplified.Rooms[Oversimplified.Rooms.currentRoom].AddObject(objectOrNewName, objectOptions);
}
// Change to the specified room.
// Runs the current Room's End() function, changes the room, and runs the specified Room's Start() function.
Oversimplified.SetRoom = function (room) {
if (typeof Oversimplified.Rooms[Oversimplified.Rooms.currentRoom] !== 'undefined') {
Oversimplified.Rooms[Oversimplified.Rooms.currentRoom].End();
}
Oversimplified.Rooms.currentRoom = room.name;
Oversimplified.O = Oversimplified.Rooms[Oversimplified.Rooms.currentRoom].objects; //Update the Oversimplified.O alias when room changes
Oversimplified.camera.following = "";
Oversimplified.Rooms[Oversimplified.Rooms.currentRoom].Start();
}
// PremadeObjects (Prefab) Namespace
Oversimplified.PremadeObjects = {};
// Add a GameObject to the list of PremadeObjects.
Oversimplified.PremadeObjects.Add = function (name, objectOptions) {// x, y, imageSrc, maskImageSrc, animationsArray) {
if (typeof Oversimplified.PremadeObjects[name] === 'undefined') {
Oversimplified.PremadeObjects[name] = new Oversimplified.GameObject(name, objectOptions);// x, y, imageSrc, maskImageSrc, animationsArray);
return Oversimplified.PremadeObjects[name];
} else {
if (Oversimplified.DEBUG.showMessages) console.log("A Premade Object with the name \"" + name + "\" already exists!");
return false;
}
}
// Alias for PremadeObjects.Add().
Oversimplified.PremadeObjects.New = Oversimplified.PremadeObjects.Add;
// Convenient aliases for PremadeObjects.
Oversimplified.Prefabs = Oversimplified.PremadeObjects; // In case someone likes the technical "prefab" term better.
Oversimplified.P = Oversimplified.PremadeObjects;
// GameObject class
Oversimplified.GameObject = function (name, options) {// x, y, imageSrc, maskImageSrc, animationsArray) {
this.id = Oversimplified.nextID++;
var self = this;
this.self = self;
this.doFirstHasRun = false;
//Required Options
this.name = name;
//Optional Options
this.depth = typeof options.depth !== 'undefined' ? options.depth : 0; // Objects with higher depth are drawn *later* than (above) objects with lower depths. Objects with the same depth are drawn in the order they are created.
this.solid = typeof options.solid !== 'undefined' ? options.solid : false;
this.persistent = typeof options.persistent !== 'undefined' ? options.persistent : false;
this.x = typeof options.x !== 'undefined' ? options.x : -1;
this.y = typeof options.y !== 'undefined' ? options.y : -1;
this.xPrevious = this.x;
this.yPrevious = this.y;
this.screenX = this.x - Oversimplified.camera.x;
this.screenY = this.y - Oversimplified.camera.y;
if (typeof options.imageSrc !== 'undefined' && options.imageSrc != "") {
this.image = new Image();
this.image.src = options.imageSrc;
} else {
this.image = Oversimplified.emptyImage;
}
this.image.xScale = typeof options.xScale !== 'undefined' ? options.xScale : 1;
this.image.yScale = typeof options.yScale !== 'undefined' ? options.yScale : this.image.xScale;
this.image.rotation = typeof options.rotation !== 'undefined' ? Math.clampAngle(options.rotation) : 0;
this.image.animations = {};
this.image.frameColumn = 0;
this.image.frameRow = 0;
if (typeof options.animations !== 'undefined') {
for (var i = 0; i < options.animations.length; i++) {
if (i == 0 && options.animations[i].name != "Default") {
this.image.animations["Default"] = options.animations[i]; // Creates a duplicate animation of the first animation called "Default" in addition to the named animation below (unless the animation's name is "Default")
}
this.image.animations[options.animations[i].name] = options.animations[i];
}
} else {
if (this.image != Oversimplified.emptyImage) {
//If no animations array is included, then just show the whole image
this.image.onload = function(){this.animations["Default"] = new Oversimplified.Animation("newAnimation", {width: this.width, height: this.height});} // Creates the default animation as the whole image once the image is loaded.
} else {
this.image.animations["Default"] = new Oversimplified.Animation("newAnimation", {width: this.image.width, height: this.image.height});
}
}
this.image.currentAnimation = "Default";
this.mask = (options.maskImageSrc) ? new Image() : {};
this.mask.src = (options.maskImageSrc) ? options.maskImageSrc : "";
if (this.mask.src == "") {
this.mask.width = this.image.animations["Default"].width;
this.mask.height = this.image.animations["Default"].height;
}
if (this.mask.src != "") {
this.mask.onload = function(){
self.xBound = this.width / 2 * self.image.xScale;
self.yBound = this.height / 2 * self.image.yScale;
};
} else {
self.xBound = this.mask.width / 2 * self.image.xScale;
self.yBound = this.mask.height / 2 * self.image.yScale;
}
// Set any extra properties from Options.
for (var property in options) {
if (typeof this[property] === 'undefined') {
this[property] = options[property];
}
}
this.DoFirst = function () {};
this.BeforeDo = function () {};
this.Do = function () {};
this.AfterDo = function () {};
this.DoLast = function () {};
this.DrawBelow = function () {};
this.DrawAbove = function () {};
}
Oversimplified.GameObject.prototype.type = "GameObject";
Oversimplified.GameObject.prototype.AddAnimation = function (animation, animationWidth, animationHeight, animationOptions) {//columns, rows, speed, xOffset, yOffset) {
//Takes either an animation or the name of an animation in the Animations namespace and adds it to the object.
if (typeof animation.name !== 'undefined') {
this.image.animations[animationOptions.name] = animation;
} else {
if (typeof Oversimplified.Animations[animation] === 'undefined') {
Oversimplified.Animations.Add(animation, animationWidth, animationHeight, animationOptions);
}
this.image.animations[Oversimplified.Animations[animation].name] = Oversimplified.Animations[animation];
}
}
Oversimplified.GameObject.prototype.Draw = function () {
this.DrawBelow();
var self = this;
var animation = self.image.currentAnimation;
if (self.image.animations[animation]) {
var animationWidth = self.image.animations[animation].width;
var animationHeight = self.image.animations[animation].height;
var width = self.image.animations[animation].width * self.image.xScale;
var height = self.image.animations[animation].height * self.image.yScale;
var columns = self.image.animations[animation].columns;
var rows = self.image.animations[animation].rows;
var xOffset = self.image.animations[animation].xOffset;
var yOffset = self.image.animations[animation].yOffset;
var animationSpeed = self.image.animations[animation].speed;
if (self.image.frameColumn < columns) {
self.image.frameColumn += animationSpeed;
}
if (self.image.frameColumn >= columns) {
self.image.frameColumn = 0;
self.image.frameRow++;
}
if (self.image.frameRow > rows - 1) {
self.image.frameRow = 0;
}
if (Oversimplified.IsOnCamera(self)) {
var adjustedColumn = Math.floor(self.image.frameColumn);
var adjustedRow = Math.floor(self.image.frameRow);
Oversimplified.context.translate(self.x - Oversimplified.camera.x, self.y - Oversimplified.camera.y);
var angleInRadians = self.image.rotation * (Math.PI/180);
Oversimplified.context.rotate(angleInRadians);
Oversimplified.context.drawImage(self.image, (animationWidth * adjustedColumn) + xOffset, (animationHeight * adjustedRow) + yOffset, animationWidth, animationHeight, -(width / 2), -(height / 2), width, height);
Oversimplified.context.rotate(-angleInRadians);
Oversimplified.context.translate(-(self.x - Oversimplified.camera.x), -(self.y - Oversimplified.camera.y));
Oversimplified.DEBUG.objectsOnScreen++;
}
} else {
if (Oversimplified.DEBUG.showMessages) console.log("No animation at " + animation);
}
this.DrawAbove();
}
Oversimplified.GameObject.prototype.SetScale = function (xScale, yScale) {
//Negative scale does not flip image.
this.image.xScale = xScale;
this.image.yScale = typeof yScale !== 'undefined' ? yScale : xScale;
this.xBound = (this.mask.width / 2) * this.image.xScale;
this.yBound = (this.mask.height / 2) * this.image.yScale;
}
Oversimplified.GameObject.prototype.SetImageRotation = function (rotation) {
this.image.rotation = Math.clampAngle(rotation);
}
Oversimplified.GameObject.prototype.RotateImage = function (amount) {
this.image.rotation += Math.clampAngle(amount);
}
Oversimplified.GameObject.prototype.SetAnimation = function (which) {
if (which.name) { //If you enter an actual animation instead of just its name,
which = which.name; //only use its name
}
this.image.currentAnimation = which;
this.image.frameColumn = 0;
this.image.frameRow = 0;
}
Oversimplified.GameObject.prototype.Start = function () {
this.DoFirst();
}
Oversimplified.GameObject.prototype.Update = function () {
this.screenX = this.x - Oversimplified.camera.x;
this.screenY = this.y - Oversimplified.camera.y;
this.xPrevious = this.x;
this.yPrevious = this.y;
if (!this.doFirstHasRun) {
this.DoFirst();
this.doFirstHasRun = true;
}
this.BeforeDo();
this.Do();
this.AfterDo();
//Make sure rotation is a valid angle before drawing
this.image.rotation = Math.clampAngle(this.image.rotation);
}
Oversimplified.GameObject.prototype.End = function () {
this.DoLast();
}
// Move toward the given point at the given speed.
// Imprecise and only moves at 90° and 45° angles, but gets the job done.
Oversimplified.GameObject.prototype.MoveTo = function (x, y, speed) {
speed = typeof speed !== 'undefined' ? speed : 1;
if (this.x < x) {
this.x += speed;
}
if (this.x > x) {
this.x -= speed;
}
if (this.y < y) {
this.y += speed;
}
if (this.y > y) {
this.y -= speed;
}
}
// Check if the given point is within the object's bounds.
Oversimplified.GameObject.prototype.PointOverlaps = function (x, y) {
if (x > this.x - this.xBound
&& x < this.x + this.xBound
&& y > this.y - this.yBound
&& y < this.y + this.yBound)
{
return true;
} else {
return false;
}
}
// Check if object is overlapping any other object in the room
//
// Accepts true, false, or no value.
Oversimplified.GameObject.prototype.IsOverlapping = function (doSimple) {
doSimple = (typeof doSimple !== 'undefined') ? doSimple : false;
for (var obj in Oversimplified.O) {
var object = Oversimplified.O[obj];
if (object != this) {
// If doSimple is false or not set, then scan all pixels in object boundaries.
if (!doSimple)
{
for (var i = 0; i < 2 * object.xBound; i++) {
for (var j = 0; j < 2 * object.yBound; j++) {
var xToCheck = (object.x - object.xBound) + i;
var yToCheck = (object.y - object.yBound) + j;
if (xToCheck > this.x - this.xBound &&
xToCheck < this.x + this.xBound &&
yToCheck > this.y - this.yBound &&
yToCheck < this.y + this.yBound)
{ //Check if the point lies inside the bounds of ANY object in the room.
return object;
}
}
}
}
// If doSimple is true, only check the corner pixels and center pixels of object bounds. This makes for much faster checking.
else
{
if (object.PointOverlaps(this.x - this.xBound, this.y - this.yBound) ||
object.PointOverlaps(this.x + this.xBound, this.y - this.yBound) ||
object.PointOverlaps(this.x - this.xBound, this.y + this.yBound) ||
object.PointOverlaps(this.x + this.xBound, this.y + this.yBound) ||
object.PointOverlaps(this.x - this.xBound, this.y) ||
object.PointOverlaps(this.x + this.xBound, this.y) ||
object.PointOverlaps(this.x, this.y - this.yBound) ||
object.PointOverlaps(this.x, this.y + this.yBound))
{
return object;
}
}
}
}
return false;
}
// Move the object away from any overlapping objects.
//
// Accepts true, false, or no value.
Oversimplified.GameObject.prototype.IfOverlappingThenMove = function (doSimple) {
var overlappingObject = this.IsOverlapping(doSimple);
if (overlappingObject != false) {
if (this.x < overlappingObject.x)
this.x--;
if (this.x >= overlappingObject.x)
this.x++;
if (this.y < overlappingObject.y)
this.y--;
if (this.y >= overlappingObject.y)
this.y++;
return true;
} else {
return false;
}
}
// Prevents the object from moving outside of the room's boundaries.
Oversimplified.GameObject.prototype.KeepInsideRoom = function () {
var currentRoom = Oversimplified.Rooms[Oversimplified.Rooms.currentRoom]
if (this.x < this.xBound || this.x > currentRoom.width - this.xBound)
{
this.x = this.xPrevious;
}
if (this.y < this.yBound || this.y > currentRoom.height - this.yBound)
{
this.y = this.yPrevious;
}
}
// Returns true if the mouse is within the object's bounding box.
Oversimplified.GameObject.prototype.MouseIsOver = function () {
if (this.PointOverlaps(Oversimplified.mouse.x, Oversimplified.mouse.y))
{
return true;
} else {
return false;
}
}
// Returns true if the object is clicked with the given mouse click, eg. Oversimplified.mouse.leftDown, Oversimplified.mouse.rightUp, etc.
//
// If no click is specified, it defaults to left down
Oversimplified.GameObject.prototype.Clicked = function (mouseClick) {
mouseClick = typeof mouseClick !== 'undefined' ? mouseClick : Oversimplified.mouse.leftDown;
if (this.MouseIsOver() && mouseClick)
{
return true;
} else {
return false;
}
}
// Move the object based upon xSpeed and ySpeed, stopping if colliding with solid objects
//
// xSpeed and ySpeed are numbers, checkCollisions is true or false, and checkEveryXPixels is a number.
//
// Returns true if successfully moved and false if not.
Oversimplified.GameObject.prototype.SimpleMove = function (xSpeed, ySpeed, checkCollisions, checkEveryXPixels) {
checkEveryXPixels = (typeof checkEveryXPixels !== 'undefined') ? checkEveryXPixels : 2;
var collisionLeft = false,
collisionRight = false,
collisionUp = false,
collisionDown = false;
if (checkCollisions) {
for (var vert = 0; vert < this.yBound * 2; vert += checkEveryXPixels) {
var yToCheck = (this.y - this.yBound + vert);
if (!collisionLeft) {
collisionLeft = xSpeed < 0 && Oversimplified.CollisionAtPoint((this.x - this.xBound) + xSpeed, yToCheck);
}
if (!collisionRight) {
collisionRight = xSpeed > 0 && Oversimplified.CollisionAtPoint((this.x + this.xBound) + xSpeed, yToCheck);
}
}
for (var hor = 0; hor < this.xBound * 2; hor += checkEveryXPixels) {
var xToCheck = (this.x - this.xBound + hor);
if (!collisionUp) {
collisionUp = ySpeed < 0 && Oversimplified.CollisionAtPoint(xToCheck, (this.y - this.yBound) + ySpeed);
}
if (!collisionDown) {
collisionDown = ySpeed > 0 && Oversimplified.CollisionAtPoint(xToCheck, (this.y + this.yBound) + ySpeed);
}
}
}
if (!checkCollisions || (!collisionLeft && !collisionRight && !collisionUp && !collisionDown)) {
this.x += xSpeed;
this.y += ySpeed;
return true;
} else {
return false;
}
}