-
Notifications
You must be signed in to change notification settings - Fork 44
/
woof.js
2030 lines (1809 loc) · 79.4 KB
/
woof.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
// We include SAT.js here as our only "external" dependency to help us detect when rotated sprites intersect. It's not really an "external" dependency because we include it here internally.
// SAT.js - Version 0.6.0 - Copyright 2012 - 2016 - Jim Riecken <[email protected]> - released under the MIT License. https://github.com/jriecken/sat-js
function Vector(t,o){this.x=t||0,this.y=o||0}function Circle(t,o){this.pos=t||new Vector,this.r=o||0}function Polygon(t,o){this.pos=t||new Vector,this.angle=0,this.offset=new Vector,this.setPoints(o||[])}function Box(t,o,e){this.pos=t||new Vector,this.w=o||0,this.h=e||0}function Response(){this.a=null,this.b=null,this.overlapN=new Vector,this.overlapV=new Vector,this.clear()}function flattenPointsOn(t,o,e){for(var r=Number.MAX_VALUE,n=-Number.MAX_VALUE,s=t.length,i=0;s>i;i++){var p=t[i].dot(o);r>p&&(r=p),p>n&&(n=p)}e[0]=r,e[1]=n}function isSeparatingAxis(t,o,e,r,n,s){var i=T_ARRAYS.pop(),p=T_ARRAYS.pop(),c=T_VECTORS.pop().copy(o).sub(t),l=c.dot(n);if(flattenPointsOn(e,n,i),flattenPointsOn(r,n,p),p[0]+=l,p[1]+=l,i[0]>p[1]||p[0]>i[1])return T_VECTORS.push(c),T_ARRAYS.push(i),T_ARRAYS.push(p),!0;if(s){var h=0;if(i[0]<p[0])if(s.aInB=!1,i[1]<p[1])h=i[1]-p[0],s.bInA=!1;else{var a=i[1]-p[0],y=p[1]-i[0];h=y>a?a:-y}else if(s.bInA=!1,i[1]>p[1])h=i[0]-p[1],s.aInB=!1;else{var a=i[1]-p[0],y=p[1]-i[0];h=y>a?a:-y}var u=Math.abs(h);u<s.overlap&&(s.overlap=u,s.overlapN.copy(n),0>h&&s.overlapN.reverse())}return T_VECTORS.push(c),T_ARRAYS.push(i),T_ARRAYS.push(p),!1}function voronoiRegion(t,o){var e=t.len2(),r=o.dot(t);return 0>r?LEFT_VORONOI_REGION:r>e?RIGHT_VORONOI_REGION:MIDDLE_VORONOI_REGION}function pointInCircle(t,o){var e=T_VECTORS.pop().copy(t).sub(o.pos),r=o.r*o.r,n=e.len2();return T_VECTORS.push(e),r>=n}function pointInPolygon(t,o){TEST_POINT.pos.copy(t),T_RESPONSE.clear();var e=testPolygonPolygon(TEST_POINT,o,T_RESPONSE);return e&&(e=T_RESPONSE.aInB),e}function testCircleCircle(t,o,e){var r=T_VECTORS.pop().copy(o.pos).sub(t.pos),n=t.r+o.r,s=n*n,i=r.len2();if(i>s)return T_VECTORS.push(r),!1;if(e){var p=Math.sqrt(i);e.a=t,e.b=o,e.overlap=n-p,e.overlapN.copy(r.normalize()),e.overlapV.copy(r).scale(e.overlap),e.aInB=t.r<=o.r&&p<=o.r-t.r,e.bInA=o.r<=t.r&&p<=t.r-o.r}return T_VECTORS.push(r),!0}function testPolygonCircle(t,o,e){for(var r=T_VECTORS.pop().copy(o.pos).sub(t.pos),n=o.r,s=n*n,i=t.calcPoints,p=i.length,c=T_VECTORS.pop(),l=T_VECTORS.pop(),h=0;p>h;h++){var a=h===p-1?0:h+1,y=0===h?p-1:h-1,u=0,V=null;c.copy(t.edges[h]),l.copy(r).sub(i[h]),e&&l.len2()>s&&(e.aInB=!1);var T=voronoiRegion(c,l);if(T===LEFT_VORONOI_REGION){c.copy(t.edges[y]);var f=T_VECTORS.pop().copy(r).sub(i[y]);if(T=voronoiRegion(c,f),T===RIGHT_VORONOI_REGION){var R=l.len();if(R>n)return T_VECTORS.push(r),T_VECTORS.push(c),T_VECTORS.push(l),T_VECTORS.push(f),!1;e&&(e.bInA=!1,V=l.normalize(),u=n-R)}T_VECTORS.push(f)}else if(T===RIGHT_VORONOI_REGION){if(c.copy(t.edges[a]),l.copy(r).sub(i[a]),T=voronoiRegion(c,l),T===LEFT_VORONOI_REGION){var R=l.len();if(R>n)return T_VECTORS.push(r),T_VECTORS.push(c),T_VECTORS.push(l),!1;e&&(e.bInA=!1,V=l.normalize(),u=n-R)}}else{var O=c.perp().normalize(),R=l.dot(O),v=Math.abs(R);if(R>0&&v>n)return T_VECTORS.push(r),T_VECTORS.push(O),T_VECTORS.push(l),!1;e&&(V=O,u=n-R,(R>=0||2*n>u)&&(e.bInA=!1))}V&&e&&Math.abs(u)<Math.abs(e.overlap)&&(e.overlap=u,e.overlapN.copy(V))}return e&&(e.a=t,e.b=o,e.overlapV.copy(e.overlapN).scale(e.overlap)),T_VECTORS.push(r),T_VECTORS.push(c),T_VECTORS.push(l),!0}function testCirclePolygon(t,o,e){var r=testPolygonCircle(o,t,e);if(r&&e){var n=e.a,s=e.aInB;e.overlapN.reverse(),e.overlapV.reverse(),e.a=e.b,e.b=n,e.aInB=e.bInA,e.bInA=s}return r}function testPolygonPolygon(t,o,e){for(var r=t.calcPoints,n=r.length,s=o.calcPoints,i=s.length,p=0;n>p;p++)if(isSeparatingAxis(t.pos,o.pos,r,s,t.normals[p],e))return!1;for(var p=0;i>p;p++)if(isSeparatingAxis(t.pos,o.pos,r,s,o.normals[p],e))return!1;return e&&(e.a=t,e.b=o,e.overlapV.copy(e.overlapN).scale(e.overlap)),!0}var SAT={};SAT.Vector=Vector,SAT.V=Vector,Vector.prototype.copy=Vector.prototype.copy=function(t){return this.x=t.x,this.y=t.y,this},Vector.prototype.clone=Vector.prototype.clone=function(){return new Vector(this.x,this.y)},Vector.prototype.perp=Vector.prototype.perp=function(){var t=this.x;return this.x=this.y,this.y=-t,this},Vector.prototype.rotate=Vector.prototype.rotate=function(t){var o=this.x,e=this.y;return this.x=o*Math.cos(t)-e*Math.sin(t),this.y=o*Math.sin(t)+e*Math.cos(t),this},Vector.prototype.reverse=Vector.prototype.reverse=function(){return this.x=-this.x,this.y=-this.y,this},Vector.prototype.normalize=Vector.prototype.normalize=function(){var t=this.len();return t>0&&(this.x=this.x/t,this.y=this.y/t),this},Vector.prototype.add=Vector.prototype.add=function(t){return this.x+=t.x,this.y+=t.y,this},Vector.prototype.sub=Vector.prototype.sub=function(t){return this.x-=t.x,this.y-=t.y,this},Vector.prototype.scale=Vector.prototype.scale=function(t,o){return this.x*=t,this.y*=o||t,this},Vector.prototype.project=Vector.prototype.project=function(t){var o=this.dot(t)/t.len2();return this.x=o*t.x,this.y=o*t.y,this},Vector.prototype.projectN=Vector.prototype.projectN=function(t){var o=this.dot(t);return this.x=o*t.x,this.y=o*t.y,this},Vector.prototype.reflect=Vector.prototype.reflect=function(t){var o=this.x,e=this.y;return this.project(t).scale(2),this.x-=o,this.y-=e,this},Vector.prototype.reflectN=Vector.prototype.reflectN=function(t){var o=this.x,e=this.y;return this.projectN(t).scale(2),this.x-=o,this.y-=e,this},Vector.prototype.dot=Vector.prototype.dot=function(t){return this.x*t.x+this.y*t.y},Vector.prototype.len2=Vector.prototype.len2=function(){return this.dot(this)},Vector.prototype.len=Vector.prototype.len=function(){return Math.sqrt(this.len2())},SAT.Circle=Circle,Circle.prototype.getAABB=Circle.prototype.getAABB=function(){var t=this.r,o=this.pos.clone().sub(new Vector(t,t));return new Box(o,2*t,2*t).toPolygon()},SAT.Polygon=Polygon,Polygon.prototype.setPoints=Polygon.prototype.setPoints=function(t){var o=!this.points||this.points.length!==t.length;if(o){var e,r=this.calcPoints=[],n=this.edges=[],s=this.normals=[];for(e=0;e<t.length;e++)r.push(new Vector),n.push(new Vector),s.push(new Vector)}return this.points=t,this._recalc(),this},Polygon.prototype.setAngle=Polygon.prototype.setAngle=function(t){return this.angle=t,this._recalc(),this},Polygon.prototype.setOffset=Polygon.prototype.setOffset=function(t){return this.offset=t,this._recalc(),this},Polygon.prototype.rotate=Polygon.prototype.rotate=function(t){for(var o=this.points,e=o.length,r=0;e>r;r++)o[r].rotate(t);return this._recalc(),this},Polygon.prototype.translate=Polygon.prototype.translate=function(t,o){for(var e=this.points,r=e.length,n=0;r>n;n++)e[n].x+=t,e[n].y+=o;return this._recalc(),this},Polygon.prototype._recalc=function(){var t,o=this.calcPoints,e=this.edges,r=this.normals,n=this.points,s=this.offset,i=this.angle,p=n.length;for(t=0;p>t;t++){var c=o[t].copy(n[t]);c.x+=s.x,c.y+=s.y,0!==i&&c.rotate(i)}for(t=0;p>t;t++){var l=o[t],h=p-1>t?o[t+1]:o[0],a=e[t].copy(h).sub(l);r[t].copy(a).perp().normalize()}return this},Polygon.prototype.getAABB=Polygon.prototype.getAABB=function(){for(var t=this.calcPoints,o=t.length,e=t[0].x,r=t[0].y,n=t[0].x,s=t[0].y,i=1;o>i;i++){var p=t[i];p.x<e?e=p.x:p.x>n&&(n=p.x),p.y<r?r=p.y:p.y>s&&(s=p.y)}return new Box(this.pos.clone().add(new Vector(e,r)),n-e,s-r).toPolygon()},SAT.Box=Box,Box.prototype.toPolygon=Box.prototype.toPolygon=function(){var t=this.pos,o=this.w,e=this.h;return new Polygon(new Vector(t.x,t.y),[new Vector,new Vector(o,0),new Vector(o,e),new Vector(0,e)])},SAT.Response=Response,Response.prototype.clear=Response.prototype.clear=function(){return this.aInB=!0,this.bInA=!0,this.overlap=Number.MAX_VALUE,this};for(var T_VECTORS=[],i=0;10>i;i++)T_VECTORS.push(new Vector);for(var T_ARRAYS=[],i=0;5>i;i++)T_ARRAYS.push([]);var T_RESPONSE=new Response,TEST_POINT=new Box(new Vector,1e-6,1e-6).toPolygon();SAT.isSeparatingAxis=isSeparatingAxis;var LEFT_VORONOI_REGION=-1,MIDDLE_VORONOI_REGION=0,RIGHT_VORONOI_REGION=1;SAT.pointInCircle=pointInCircle,SAT.pointInPolygon=pointInPolygon,SAT.testCircleCircle=testCircleCircle,SAT.testPolygonCircle=testPolygonCircle,SAT.testCirclePolygon=testCirclePolygon,SAT.testPolygonPolygon=testPolygonPolygon;
function detectCollision(a, b){
if (a instanceof SAT.Circle && b instanceof SAT.Circle) {
return SAT.testCircleCircle(a, b)
} else if (a instanceof SAT.Polygon && b instanceof SAT.Circle) {
return SAT.testPolygonCircle(a, b)
} else if (a instanceof SAT.Circle && b instanceof SAT.Polygon) {
return SAT.testCirclePolygon(a, b)
} else if (a instanceof SAT.Polygon && b instanceof SAT.Polygon) {
return SAT.testPolygonPolygon(a, b)
} else {
throw Error('Unexpected Shape.')
}
}
// alias Image to BrowserImage because we will overwrite Image with Woof.Image
window.BrowserImage = Image;
function Woof({global = false, fullScreen = false, height = 500, width = 350} = {}) {
if(window.global) throw new Error("You must turn off global mode in the Woof script tag if you want to create your own Woof object.")
this.global = global;
// thisContext is either the Woof object or the window, depending on whether or not you start Woof in global mode
var thisContext = this.global ? window : this;
thisContext.global = global;
thisContext._sprites = [];
Object.defineProperty(thisContext, 'sprites', {
get: function() {
return thisContext._sprites;
},
set: function() {
throw new TypeError("sprites is used internaly by Woof and should not be modified");
}
});
thisContext.backdrop = {color: null, type: null, url: null, size: "100% 100%", repeat: "no-repeat"};
thisContext.stopped = true;
// internally named fullScreen1 because the keyword "fullScreen" on the global scope was wonky in firefox
thisContext.fullScreen1 = fullScreen;
thisContext._cameraX = 0;
thisContext._cameraY = 0;
Object.defineProperty(thisContext, 'cameraX', {
get: function() {
return thisContext._cameraX;
},
set: function(value) {
// whenever the camera is changed, update relevant values
thisContext.maxX = Math.round(value + this.width / 2);
thisContext.minX = Math.round(value - this.width / 2);
thisContext.mouseX += (value - thisContext._cameraX)
thisContext._cameraX = value;
}
});
Object.defineProperty(thisContext, 'cameraY', {
get: function() {
return thisContext._cameraY;
},
set: function(value) {
// whenever the camera is changed, update relevant values
thisContext.maxY = value + Math.round(this.height / 2);
thisContext.minY = value - Math.round(this.height / 2);
thisContext.mouseY += (value - thisContext._cameraY)
thisContext._cameraY = value;
}
});
Object.defineProperty(thisContext, 'fullScreen', {
get: function() {
return thisContext.fullScreen1;
},
set: function(value) {
thisContext.fullScreen1 = value;
}
});
thisContext._width = width;
thisContext._height = height;
Object.defineProperty(thisContext, 'width', {
get: function() {
return thisContext._width;
},
set: function(value) {
// throw an error if we're in fullscreen and it's updating to a invalid value
if (thisContext.fullScreen1 && value != window.innerWidth) {
throw new TypeError("width can't be changed if you are in fullscreen mode");
} else if (!thisContext.fullScreen1) {
// if we're not in fullscreen, this is the 'approved' way to modify
thisContext.setBackdropSize(value, thisContext._height);
} else {
// presumably this is from a resizing event, so just changes the private variable
thisContext._width = value;
}
}
});
Object.defineProperty(thisContext, 'height', {
get: function() {
return thisContext._height;
},
set: function(value) {
// throw an error if we're in fullscreen and it's updating to a invalid value
if (thisContext.fullScreen1 && value != window.innerHeight) {
throw new TypeError("height can't be changed if you are in fullscreen mode");
} else if (!thisContext.fullScreen1) {
// if we're not in fullscreen, this is the 'approved' way to modify
thisContext.setBackdropSize(thisContext._width, value);
} else {
// presumably this is from a resizing event, so just changes the private variable
thisContext._height = value;
}
}
});
if (thisContext.fullScreen1) {
width = window.innerWidth;
height = window.innerHeight;
window.addEventListener("load", () => {
document.body.style.margin = 0;
document.body.style.padding = 0;
});
}
thisContext._readys = [];
thisContext.ready = (func) => {
if (typeof func != "function") { throw new TypeError("ready(function) requires one function input."); }
if (thisContext.stopped) {
thisContext._readys.push(func);
} else {
func();
}
}
thisContext._runReadys = () => {
thisContext.stopped = false;
thisContext._readys.forEach(func => { func() });
thisContext._readys = [];
};
window.addEventListener("load", () => {
document.documentElement.style.width = "100%";
document.documentElement.style.height = "100%";
document.body.style.width = "100%";
document.body.style.height = "100%";
// if the project already exists, remove it so we don't have duplicates
// (this shouldn't happen, but firefox
// was firing this code twice which led to bugs)
if (document.getElementById('project') != null) {
document.getElementById('project').remove();
}
// create the main div that Woof lives in
thisContext._mainDiv = document.createElement("div");
document.body.appendChild(thisContext._mainDiv);
thisContext._mainDiv.id = "project";
thisContext._mainDiv.style.position = "relative";
thisContext._mainDiv.style.width = "100%";
thisContext._mainDiv.style.height = "100%";
// create the canvas where we will draw sprites
thisContext._spriteCanvas = document.createElement("canvas");
thisContext._mainDiv.appendChild(thisContext._spriteCanvas);
thisContext._spriteCanvas.id = "sprites";
thisContext._spriteCanvas.width = width;
thisContext._spriteCanvas.height = height;
thisContext._spriteCanvas.style.zIndex = 3;
thisContext._spriteCanvas.style.position = "absolute";
// create the canvas where we will draw the pen
thisContext._penCanvas = document.createElement("canvas");
thisContext._mainDiv.appendChild(thisContext._penCanvas);
thisContext._penCanvas.id = "pen";
thisContext._penCanvas.width = width;
thisContext._penCanvas.height = height;
thisContext._penCanvas.style.zIndex = 2;
thisContext._penCanvas.style.position = "absolute";
// create the div where we show the backdrop using CSS
thisContext._backdropDiv = document.createElement("div");
thisContext._mainDiv.appendChild(thisContext._backdropDiv);
thisContext._backdropDiv.id = "backdrop";
thisContext._backdropDiv.width = width;
thisContext._backdropDiv.height = height;
thisContext._backdropDiv.style.zIndex = 1;
thisContext._backdropDiv.style.position = "absolute";
thisContext._backdropDiv.style.width = "100%";
thisContext._backdropDiv.style.height = "100%";
thisContext._spriteContext = thisContext._spriteCanvas.getContext("2d");
thisContext._penContext = thisContext._penCanvas.getContext("2d");
thisContext._runReadys();
});
thisContext.setBackdropSize = (width, height) => {
if (typeof width != "number" || typeof height != "number") { throw new TypeError("setBackdropSize(width, height) requires two number inputs."); }
if (thisContext.fullScreen1) {
throw Error("You cannot manually set the backdrop size in full-screen mode. You can full-screen mode off with: fullScreen = false.")
} else {
thisContext._setCanvasSize(width, height);
}
}
thisContext._setCanvasSize = (width, height) => {
thisContext._height = height;
thisContext._width = width;
thisContext.minX = Math.round(thisContext.cameraX - thisContext.width / 2);
thisContext.maxX = Math.round(thisContext.cameraX + thisContext.width / 2);
thisContext.minY = Math.round(thisContext.cameraY - thisContext.height / 2);
thisContext.maxY = Math.round(thisContext.cameraY + thisContext.height / 2);
thisContext.ready(() => {
thisContext._spriteCanvas.width = thisContext.width;
thisContext._spriteCanvas.height = thisContext.height;
// when you change the canvas size, you have to copy the pen data onto the newly-sized canvas
var penData = thisContext._penContext.getImageData(0, 0, width, height);
thisContext._penCanvas.width = thisContext.width;
thisContext._penCanvas.height = thisContext.height;
thisContext._penContext.putImageData(penData, 0, 0);
thisContext._backdropDiv.style.width = thisContext.width;
thisContext._backdropDiv.style.height = thisContext.height;
setTimeout(thisContext._renderBackdrop);
})
};
thisContext._setCanvasSize(width, height);
window.addEventListener("resize", () => {
if (thisContext.fullScreen1) {
thisContext._setCanvasSize(window.innerWidth, window.innerHeight);
}
});
thisContext.bounds = () => {
return {left: thisContext.minX, right: thisContext.maxX, bottom: thisContext.minY, top: thisContext.maxY}
};
thisContext.randomX = function() {
if (arguments.length > 0) { throw new TypeError("randomX() requires no inputs."); }
return Woof.prototype.random(thisContext.minX, thisContext.maxX);
};
thisContext.randomY = function() {
if (arguments.length > 0) { throw new TypeError("randomY() requires no inputs."); }
return Woof.prototype.random(thisContext.minY, thisContext.maxY);
};
thisContext._renderBackdrop = () => {
var {size, type, url, color, repeat} = thisContext.backdrop;
thisContext._backdropDiv.style.background = (type === 'url' ) ? `url('${url}')` : color
thisContext._backdropDiv.style.backgroundRepeat = repeat;
thisContext._backdropDiv.style.backgroundSize = size;
};
thisContext.setBackdropURL = function(url){
if (typeof url != "string") { throw new TypeError("setBackDropURL(url) requires one string input."); }
thisContext.backdrop.url = url;
thisContext.backdrop.type = 'url'
var img = new BrowserImage()
img.onload = function(){
thisContext.ready(thisContext._renderBackdrop);
};
img.onerror = function(e){
var error = new Error()
error.type = "ImageLoadError"
error.url = url
throw error
}
img.src = url;
};
thisContext.setBackdropStyle = function(coverOrContain){
coverOrContain = coverOrContain.split(' ')
if(coverOrContain.length > 2){
throw Error("setBackdropStyle can take one or two arguments, separated by a space.")
}
//match each part of the input, maybe it looks like '50% 50px' or 'auto auto' or just '3em'
//regex translates to: the word cover on its own, the word contain on its own, at least one digit followed by 'em', at least on digit followed by 'px', at least one digit followed by '%'
let acceptableSizes = [/^cover$/,/^contain$/,/^\d+em$/,/^\d+px$/,/^\d+%$/,/^auto$/]
if(!coverOrContain.every(prop => acceptableSizes.some(each => prop.match(each)))){
throw Error("setBackdropStyle only understands sizes such as 5em, 50px, 50% and the keywords cover, contain, and auto")
}
thisContext.backdrop.size = coverOrContain.join(' ');
thisContext.ready(thisContext._renderBackdrop);
};
thisContext.setBackdropRepeat = function(repeatString){
let acceptableValues = ["repeat", "no-repeat", "repeat-x", "repeat-y","space","round"]
if(!acceptableValues.includes(repeatString)){
throw Error(`setBackdropRepeat can only understand one of the following: ${acceptableValues.join(', ')}`)
}
thisContext.backdrop.repeat = repeatString;
thisContext.ready(thisContext._renderBackdrop);
}
thisContext.setBackdropColor = function(color){
if (typeof color != "string") { throw new TypeError("setBackdropColor(color) takes one string input."); }
thisContext.backdrop.color = color;
thisContext.backdrop.type = 'color'
thisContext.ready(thisContext._renderBackdrop);
};
// WARNING - freeze is notoriously difficult to get right
// Any change you make to it will have unintended consequenses.
// Only change this code if absolutely neccesary and after rigerous testing.
thisContext.freezing = false // whether or not a freeze is currently in progress
thisContext.freeze = function() {
if (arguments.length > 0) { throw new TypeError("freeze() requires no inputs."); }
if (thisContext.freezing || thisContext.stopped) { return }
thisContext.freezing = true
thisContext.after(10, "miliseconds", () => {
thisContext.stopped = true
thisContext.freezing = false
});
};
thisContext.defrost = function() {
if (arguments.length > 0) { throw new TypeError("defrost() requires no inputs."); }
thisContext.stopped = false;
};
// the HTML canvas puts (0, 0) in the top-left corner of the screen
// the x-axis works as you'd expect, with x increasing as you move left-to-right
// the y-axis works counter-intuitively, decreasing as you move up, and increasing as you move down
// translateToCenter maps coordinates from the HTML canvas to the Scratch-world where (0,0) is in the center of the screen
thisContext.translateToCenter = (x, y) => {
return [(x - (thisContext.width / 2) + thisContext.cameraX) - thisContext._spriteCanvas.offsetLeft, (((thisContext.height / 2) - y) + thisContext.cameraY) + thisContext._spriteCanvas.offsetTop];
};
// translateToCanvas (the opposite of translateToCenter) maps coordinates from the Scratch-world to the HTML canvas world with (0,0) in the top-left
thisContext.translateToCanvas = (x, y) => {
return [(x + thisContext.maxX) - thisContext._spriteCanvas.offsetLeft, (thisContext.maxY - y) + thisContext._spriteCanvas.offsetTop];
};
// Below is where we handle mouse and keyboard events
// The strategy is:
// 1. Listen to all mouse and keyboard events
// 2. Keep global values updated, including which keys are down, and whether the mouse is down
// 3. Run events if we have them for that corresponding event
thisContext.mouseDown = false;
thisContext.mouseX = 0;
thisContext.mouseY = 0;
thisContext.pMouseX = 0;
thisContext.pMouseY = 0;
thisContext.mouseXSpeed = 0;
thisContext.mouseYSpeed = 0;
thisContext.keysDown = [];
//modify keysDown.includes() to not be case-sensitive and to accept more user input possibilities
thisContext.keysDown.oldIncludes = thisContext.keysDown.includes
thisContext.keysDown.includes = function(arg) {
var key = arg.toUpperCase()
if (key === "UP ARROW") {
key = "UP"
}
else if (key === "LEFT ARROW") {
key = "LEFT"
}
else if (key === "RIGHT ARROW") {
key = "RIGHT"
}
else if (key === "DOWN ARROW") {
key = "DOWN"
}
else if (key === "RETURN") {
key = "ENTER"
}
else if (key === "CONTROL") {
key = "CTRL"
}
else if (key === "OPTION") {
key = "ALT"
}
else if (key === "ESC") {
key = "ESCAPE"
}
else if (key === "SPACE BAR" || key === "SPACEBAR") {
key = "SPACE"
}
else if (key === "CAPS") {
key = "CAPS LOCK"
}
else if (key === "DEL" || key === "BACKSPACE") {
key = "DELETE"
}
else if (key === "CMD" || key === "WINDOWS" || key === "SEARCH") {
key = "COMMAND"
}
return this.oldIncludes(key)
}
thisContext.ready(() => {
thisContext._spriteCanvas.addEventListener("mousedown", (event) => {
thisContext.mouseDown = true;
[thisContext.mouseX, thisContext.mouseY] = thisContext.translateToCenter(event.clientX, event.clientY);
});
window.addEventListener("mouseup", (event) => {
thisContext.mouseDown = false;
[thisContext.mouseX, thisContext.mouseY] = thisContext.translateToCenter(event.clientX, event.clientY);
});
thisContext._spriteCanvas.addEventListener("touchstart", (event) => {
thisContext.mouseDown = true;
[thisContext.mouseX, thisContext.mouseY] = thisContext.translateToCenter(event.targetTouches[0].clientX, event.targetTouches[0].clientY);
});
thisContext._spriteCanvas.addEventListener("touchend", (event) => {
// for some reason touchend events are firing too quickly
// and are not getting picked up in 40 ms every-if's
// so thisContext setTimeout slows things down just enouch so
// touch events mirror mouse events
setTimeout(() => {thisContext.mouseDown = false;}, 0);
});
thisContext._spriteCanvas.addEventListener("mousemove", (event) => {
[thisContext.mouseX, thisContext.mouseY] = thisContext.translateToCenter(event.clientX, event.clientY);
});
thisContext._spriteCanvas.addEventListener("touchmove", event => {
[thisContext.mouseX, thisContext.mouseY] = thisContext.translateToCenter(event.targetTouches[0].clientX, event.targetTouches[0].clientY);
event.preventDefault();
});
document.body.addEventListener("keydown", event => {
var key = Woof.prototype.keyCodeToString(event.keyCode);
if (!thisContext.keysDown.includes(key)){
thisContext.keysDown.push(key);
}
});
document.body.addEventListener("keyup", event => {
var key = Woof.prototype.keyCodeToString(event.keyCode);
if (thisContext.keysDown.includes(key)){
thisContext.keysDown.splice(thisContext.keysDown.indexOf(key), 1);
}
});
thisContext._onMouseMoveHandler = event => {
var [mouseX, mouseY] = thisContext.translateToCenter(event.clientX, event.clientY);
thisContext._onMouseMoves.forEach((func) => {func(mouseX, mouseY)});
};
thisContext._spriteCanvas.addEventListener("mousemove", thisContext._onMouseMoveHandler);
thisContext._onMouseDownHandler = event => {
var [mouseX, mouseY] = thisContext.translateToCenter(event.clientX, event.clientY);
thisContext._onMouseDowns.forEach((func) => {func(mouseX, mouseY)});
};
thisContext._spriteCanvas.addEventListener("mousedown", thisContext._onMouseDownHandler);
thisContext._onMouseUpHandler = event => {
var [mouseX, mouseY] = thisContext.translateToCenter(event.clientX, event.clientY);
thisContext._onMouseUps.forEach((func) => {func(mouseX, mouseY)});
};
thisContext._spriteCanvas.addEventListener("mouseup", thisContext._onMouseUpHandler);
thisContext._onKeyDownHandler = event => {
var key = Woof.prototype.keyCodeToString(event.keyCode);
thisContext._onKeyDowns.forEach((func) => {func(key)});
};
document.body.addEventListener("keydown", thisContext._onKeyDownHandler);
thisContext._onKeyUpHandler = event => {
var key = Woof.prototype.keyCodeToString(event.keyCode);
thisContext._onKeyUps.forEach((func) => {func(key)});
};
document.body.addEventListener("keyup", thisContext._onKeyUpHandler);
})
// The following methods is where we keep track of user's events
thisContext._onMouseMoves = [];
thisContext.onMouseMove = func => {
if (typeof func != "function") { throw new TypeError("onMouseMove(function) requires one function input."); }
thisContext._onMouseMoves.push(func);
};
thisContext._onMouseDowns = [];
thisContext.onMouseDown = func => {
if (typeof func != "function") { throw new TypeError("onMouseDown(function) requires one function input."); }
thisContext._onMouseDowns.push(func);
};
thisContext._onMouseUps = [];
thisContext.onMouseUp = func => {
if (typeof func != "function") { throw new TypeError("onMouseUp(function) requires one function input."); }
thisContext._onMouseUps.push(func);
};
thisContext._onKeyDowns = [];
thisContext.onKeyDown = func => {
if (typeof func != "function") { throw new TypeError("onKeyDown(function) requires one function input."); }
thisContext._onKeyDowns.push(func);
};
thisContext._onKeyUps = [];
thisContext.onKeyUp = func => {
if (typeof func != "function") { throw new TypeError("onKeyUp(function) requires one function input."); }
thisContext._onKeyUps.push(func);
};
thisContext._everys = [];
thisContext.every = (time, units, func) => {
if (typeof func != "function" || (typeof time != "number" && typeof time != "function")) { throw new TypeError("every(time, units, function) requires a number/function, time unit and function input."); }
// if the user inputs something like () => random(1, 10) for the time parameter, re-evaluate the function every time it's run, and update the frequency
if (typeof time == "function") {
if (typeof time() != "number") { throw new TypeError("every(time, units, function) requires a time function that returns a number")}
// create a variable that will be used to store the value of the previous setTimeout()
var theFunction = function(timeoutValue) {
var ms = Woof.prototype.unitsToMiliseconds(time(), units);
func();
// if the previous setTimeout() value is in the ._everys array, remove it
if (timeoutValue && thisContext._everys.includes(timeoutValue)) {
thisContext._everys.splice(thisContext._everys.indexOf(timeoutValue), 1)
}
// use setTimeout() here instead of setInterval() because the interval has to be able to change
// pass an anonymous function so we can pass the argument lastTimeout to theFunction()
var lastTimeout = setTimeout(function() {
theFunction(lastTimeout)
}, ms)
thisContext._everys.push(lastTimeout);
}
theFunction()
}
else {
var milis = Woof.prototype.unitsToMiliseconds(time, units);
func();
thisContext._everys.push(setInterval(func, milis));
}
};
// thisContext.every = (time, units, func) => {
// var milis = Woof.prototype.unitsToMiliseconds(time, units);
// if (typeof func != "function" || typeof time != "number") { throw new TypeError("every(time, units, function) requires a number, unit and function input."); }
// func();
// thisContext._everys.push(setInterval(func, milis));
// };
thisContext.forever = (func) => {
if (typeof func != "function") { throw new TypeError("forever(function) requires one function input."); }
thisContext.repeatUntil(() => false, func);
};
thisContext.when = (condition, func) => {
if (typeof func != "function" || typeof condition != "function") { throw new TypeError("when(conditionFunction, function) requires two function inputs."); }
thisContext.forever(() => {
var cond;
try {
cond = condition();
} catch (e) {
console.error("Bad condition in when(conditionFunction, function): " + condition);
throw e;
}
if (cond) {
func();
};
});
};
// Woof repeats differ from a traditional JavaScript while or for-loop:
// 1. JavaScript loops are synchronous, and Woof loops are asynchronous
// 2. JavaScript loops are wicked fast, and Woof loops happen as fast as Woof forevers (about 30 times per second, in line with 30fps) which allow users to animate with them
thisContext._repeats = [];
thisContext.repeat = (times, func, after) => {
if (typeof func != "function" || typeof times != "number" || (after !== undefined && typeof after != "function")) { throw new TypeError("repeat(times, function, afterFunction) requires a number and a function, and optionally accepts an extra function."); }
thisContext._repeats.push(new Woof.prototype.Repeat(times, func, after));
};
thisContext.repeatUntil = (condition, func, after) => {
if (typeof func != "function" || typeof condition != "function" || (after !== undefined && typeof after != "function")) { throw new TypeError("repeatUntil(conditionFunction, function, afterFunction) requires two functions, and optionally accepts an extra function."); }
thisContext._repeats.push(new Woof.prototype.RepeatUntil(condition, func, after));
};
thisContext._runRepeats = () => {
thisContext._repeats.forEach(repeat => {
repeat.next();
});
thisContext._repeats = thisContext._repeats.filter(repeat => {return !repeat.done});
};
// thisContext._afters isn't read from as of commit 967, and only contains the IDs returned by setTimeout()
// These IDs could conceivably be used by clearTimeout() to cancel things in the future,
// but could get cleaned up (though this is low priority)
thisContext._afters = [];
thisContext.after = (time, units, func) => {
var milis = Woof.prototype.unitsToMiliseconds(time, units);
if (typeof func != "function" || typeof time != "number") { throw new TypeError("after(time, units, function) requires a number, unit and function input."); }
thisContext._afters.push(setTimeout(func, milis));
};
thisContext._renderSprites = () => {
thisContext._spriteContext.clearRect(0, 0, thisContext.width, thisContext.height);
thisContext._sprites.forEach((sprite) => {
sprite._render(thisContext._spriteContext);
});
};
thisContext.clearPen = function() {
if (arguments.length > 0) { throw new TypeError("clearPen() requires no inputs."); }
thisContext._penContext.clearRect(0, 0, thisContext.width, thisContext.height);
}
thisContext._calculateMouseSpeed = () => {
thisContext.mouseXSpeed = thisContext.mouseX - thisContext.pMouseX;
thisContext.mouseYSpeed = thisContext.mouseY - thisContext.pMouseY;
[thisContext.pMouseX, thisContext.pMouseY] = [thisContext.mouseX, thisContext.mouseY];
};
// The timer begins when Woof is loaded
thisContext.woofEpoch = new Date();
thisContext.timer = function(){
if (arguments.length > 0) { throw new TypeError("timer() requires no inputs." ); }
let date = new Date();
return ( date - thisContext.woofEpoch ) / 1000;
};
thisContext.resetTimer = function(){
if (arguments.length > 0) { throw new TypeError("resetTimer() requires no inputs."); }
thisContext.woofEpoch = new Date();
};
thisContext._render = () => {
thisContext._runRepeats(); // we need to run the repeats even if stopped because the defrost() code likely lives in a repeat
thisContext._calculateMouseSpeed();
thisContext.renderInterval = window.requestAnimationFrame(thisContext._render); // WARNING this line makes render recursive. Only call is once and it will continue to call itself ~30fps.
if (thisContext.stopped) { return; }
thisContext._renderSprites();
};
thisContext.ready(thisContext._render);
thisContext.collider = () => {
return new SAT.Polygon(new SAT.Vector(), [
new SAT.Vector(-thisContext.width/2, thisContext.height/2),
new SAT.Vector(-thisContext.width/2, -thisContext.height/2),
new SAT.Vector(thisContext.width/2, -thisContext.height/2),
new SAT.Vector(thisContext.width/2, thisContext.height/2)
]);
}
};
Woof.prototype.Sprite = function({project = undefined, x = 0, y = 0, angle = 0, rotationStyle = "ROTATE", showing = true, penColor = "black", penWidth = 1, penDown = false, showCollider = false, brightness = 100} = {}) {
if (!project) {
if (global) {
this.project = window;
} else {
throw new TypeError("When not in global mode, you must supply your {project: project} to each Sprite.")
}
} else {
this.project = project.global ? window : project;
}
this.project._sprites.push(this);
Object.defineProperty(this, 'x', {
get: function() {
return this.privateX;
},
set: function(value) {
if (typeof value != "number") { throw new TypeError("sprite.x can only be set to a number."); }
this.privateX = value;
this.project.ready(this.trackPen); // any change to x, is tracked for the pen
}
});
Object.defineProperty(this, 'y', {
get: function() {
return this.privateY;
},
set: function(value) {
if (typeof value != "number") { throw new TypeError("sprite.y can only be set to a number."); }
this.privateY = value;
this.project.ready(this.trackPen); // any change to y to tracked for the pen
}
});
this.privateX = x;
this.privateY = y;
this.angle = angle
this.rotationStyle = rotationStyle;
this.showing = showing;
this._penDown = penDown;
this.penColor = penColor;
this.penWidth = penWidth;
this.deleted = false;
this.showCollider = showCollider;
this.brightness = brightness;
this.toJSON = () => {
return {x: this.x, y: this.y, angle: this.angle, rotationStyle: this.rotationStyle, showing: this.showing, penDown: this._penDown, penColor: this.penColor, penWidth: this.penWidth, deleted: this.deleted};
};
[this.lastX, this.lastY] = [this.x, this.y];
this.trackPen = () => {
if (this._penDown) {
if(this.lastX != this.x || this.lastY != this.y) {
this.project._penContext.save();
this.project._penContext.beginPath();
this.project._penContext.moveTo(...this.project.translateToCanvas(this.lastX, this.lastY));
this.project._penContext.lineTo(...this.project.translateToCanvas(this.x, this.y));
this.project._penContext.lineCap = "round";
this.project._penContext.strokeStyle = this.penColor;
this.project._penContext.lineWidth = this.penWidth;
this.project._penContext.stroke();
this.project._penContext.restore();
}
}
[this.lastX, this.lastY] = [this.x, this.y];
};
// SAT collision for touching, works with rotated sprites
this.rotatedVector = function(x, y){
var rotatedX;
var rotatedY;
// If sprite is a line, offsets positioning by half the height as line is drawn from endpoints, not center
if (this.type == 'line') {
rotatedX = Math.cos(this.radians()) * (x - this.x) - Math.sin(this.radians()) * (y - this.y + this.height / 2) + this.x;
rotatedY = Math.sin(this.radians()) * (x - this.x) + Math.cos(this.radians()) * (y - this.y + this.height / 2) + this.y;
}
else {
rotatedX = Math.cos(this.radians()) * (x - this.x) - Math.sin(this.radians()) * (y - this.y) + this.x;
rotatedY = Math.sin(this.radians()) * (x - this.x) + Math.cos(this.radians()) * (y - this.y) + this.y;
}
return new SAT.Vector(rotatedX, rotatedY);
}
// Makes collider vector vertices relative to the point 'pos'
this.translatedVector = function(pos, v){
return new SAT.Vector(v.x - pos.x, v.y - pos.y);
}
// Creates collider from vector vertices
this.collider = function() {
// If sprite is a circle, create circle collider
if (this.type == "circle") {
return new SAT.Circle(new SAT.Vector(this.x,this.y), this.radius);
}
// If sprite is a polygon, create polygon collider
else if (this.type == "polygon") {
var pos
if (this.rotationStyle == "ROTATE") {
pos = this.rotatedVector(this.x, this.y)
} else {
pos = new SAT.Vector(this.x, this.y)
}
var vs = [new SAT.Vector(this.length*1, this.length*0)];
for (var i = 1; i < this.sides; i++) {
vs.push(new SAT.Vector(this.length*Math.cos(i*(2*Math.PI/this.sides)),this.length*Math.sin(i*(2*Math.PI/this.sides))));
}
if (this.rotationStyle == "ROTATE") {
return new SAT.Polygon(pos, vs).rotate(this.radians());
} else if (this.rotationStyle == "ROTATE LEFT RIGHT" && ((this.angle%360 >= 90 && this.angle%360 < 270) ||
(this.angle%360 <= -90 && this.angle%360 > -270))) { // this math should match code in this._render
return new SAT.Polygon(pos, vs).rotate(Math.PI);
} else {
return new SAT.Polygon(pos, vs);
}
}
// Otherwise, create 4-sided collider around sprite
else {
if (this.rotationStyle == "ROTATE") {
var pos = this.rotatedVector(this.x - this.width / 2, this.y - this.height / 2)
var v1 = new SAT.Vector(0, 0)
var v2 = this.translatedVector(pos, this.rotatedVector(this.x + this.width / 2, this.y - this.height / 2))
var v3 = this.translatedVector(pos, this.rotatedVector(this.x + this.width / 2, this.y + this.height / 2))
var v4 = this.translatedVector(pos, this.rotatedVector(this.x - this.width / 2, this.y + this.height / 2))
return new SAT.Polygon(pos, [v1, v2, v3, v4])
} else { // rotation style is LEFT RIGHT or NO ROTATE, which should have non-rotated collider
var pos = new SAT.Vector(this.x - this.width/2, this.y - this.height/2)
var v1 = new SAT.Vector(0,0)
var v2 = this.translatedVector(pos, new SAT.Vector(this.x + this.width / 2, this.y - this.height / 2))
var v3 = this.translatedVector(pos, new SAT.Vector(this.x + this.width / 2, this.y + this.height / 2))
var v4 = this.translatedVector(pos, new SAT.Vector(this.x - this.width / 2, this.y + this.height / 2))
return new SAT.Polygon(pos, [v1, v2, v3, v4])
}
}
}
// for debugging purposes, this function displays the collider on the screen according to type of sprite
this._renderCollider = function(context){
var collider = this.collider()
context.save();
context.beginPath();
if (collider.constructor.name == "Circle") {
context.arc(...this.project.translateToCanvas(collider.pos.x, collider.pos.y), collider.r, 0, 2*Math.PI);
} else {
context.moveTo(...this.project.translateToCanvas(collider.calcPoints[0].x + collider.pos.x, collider.calcPoints[0].y + collider.pos.y));
for (var i = 1; i < this.collider().edges.length; i++) {
context.lineTo(...this.project.translateToCanvas(collider.calcPoints[i].x + collider.pos.x, collider.calcPoints[i].y + collider.pos.y));
}
context.lineTo(...this.project.translateToCanvas(collider.calcPoints[0].x + collider.pos.x, collider.calcPoints[0].y + collider.pos.y));
}
context.strokeStyle = "green";
context.lineWidth = 4;
context.stroke();
context.restore();
}
this._render = function(context) {
if (this.showing && !this.deleted && this.overlap(this.project.bounds())) {
if (this.showCollider) { this._renderCollider(context); }
context.save();
context.translate(Math.round(this.canvasX()), Math.round(this.canvasY()));
context.globalAlpha = this.brightness / 100;
if (this.rotationStyle == "ROTATE") {
context.rotate(-this.radians());
} else if (this.rotationStyle == "NO ROTATE"){
// no rotate
} else if (this.rotationStyle == "ROTATE LEFT RIGHT"){
if ((this.angle%360 >= 90 && this.angle%360 < 270) ||
(this.angle%360 <= -90 && this.angle%360 > -270)){
context.scale(-1, 1);
} else if ((this.angle%360 >=0 && this.angle%360 < 90) ||
(this.angle%360 <= 360 && this.angle%360 >=270) ||
(this.angle%360 <= 0 && this.angle%360 > -90) ||
(this.angle%360 >= -360 && this.angle%360 <= -270)){
// no rotate
}
}
this.render(context);
context.restore();
}
};
this.distanceTo = function distanceTo(xGiven, yGiven){
if (arguments.length === 1) {
if (typeof xGiven == "object"){
var x = this.x - xGiven.x;
var y = this.y - xGiven.y;
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
} else {
throw new TypeError("distanceTo(sprite) requires one sprite input.")
}
} else if (typeof xGiven != "number" || typeof yGiven != "number") {
throw new TypeError("distanceTo(x,y) requires two number inputs.");
} else {
var x = this.x - xGiven;
var y = this.y - yGiven;
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
};
this.move = function(steps = 1){
if (typeof steps != "number") { throw new TypeError("move(steps) requires one number input."); }
// we modify privateX and privateY here before tracking pen so that the pen thinks they changed at the same time
this.privateX += steps * Math.cos(this.radians());
this.privateY += steps * Math.sin(this.radians());
this.project.ready(this.trackPen);
};
this.setRotationStyle = style => {
if (style == "ROTATE"){
this.rotationStyle = "ROTATE";
} else if (style == "NO ROTATE") {
this.rotationStyle = "NO ROTATE";
} else if (style == "ROTATE LEFT RIGHT") {
this.rotationStyle = "ROTATE LEFT RIGHT";
} else {
throw TypeError("Unrecognized rotation style: " + style + ". setRotationStyle only accepts 'ROTATE', 'NO ROTATE', and 'ROTATE LEFT RIGHT'");
}
};
this.radians = () => {
return this.angle * Math.PI / 180;
};
this.canvasX = () => {
return (this.x - this.project.cameraX) + (this.project.width / 2) ;
};
this.canvasY = () => {
return (this.project.height / 2) - (this.y - this.project.cameraY);
};
this.bounds = () => {
var halfWidth = (this.width / 2);
var halfHeight = (this.height / 2);
var left = this.x - halfWidth;
var right = this.x + halfWidth;
var bottom = this.y - halfHeight;
var top = this.y + halfHeight;
return {left: left, right: right, top: top, bottom: bottom};
};
this.collisionCanvas = document.createElement('canvas');
this.collisionContext = this.collisionCanvas.getContext('2d');
this.touching = (sprite, precise) => {
if (!(typeof sprite == "object")) { throw new TypeError("touching(sprite) requires one sprite input."); }
if (this.deleted || !this.showing) { return false; }
if (sprite.deleted || !sprite.showing) { return false; }
if (!detectCollision(this.collider(), sprite.collider())) { return false; }
if (!precise) { return true; }
// this code scans the pixels of both sprites to see if they are touching
// it's very slow so we turn it off by default
var r1 = this.bounds();
var r2 = sprite.bounds();
var left = Math.min(r1.left, r2.left);
var top = Math.max(r1.top, r2.top);
var right = Math.max(r1.right, r2.right);
var bottom = Math.min(r1.bottom, r2.bottom);
this.collisionCanvas.width = this.project.width;
this.collisionCanvas.height = this.project.height;
this._render(this.collisionContext);
this.collisionContext.globalCompositeOperation = 'source-in';
sprite._render(this.collisionContext);
var [canvasLeft, canvasTop] = this.project.translateToCanvas(left, top)
try {
var data = this.collisionContext.getImageData(canvasLeft, canvasTop, right - left, top - bottom).data;
} catch (e) {
if (e instanceof DOMException) {
console.warn("You have an image at an untrusted URL. Consider uploading to Imgur and using https.")
// bounds are overlapping and we can't get canvas data, so return true
return true;
}
}
var length = (right - left) * (top - bottom) * 4;
for (var j = 0; j < length; j += 4) {
if (data[j + 3]) {
return true;
}
}
return false;
};
this.overlap = ({left, right, top, bottom}) => {
if (this.collider().constructor.name == "Circle") {
return SAT.testPolygonCircle(this.project.collider(), this.collider(), new SAT.Response())
}
return SAT.testPolygonPolygon(this.project.collider(), this.collider(), new SAT.Response())
}
this.over = (x, y) => {
if (typeof x != "number" || typeof y != "number") { throw new TypeError("over(x, y) requires two number inputs."); }
if (this.deleted || !this.showing) { return false; }
var collider = this.collider();
// If shape is an oval, find point in oval. SAT.js does not include this in library.
// This is more exact than a collider polygon drawn around it.
if (this.type == "oval") {
return Math.pow(x-this.x, 2) / Math.pow(this.width/2, 2) + Math.pow(y-this.y, 2) / Math.pow(this.height/2, 2) <= 1;
}
// If collider is a circle, find point in circle
else if (collider.constructor.name == "Circle") {
return SAT.pointInCircle(new SAT.Vector(x,y), this.collider());
}
// Otherwise look for point in polygon
else {
return SAT.pointInPolygon(new SAT.Vector(x,y), this.collider())
}
};
Object.defineProperty(this, 'mouseOver', {
get: function() {
if (this.deleted || !this.showing) { return false; }
return this.over(this.project.mouseX, this.project.mouseY);
}
});
Object.defineProperty(this, 'mouseDown', {
get: function() {
if (this.deleted || !this.showing) { return false; }
return this.mouseOver && this.project.mouseDown;
}
});
this.turnLeft = (degrees = 1) => {
if (typeof degrees != "number") { throw new TypeError("turnLeft(degrees) requires one number input."); }
this.angle += degrees;
};
this.turnRight = (degrees = 1) => {
if (typeof degrees != "number") { throw new TypeError("turnRight(degrees) requires one number input."); }
this.angle -= degrees;
};
this.sendToBack = function() {
if (arguments.length > 0) { throw new TypeError("sendToBack() requires no inputs."); }
var _sprites = this.project._sprites;
_sprites.splice(0, 0, _sprites.splice(_sprites.indexOf(this), 1)[0]);
};
this.sendToFront = function() {
if (arguments.length > 0) { throw new TypeError("sendToFront() requires no inputs."); }
var _sprites = this.project._sprites;