-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjumpingjan.js
2394 lines (1984 loc) · 72.3 KB
/
jumpingjan.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
var sketchProc=function(processingInstance){ with (processingInstance){
size(400, 600);
frameRate(60);
/* /* ^^^^^^^^^^^^^^^^^^^^^ BEGIN PROGRAM CODE ^^^^^^^^^^^^^^^^^^^^^ \*/
angleMode = 'radians';
var KEYS = [];
var GRAVITY = new PVector(0,0.6); // TODO remove
textAlign(CENTER, CENTER);
/* --------------------- Button Object --------------------- \*/
var Button = function(x, y, txt) {
this.position = new PVector(x, y); // Center of the button
this.text = txt;
this.width = 80;
this.height = 30;
};
Button.prototype.mouseIsOnMe = function() {
return (mouseX > this.getCornerPositionX('upper_left') && mouseX < this.getCornerPositionX('upper_right')) && (mouseY > this.getCornerPositionY('upper_left') && mouseY < this.getCornerPositionY('lower_right'));
};
/* Expects a string parameter of either 'upper_left', 'upper_right', 'lower_left', 'lower_right' */
Button.prototype.getCornerPositionX = function(corner) {
switch(corner)
{
case "upper_left":
return this.position.x - this.width/2;
case "upper_right":
return this.position.x + this.width/2;
case "lower_left":
return this.position.x - this.width/2;
case "lower_right":
return this.position.x + this.width/2;
default:
return 0; // Error Case
}
};
/* Expects a string parameter of either 'upper_left', 'upper_right', 'lower_left', 'lower_right' */
Button.prototype.getCornerPositionY = function(corner) {
switch(corner)
{
case "upper_left":
return this.position.y - this.height/2;
case "upper_right":
return this.position.y - this.height/2;
case "lower_left":
return this.position.y + this.height/2;
case "lower_right":
return this.position.y + this.height/2;
default:
return 0; // Error Case
}
};
Button.prototype.getVertices = function() {
return [{x:this.getCornerPositionX("upper_left") , y:this.getCornerPositionY("upper_left") },
{x:this.getCornerPositionX("upper_right"), y:this.getCornerPositionY("upper_right")},
{x:this.getCornerPositionX("lower_right"), y:this.getCornerPositionY("lower_right")},
{x:this.getCornerPositionX("lower_left") , y:this.getCornerPositionY("lower_left") }];
};
Button.prototype.draw = function() {
if (this.mouseIsOnMe()) {
fill(200, 200, 200);
cursor(HAND);
}
else {
noFill();
}
var vertices = this.getVertices();
beginShape();
for (var i = 0; i < vertices.length;i++) {
vertex(vertices[i].x, vertices[i].y);
}
endShape(CLOSE);
fill(0);
textSize(16);
text(this.text, this.position.x, this.position.y);
};
/* --------------------- END Button Object --------------------- \*/
var ArrowButton = function(x, y, txt, dir) {
Button.apply(this, arguments);
this.width = 50;
this.height = 25;
this.direction = ((dir || 1) > 0) ? 1 : -1;
};
ArrowButton.prototype = Object.create(Button.prototype);
ArrowButton.prototype.constructor = ArrowButton;
ArrowButton.prototype.getVertices = function() {
var stemSize_h = 0.6;
var stemSize_w = 0.6;
var x1 = -(this.width/2);
var x2 = (stemSize_w * this.width) - (this.width/2);
var x3 = (this.width/2);
var y1 = -1*((stemSize_h * this.height)/2);
var y2 = -1*(this.height/2);
var y3 = 0;
return [{x:this.position.x+(this.direction*x1), y:this.position.y-y1},
{x:this.position.x+(this.direction*x2), y:this.position.y-y1},
{x:this.position.x+(this.direction*x2), y:this.position.y-y2},
{x:this.position.x+(this.direction*x3), y:this.position.y-y3}, // ArrowHead
{x:this.position.x+(this.direction*x2), y:this.position.y+y2},
{x:this.position.x+(this.direction*x2), y:this.position.y+y1},
{x:this.position.x+(this.direction*x1), y:this.position.y+y1}];
};
/* --------------------- Color Selector Object --------------------- \*/
var ColorSelectorBox = function(x, y, col) {
this.position = new PVector(x, y);
this.size = 10;
this.selected = false;
this.color = (col === 'r') ? color(185, 0, 0) :
(col === 'b') ? color(80, 0, 255) :
(col === 'g') ? color(150, 200, 20) :
(col === 'y') ? color(236, 166, 35) :
color(185, 0, 0);
};
ColorSelectorBox.prototype.setSelected = function(newValue) {
this.selected = newValue;
};
ColorSelectorBox.prototype.mouseIsOnMe = function() {
return (mouseX > this.position.x-this.size/2 && mouseX < this.position.x+this.size/2 && (mouseY > this.position.y-this.size/2 && mouseY < this.position.y+this.size/2));
};
ColorSelectorBox.prototype.draw = function() {
strokeWeight( (this.selected) ? 3 : 1 );
stroke(0);
if (this.mouseIsOnMe()) {
cursor(HAND);
}
fill(this.color);
rect(this.position.x-this.size/2, this.position.y-this.size/2, this.size, this.size);
strokeWeight(1);
};
var ColorSelector = function(x, y) {
this.position = new PVector(x, y);
this.items = [];
this.selectedIndex = 0;
this.PADDING = 5;
this.ITEM_SIZE = 10;
};
ColorSelector.prototype.getWidth = function() {
return this.items.length*(this.PADDING+this.ITEM_SIZE);
};
ColorSelector.prototype.getNextItemPosition = function() {
return {x:this.position.x+(this.PADDING + this.ITEM_SIZE/2) + this.getWidth(), y:this.position.y};
};
ColorSelector.prototype.add = function(newItem_colorCode) {
var pos = this.getNextItemPosition();
this.items.push(new ColorSelectorBox(pos.x, pos.y, newItem_colorCode));
};
ColorSelector.prototype.setSelectedIndex = function(itemNumber) {
if (itemNumber < this.items.length) {
this.items[this.selectedIndex].setSelected(false);
this.selectedIndex = itemNumber;
this.items[itemNumber].setSelected(true);
}
};
ColorSelector.prototype.mouseIsOnMe = function() {
return (mouseX > this.position.x && mouseX < this.position.x+this.getWidth() && (mouseY > this.position.y-20/2 && mouseY < this.position.y+20/2));
};
ColorSelector.prototype.draw = function() {
for (var i = 0; i < this.items.length; i++) {
this.items[i].draw();
}
};
var Sun = function() {this.ang = 0; this.maxRayLength = 20;};
Sun.prototype.drawRay = function(len, ang) {
pushMatrix();
rotate(ang);
var a = len;//random(30);
stroke(217, 222, 69);
//point((x < 130) ? x++ : , 0);
line(100, 0, 100+a, 0);
popMatrix();
};
Sun.prototype.draw = function() {
strokeWeight(2);
fill(217, 222, 69);
pushMatrix();
translate(400, 0);
rotate(this.ang+=0.01);
var cnt = 5;
var curL = 0;
for (var i = 0; i < TWO_PI; i += PI/128) {
if (curL > cnt) {
curL = -cnt;
}
else if (curL === 0) {
//curL++;
}
var len = (++curL < 0) ? -curL : curL;
this.drawRay(this.maxRayLength/len, i);
}
ellipse(0, 0, 200, 200);
popMatrix();
};
/* --------------------- GAME Variables --------------------- \*/
var TILE_SIZE = 40;
var NUM_TILES = 400/TILE_SIZE;
var SETTINGS_PLAYER_COLOR = 0;
/* --------------------- GAME CLASSES --------------------- \*/
var Coord = function(x, y){
this.x = x;
this.y = y;
};
Coord.prototype.compare = function(other) {
if (this.x === other.x && this.y === other.y) {
return 0;
}
else if (this.x > other.x && this.y < other.y) {
return 1;
}
else if (this.x < other.x && this.y > other.y) {
return -1;
}
else {
return -100;
}
};
var splitPoints = function(pts) {
var p2 = [];
for (var i = 0; i < pts.length - 1; i++) {
p2.push(new PVector(pts[i].x, pts[i].y));
p2.push(new PVector((pts[i].x + pts[i+1].x)/2, (pts[i].y +
pts[i+1].y)/2));
}
p2.push(new PVector(pts[i].x, pts[i].y));
p2.push(new PVector((pts[0].x + pts[i].x)/2, (pts[0].y +
pts[i].y)/2));
return p2;
};
var average = function(p2, pts) {
for (var i = 0; i < p2.length - 1; i++) {
var x = (p2[i].x + p2[i+1].x)/2;
var y = (p2[i].y + p2[i+1].y)/2;
p2[i].set(x, y);
}
var x = (p2[i].x + pts[0].x)/2;
var y = (p2[i].y + pts[0].y)/2;
pts.splice(0, pts.length);
for (i = 0; i < p2.length; i++) {
pts.push(new PVector(p2[i].x, p2[i].y));
}
};
var subdivide = function(pts) {
var intermed = splitPoints(pts);
average(intermed, pts);
};
var drawShapeFromVertices = function(vertices, notClosed) {
notClosed |= false;
beginShape();
for (var i = 0; i < vertices.length; i++) {
vertex(vertices[i].x, vertices[i].y);
}
if (!notClosed) {
vertex(vertices[0].x, vertices[0].y);
}
endShape();
};
// Rotates an (x, y) p1 around an (x, y) p2 by n radians
var rotateAroundPoint = function(p1, p2, n) {
var translatedVertex = new PVector(p1.x-p2.x, p1.y-p2.y);
var cosn = cos(n);
var sinn = sin(n);
var rotatedVertex = new PVector(translatedVertex.x*cosn - translatedVertex.y*sinn, translatedVertex.y*cosn + translatedVertex.x*sinn);
p1.x = rotatedVertex.x+p2.x;
p1.y = rotatedVertex.y+p2.y;
};
/***************************************************************************
MOVER BASE CLASS
***************************************************************************/
var Mover = function(x, y, tm) {
this.position = new PVector(x, y);
this.nextPosition = new PVector(0, 0);
this.velocity = new PVector(0, 0);
this.nextVelocity = new PVector(0, 0);
this.acceleration = new PVector(0, 0);
this.tm = tm;
this.mass = 1;
this.height = 0;
this.width = 0;
this.size = TILE_SIZE;
this.movementSpeed = 0;
this.maxSpeed = 0;
this.restitution = 0;
this.isAffectedByGravity = false;
this.isAffectedByCollisions = false;
this.onGround = false;
this.wasOnGround = false;
};
Mover.prototype.applyForce = function(force) {
this.acceleration.add(PVector.div(force, this.mass));
};
Mover.prototype.getWidth = function() {return this.width*(this.size/400);};
Mover.prototype.getHeight = function() {return this.height*(this.size/400);};
Mover.prototype.getScaleFactor = function() {return this.size/400;};
Mover.prototype.getBoundingBoxEdges = function() {
return {top:this.position.y-this.getHeight()/2, bottom:this.position.y+this.getHeight()/2, left: this.position.x-this.getWidth()/2, right:this.position.x+this.getWidth()/2};
};
Mover.prototype.checkCollision = function(mover) {
var myEdges = this.getBoundingBoxEdges();
var otherEdges = mover.getBoundingBoxEdges();
var scaleFactor = this.getScaleFactor();
var l1 = new Coord(myEdges.left*scaleFactor, myEdges.top*scaleFactor);
var r1 = new Coord(myEdges.right*scaleFactor, myEdges.bottom*scaleFactor);
//l1.x += this.nextPosition.x-this.position.x;
//r1.x += this.nextPosition.x-this.position.x;
//l1.y += this.nextPosition.y-this.position.y;
//r1.y += this.nextPosition.y-this.position.y;
var l2 = new Coord(otherEdges.left*scaleFactor, otherEdges.top*scaleFactor);
var r2 = new Coord(otherEdges.right*scaleFactor, otherEdges.bottom*scaleFactor);
if (l1.compare(r2) === 1 || l2.compare(r1) === 1) {
return false;
}
if (l1.compare(r2) === -1 || l2.compare(r1) === -1) {
return false;
}
if ((r1.x <= l2.x && r1.y <= l2.y) || (l1.x >= r2.x && l1.y >= r2.y)) {
return false;
}
return true;
};
/*Mover.prototype.checkStandingOn = function(mover) {
var myEdges = this.getBoundingBoxEdges();
var otherEdges = mover.getBoundingBoxEdges();
var scaleFactor = this.getScaleFactor();
var l1 = new Coord(myEdges.left*scaleFactor, myEdges.top*scaleFactor);
var r1 = new Coord(myEdges.right*scaleFactor, myEdges.bottom*scaleFactor);
var l2 = new Coord(otherEdges.left*scaleFactor, otherEdges.top*scaleFactor);
var r2 = new Coord(otherEdges.right*scaleFactor, otherEdges.bottom*scaleFactor);
var dy = 1;
if (abs(r1.y - l2.y) > dy) {
return false;
}
if (l1.compare(r2) === -1 || l2.compare(r1) === -1) {
return false;
}
return true;
};
Mover.prototype.resolveCollision = function(A, B, norm) {
// Calculate relative velocity
var rv = PVector.sub(B.velocity, A.velocity);
// Calculate relative velocity in terms of the normal direction
var velAlongNormal = rv.dot(norm);
// Do not resolve if velocities are separating
if(velAlongNormal > 0) {
return;
}
// Calculate restitution
var e = min( A.restitution, B.restitution);
// Calculate impulse scalar
var j = -(1 + e) * velAlongNormal;
j /= 1 / A.mass + 1 / B.mass;
// Apply impulse
var impulse = PVector.mult(normal, j);
A.velocity.sub(PVector.div(impulse, A.mass));
B.velocity.add(PVector.div(impulse, B.mass));
};
*/
Mover.prototype.setPreCollisionVariables = function() {
this.wasOnGround = this.onGround;
this.onGround = false;
};
Mover.prototype.handleCollision = function() {
// AABB containing current position and next position
var minPos = new PVector(0, 0);
minPos.x = min(this.position.x, this.nextPosition.x);
minPos.y = min(this.position.y, this.nextPosition.y);
var maxPos = new PVector(0, 0);
maxPos.x = max(this.position.x, this.nextPosition.x);
maxPos.y = max(this.position.y, this.nextPosition.y);
// Extend AABB to bound entire object - position is from the center
minPos.sub(new PVector(this.getWidth()/2, this.getHeight()/2));
maxPos.add(new PVector(this.getWidth()/2, this.getHeight()/2));
// Debug Collisions
/*fill(255, 0, 0);
ellipse(minPos.x, minPos.y, 20, 20);
fill(0, 0, 255);
ellipse(maxPos.x, maxPos.y, 20, 20);*/
// Extend AABB a bit more - helps when player is very close to boundary of a cell
// Note: not sure if need this or not
//minPos.add(5, 5);
//maxPos.sub(5, 5);
this.setPreCollisionVariables();
this.tm.checkForCollisions(minPos, maxPos, this);
};
Mover.prototype.resolveLanding = function() {};
Mover.prototype.CollisionResolution = function(normal, distanceToPlane) {
var separation = max(distanceToPlane, 0);
var penetration = min(distanceToPlane, 0);
var dt = 60; // TODO. maybe this should be something
var normal_velocity = this.nextVelocity.dot(normal) + separation/dt;
this.nextPosition.sub(PVector.mult(normal, penetration/dt));
if ( normal_velocity < -0.1 ) {
// remove normal velocity
var temp = PVector.mult(normal, normal_velocity);
this.nextVelocity.sub(temp);
if (normal.y < 0) {
this.onGround = true;
if (!this.wasOnGround) {
this.resolveLanding();
}
}
}
};
Mover.prototype.update = function() {
if (this.isAffectedByGravity) {
this.applyForce(GRAVITY);
}
this.nextVelocity = PVector.add(this.velocity, this.acceleration); // TODO maybe don't need this
var nextVelocityYDirection = (this.nextVelocity.y > 0) ? 1 : -1;
// Clamp max speed
this.nextVelocity.y = nextVelocityYDirection * min(this.maxSpeed, abs(this.nextVelocity.y));
//this.nextVelocity.x = nextVelocityYDirection * min(this.maxSpeed, abs(this.nextVelocity.x));
this.nextPosition.add(PVector.add(this.position, this.nextVelocity));
if (this.isAffectedByCollisions) {
// TODO handle collision
/*if (this.nextPosition.y >= 400-TILE_SIZE-this.getHeight()/2 + 1) { // on Ground
this.onGround = true;
if (this.nextVelocity.y > 0) { // moving toward ground
this.nextVelocity.set(this.nextVelocity.x, 0);
this.nextPosition.y = this.position.y;
}
}
else {
this.onGround = false;
}*/
this.handleCollision();
}
this.position.set(this.nextPosition);
this.velocity.set(this.nextVelocity);
this.acceleration.mult(0);
this.nextPosition.set(0, 0);
this.nextVelocity.y = 0;
};
var LeverTile = function(tile) {
var pos = Tilemap.getCoordinateFromTile(tile);
Mover.apply(this, [pos.x, pos.y]);
this.active = false;
this.affectedObjects = [];
};
LeverTile.prototype = Object.create(Mover.prototype);
LeverTile.prototype.constructor = LeverTile;
LeverTile.prototype.toggle = function() {
this.active = !this.active;
for (var i = 0; i < this.affectedObjects.length; i++) {
this.affectedObjects[i].effectCallback(this.active);
}
};
LeverTile.prototype.addAffectedObject = function(affected) {
this.affectedObjects.push(affected);
};
LeverTile.prototype.draw = function() {
var leverColor = color(77, 64, 32);
strokeWeight(8);
stroke(leverColor);
if (this.active) {
line(this.position.x, this.position.y+TILE_SIZE*0.4, this.position.x-TILE_SIZE*0.4, this.position.y-TILE_SIZE/4);
}
else {
line(this.position.x, this.position.y+TILE_SIZE*0.4, this.position.x+TILE_SIZE*0.4, this.position.y-TILE_SIZE/4);
}
strokeWeight(1);
fill(leverColor);
arc(this.position.x, this.position.y+TILE_SIZE/2, TILE_SIZE*0.8, TILE_SIZE*0.6, PI, TWO_PI);
};
/***************************************************************************
PLATFORM CLASS
***************************************************************************/
var Platform = function(x, y, width) {
Mover.apply(this, [x, y, -1]);
this.width = width;
this.height = TILE_SIZE;
this.mass = 1000;
this.size = 400;
this.isAffectedByGravity = false;
this.isAffectedByCollisions = false;
};
Platform.prototype = Object.create(Mover.prototype);
Platform.prototype.constructor = Platform;
Platform.prototype.draw = function() {
var left = this.position.x - this.width/2;
var right = this.position.x + this.width/2;
var top = this.position.y - this.height/2;
var bottom = this.position.y + this.height/2;
stroke(218, 200, 198);
fill(218, 200, 198);
rect(left, top, this.width, this.height);
};
var SandBlock = function(x, y, width, grassFlag) {
Platform.apply(this, arguments);
this.hasGrass = grassFlag;
};
SandBlock.prototype = Object.create(Platform.prototype);
SandBlock.prototype.constructor = SandBlock;
SandBlock.sandSpots = [];
SandBlock.img = 0;
SandBlock.img_g = 0;
SandBlock.prototype.draw = function() {
if (SandBlock.img !== 0 && SandBlock.img_g !== 0) {
if (this.hasGrass) {
image(SandBlock.img_g, this.position.x-this.width/2, this.position.y-this.height/2, TILE_SIZE, TILE_SIZE);
}
else {
image(SandBlock.img, this.position.x-this.width/2, this.position.y-this.height/2, TILE_SIZE, TILE_SIZE);
}
}
else {
fill(212, 175, 74);
strokeWeight(1);
noStroke();
rect(this.position.x-this.width/2, this.position.y-this.height/2, TILE_SIZE, TILE_SIZE);
for (var i = 0; i < 280; i++) {
stroke(89, 69, 13);
if (SandBlock.sandSpots.length < 280) {
SandBlock.sandSpots.push([random(40), random(40)]);
}
point(SandBlock.sandSpots[i][0]+this.position.x-this.width/2, SandBlock.sandSpots[i][1]+this.position.y-this.height/2);
}
if (this.hasGrass) {
fill(67, 143, 37);
noStroke();
var x = this.position.x-this.width/2;
var y = this.position.y-this.height/2;
ellipse(x+TILE_SIZE*0.23, y+5, TILE_SIZE/2, TILE_SIZE/2);
ellipse(x+TILE_SIZE*0.5, y+5, TILE_SIZE/2, TILE_SIZE/2);
ellipse(x+TILE_SIZE*0.77, y+5, TILE_SIZE/2, TILE_SIZE/2);
SandBlock.img_g = get(this.position.x-this.width/2, this.position.y-this.height/2, TILE_SIZE, TILE_SIZE);
}
else {
SandBlock.img = get(this.position.x-this.width/2, this.position.y-this.height/2, TILE_SIZE, TILE_SIZE);
}
}
};
var HalfGroundBlock = function(x, y, width) {Platform.apply(this, arguments);};
HalfGroundBlock.prototype = Object.create(Platform.prototype);
HalfGroundBlock.prototype.constructor = HalfGroundBlock;
HalfGroundBlock.prototype.draw = function() {
var corner = new PVector(this.position.x-this.width/2, this.position.y-this.height/2);
fill(154, 51, 19);
noStroke();
rect(corner.x, corner.y, TILE_SIZE, TILE_SIZE);
fill(67, 143, 37);
ellipse(corner.x+TILE_SIZE*0.23, corner.y+5, TILE_SIZE/2, TILE_SIZE/2);
ellipse(corner.x+TILE_SIZE*0.5, corner.y+5, TILE_SIZE/2, TILE_SIZE/2);
ellipse(corner.x+TILE_SIZE*0.77, corner.y+5, TILE_SIZE/2, TILE_SIZE/2);
};
var MidGroundBlock = function(x, y, width, hasGrass) {Platform.apply(this, arguments);this.hasGrass=hasGrass};
MidGroundBlock.prototype = Object.create(Platform.prototype);
MidGroundBlock.prototype.constructor = MidGroundBlock;
MidGroundBlock.prototype.draw = function() {
var corner = new PVector(this.position.x-this.width/2, this.position.y-this.height/2);
fill(154, 51, 19);
noStroke();
rect(corner.x, corner.y, TILE_SIZE, TILE_SIZE);
fill(78, 22, 11);
rect(corner.x, corner.y+TILE_SIZE/2, TILE_SIZE, TILE_SIZE/2);
ellipse(corner.x+TILE_SIZE*0.2, corner.y+TILE_SIZE*0.6, TILE_SIZE/3, TILE_SIZE/3);
ellipse(corner.x+TILE_SIZE*0.5, corner.y+TILE_SIZE*0.6, TILE_SIZE/3, TILE_SIZE/3);
ellipse(corner.x+TILE_SIZE*0.8, corner.y+TILE_SIZE*0.6, TILE_SIZE/3, TILE_SIZE/3);
if (this.hasGrass) {
fill(67, 143, 37);
noStroke();
rect(corner.x, corner.y, TILE_SIZE, TILE_SIZE*0.1);
}
};
var LowerGroundBlock = function(x, y, width) {Platform.apply(this, arguments);};
LowerGroundBlock.prototype = Object.create(Platform.prototype);
LowerGroundBlock.prototype.constructor = LowerGroundBlock;
LowerGroundBlock.prototype.draw = function() {
var corner = new PVector(this.position.x-this.width/2, this.position.y-this.height/2);
fill(78, 22, 11);
noStroke();
rect(corner.x, corner.y, TILE_SIZE, TILE_SIZE);
};
/**************************************************************************/
var Flatform = function(x, y, orientation) {
Platform.apply(this, [x, y, TILE_SIZE]);
this.width = TILE_SIZE;
this.height = TILE_SIZE/5;
this.orientation = orientation;
this.color = color(80, 140, 200);
};
Flatform.getPositionFromTile = function(tile, orientation) {
var x = tile.col*TILE_SIZE+ TILE_SIZE/2;
if (orientation === 't') {
var y = tile.row*TILE_SIZE + TILE_SIZE/5/2;
}
else if (orientation === 'b') {
var y = (tile.row+1)*TILE_SIZE - TILE_SIZE/5/2;
}
var temp = {'x':x, 'y':y};
return temp;
};
Flatform.prototype = Object.create(Platform.prototype);
Flatform.prototype.constructor = Flatform;
Flatform.prototype.draw = function() {
var left = this.position.x - this.width/2;
var right = this.position.x + this.width/2;
var top = this.position.y - this.height/2;
var bottom = this.position.y + this.height/2;
var cornerPos = new PVector(left,
(this.orientation === 't') ? top : this.position.y-this.height/2);
noStroke();
fill(this.color);
rect(cornerPos.x, cornerPos.y, this.width, this.height);
};
/**************************************************************************/
var InvisibleFlatform = function(x, y, orientation, defEnabled) {
Flatform.apply(this, arguments);
this.enabled = defEnabled;
this.enabled |= false;
};
InvisibleFlatform.prototype = Object.create(Flatform.prototype);
InvisibleFlatform.prototype.constructor = InvisibleFlatform;
InvisibleFlatform.prototype.effectCallback = function(status) {
this.enabled = !this.enabled;
};
InvisibleFlatform.prototype.draw = function() {
if (this.enabled) {
Flatform.prototype.draw.call(this);
}
};
/***************************************************************************
PLAYER CLASS
***************************************************************************/
var Player = function(x, y, tm) {
Mover.apply(this, arguments);
this.startPosition = new PVector(x, y);
this.position = new PVector(x, y);
this.mass = 10;
this.height = 390;
this.width = 300;
this.size = TILE_SIZE;
this.isAffectedByGravity = true;
this.isAffectedByCollisions = true;
this.onGround = true;
this.movementSpeed = 3;
this.jumpForce = new PVector(0, -25);
this.jumped = 0;
this.maxSpeed = 20;
this.armAngle = 0;
this.playerColor = color(236, 166, 35);
this.direction = 1;
this.sunglassPoints = [{x:138, y:132}, {x:143, y:121}, {x:157, y:135}, {x:170, y:113}, {x:195, y:113}, {x:197, y:136}, {x:208, y:135}, {x:220, y:114}, {x:238, y:115}, {x:245, y:133}, {x:252, y:120}, {x:258, y:130}, {x:247, y:147}, {x:238, y:167}, {x:212, y:164}, {x:204, y:146}, {x:192, y:147}, {x:185, y:164}, {x:161, y:164}, {x:155, y:146}, ];
this.subdivsLeft = 1;
//{ Character Vertices
this.hairVertices = [{x:266, y:94}, {x:235, y:88}, {x:208, y:90}, {x:183, y:108}, {x:176, y:136}, {x:171, y:180}, {x:148, y:195}, {x:134, y:207}, {x:115, y:205}, {x:96, y:167}, {x:94, y:131}, {x:81, y:127}, {x:74, y:144}, {x:75, y:165}, {x:83, y:198}, {x:35, y:208}, {x:46, y:198}, {x:41, y:172}, {x:55, y:136}, {x:60, y:112}, {x:80, y:105}, {x:96, y:118}, {x:106, y:93}, {x:132, y:73}, {x:178, y:53}, {x:233, y:55}, {x:254, y:74}, {x:274, y:84}, {x:264, y:89},];
this.hairVertices_bw = [{x:134, y:94}, {x:165, y:88}, {x:192, y:90}, {x:217, y:108}, {x:224, y:136}, {x:229, y:180}, {x:252, y:195}, {x:266, y:207}, {x:285, y:205}, {x:304, y:167}, {x:306, y:131}, {x:319, y:127}, {x:326, y:144}, {x:325, y:165}, {x:317, y:198}, {x:365, y:208}, {x:354, y:198}, {x:359, y:172}, {x:345, y:136}, {x:340, y:112}, {x:320, y:105}, {x:304, y:118}, {x:294, y:93}, {x:268, y:73}, {x:222, y:53}, {x:167, y:55}, {x:146, y:74}, {x:126, y:84}, {x:136, y:89}];
this.armLeftVertices = [{x:148, y:241}, {x:151, y:275}, {x:165, y:297}, {x:185, y:302}, {x:189, y:282}, {x:177, y:257}, {x:179, y:244},];
this.armLeftVertices_bw = [{x:252, y:241}, {x:249, y:275}, {x:235, y:297}, {x:215, y:302}, {x:211, y:282}, {x:223, y:257}, {x:221, y:244}];
this.armRightVertices = [{x:213, y:251}, {x:219, y:285}, {x:235, y:305}, {x:255, y:308}, {x:259, y:287}, {x:244, y:264}, {x:243, y:236},];
this.armRightVertices_bw = [{x:187, y:251}, {x:181, y:285}, {x:165, y:305}, {x:145, y:308}, {x:141, y:287}, {x:156, y:264}, {x:157, y:236}];
this.legVertices = [{x:150, y:310}, {x:158, y:330}, {x:156, y:361}, {x:215, y:361}, {x:214, y:344}, {x:227, y:338}, {x:215, y:342},/*Short-Leg{x:190, y:363}*/ {x:190, y:363}, {x:250, y:361}, {x:250, y:337}, {x:246, y:322}, {x:250, y:310}, {x:200, y:310}, ];
this.legVertices_bw = [{x:250, y:310}, {x:242, y:330}, {x:244, y:361}, {x:185, y:361}, {x:186, y:344}, {x:173, y:338}, {x:185, y:342}, {x:210, y:363}, {x:150, y:361}, {x:150, y:337}, {x:154, y:322}, {x:150, y:310}, {x:200, y:310}];
this.legAngleOffset = 20;
this.legAngleOffsetCounter = this.legAngleOffset/2;
this.bootLeftVertices = this.getBootVertices(185, 377);
this.bootLeftVertices_bw = this.getBootVertices_bw(185, 377);
this.bootRightVertices = this.getBootVertices(220, 377);
this.bootRightVertices_bw = this.getBootVertices_bw(220, 377);
//}
};
Player.prototype = Object.create(Mover.prototype);
Player.prototype.constructor = Player;
Player.prototype.resetPlayer = function() {
this.position.x = this.startPosition.x;
this.position.y = this.startPosition.y;
};
Player.prototype.changeColor = function(col) {
this.playerColor = (col === 0) ? color(236, 165, 15) :
(col === 1) ? color(185, 0, 0) :
(col === 2) ? color(80, 0, 255) :
(col === 3) ? color(150, 200, 20) :
(col === 4) ? color(228, 232, 16) :
color(236, 165, 15);
};
Player.prototype.getBootVertices = function(center_X, center_Y) {
return [{x:center_X+60, y:center_Y+13}, {x:center_X+40, y:center_Y+-2}, {x:center_X+30, y:center_Y+-17}, {x:center_X+27, y:center_Y+-17}, {x:center_X+-30, y:center_Y+-17}, {x:center_X+-30, y:center_Y+-17}, {x:center_X+-30, y:center_Y+18}, {x:center_X+-30, y:center_Y+18}, {x:center_X+30, y:center_Y+18}, {x:center_X+40, y:center_Y+18}];
};
Player.prototype.getBootVertices_bw = function(center_X, center_Y) {
return [{x:center_X-60, y:center_Y+13}, {x:center_X-40, y:center_Y+-2}, {x:center_X-30, y:center_Y+-17}, {x:center_X-27, y:center_Y+-17}, {x:center_X+30, y:center_Y+-17}, {x:center_X+30, y:center_Y+-17}, {x:center_X+30, y:center_Y+18}, {x:center_X+30, y:center_Y+18}, {x:center_X-30, y:center_Y+18}, {x:center_X-40, y:center_Y+18}];
};
Player.prototype.moveArms = function() {
var frontArm = (this.direction === -1) ? this.armRightVertices_bw : this.armLeftVertices;
var backArm = (this.direction === -1) ? this.armLeftVertices_bw : this.armRightVertices;
var angleIncrement = 0.05*((this.legAngleOffsetCounter<0) ? -1 : 1);
if (this.legAngleOffsetCounter <= -this.legAngleOffset-1) {
this.legAngleOffsetCounter = this.legAngleOffset;
}
var leftArmPivotPoint = {x:(frontArm[0].x+frontArm[frontArm.length-1].x)/2, y:frontArm[0].y};
for (var i = 1; i < frontArm.length; i++) {
rotateAroundPoint(frontArm[i], leftArmPivotPoint, angleIncrement);
}
var rightArmPivotPoint = {x:(backArm[0].x+backArm[backArm.length-1].x)/2, y:backArm[0].y};
for (var i = 1; i < backArm.length; i++) {
rotateAroundPoint(backArm[i], rightArmPivotPoint, -angleIncrement);
}
this.armAngle += angleIncrement;
};
Player.prototype.rotateLegsByNumOfIncrement = function(incs) {
var legs = (this.direction === -1) ? this.legVertices_bw : this.legVertices;
var bootLeft = (this.direction === -1) ? this.bootRightVertices_bw : this.bootLeftVertices;
var bootRight = (this.direction === -1) ? this.bootLeftVertices_bw : this.bootRightVertices;
var angleIncrement = 0.1*incs;
var leftLegPivotPoint = {x:(legs[4].x+legs[1].x)/2, y:legs[1].y};
rotateAroundPoint(legs[2], leftLegPivotPoint, angleIncrement);
rotateAroundPoint(legs[3], leftLegPivotPoint, angleIncrement);
for (var i = 0; i < bootLeft.length; i++) {
rotateAroundPoint(bootLeft[i], leftLegPivotPoint, angleIncrement);
}
var rightLegPivotPoint = {x:(legs[8].x+legs[9].x)/2, y:legs[1].y};
rotateAroundPoint(legs[7], legs[5], -angleIncrement);
rotateAroundPoint(legs[8], legs[5], -angleIncrement);
rotateAroundPoint(legs[9], legs[5], -angleIncrement);
for (var i = 0; i < bootRight.length; i++) {
rotateAroundPoint(bootRight[i], legs[5], -angleIncrement);
}
};
Player.prototype.rotateArmsByAngle = function(theta) {
var armLeft = (this.direction === -1) ? this.armRightVertices_bw : this.armLeftVertices;
var armRight = (this.direction === -1) ? this.armLeftVertices_bw : this.armRightVertices;
var leftArmPivotPoint = {x:(armLeft[0].x+armLeft[armLeft.length-1].x)/2, y:armLeft[0].y};
for (var i = 1; i < armLeft.length; i++) {
rotateAroundPoint(armLeft[i], leftArmPivotPoint, theta);
}
var rightArmPivotPoint = {x:(armRight[0].x+armRight[armRight.length-1].x)/2, y:armRight[0].y};
for (var i = 1; i < armRight.length; i++) {
rotateAroundPoint(armRight[i], rightArmPivotPoint, -theta);
}
};
Player.prototype.moveLegs = function() {
var angleIncrement = (this.legAngleOffsetCounter<0) ? -1 : 1;
this.rotateLegsByNumOfIncrement(angleIncrement);
this.legAngleOffsetCounter = (this.legAngleOffsetCounter-1 < -this.legAngleOffset) ? this.legAngleOffset-1 : this.legAngleOffsetCounter-1;
};
Player.prototype.walk = function() {
this.moveArms();
this.moveLegs();
};
Player.prototype.jumpReset = function() {
var angleIncrement = 3 * TWO_PI / 4;
this.rotateArmsByAngle(angleIncrement);
this.armAngle -= TWO_PI/4;
};
Player.prototype.armAngleReset = function() {
var angleIncrement = -1 * this.armAngle;
this.rotateArmsByAngle(angleIncrement);
this.armAngle = 0;
};
Player.prototype.legAngleReset = function() {
if (this.legAngleOffsetCounter >= 0) {
var base = this.legAngleOffset/2;
var base_offset = this.legAngleOffsetCounter - base;
this.rotateLegsByNumOfIncrement(base_offset);
this.legAngleOffsetCounter = this.legAngleOffset/2;
}
else {
var base = -this.legAngleOffset/2;
var base_offset = base - this.legAngleOffsetCounter-2;
this.rotateLegsByNumOfIncrement(base_offset);
this.legAngleOffsetCounter = this.legAngleOffset/2;
}
};
Player.prototype.jump = function() {
var angleIncrement = TWO_PI / 4;
this.armAngleReset();
this.legAngleReset();
this.rotateArmsByAngle(angleIncrement);
this.armAngle += angleIncrement;
};
Player.prototype.resolveLanding = function() {
if (this.jumped > 0) {
this.jumped = 0;
this.jumpReset();
}
};
Player.prototype.keyboardCallback = function() {
if (KEYS[87] === 1) { // w was pressed - Jump
if (this.jumped < 2) {
this.applyForce(this.jumpForce);
this.jump();
this.jumped++;
}
KEYS[87] = 0;
}
if (KEYS[65] === 1) { // a was pressed - Walk Left
var inAirTurn = false;
if (this.direction === 1 && this.onGround) {this.legAngleReset();this.armAngleReset();}
else if (this.direction === 1) {this.jumpReset(); inAirTurn = true;}
this.direction = -1;
if (inAirTurn) {this.jump();}
if (this.onGround) {
this.walk();