-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfruit.js
61 lines (51 loc) · 1.56 KB
/
fruit.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
const fruitHeirarchy = Array("cherry", "strawberry", "grape",
"dekopon", "orange", "apple",
"pear", "peach", "pineapple",
"honeydew", "watermelon");
function clamp (min, max, val) {
return Math.max(min, Math.min(val, max));
}
function Fruit(x, y, type) {
var options = {
restitution: 0.3,
friction: 1
};
this.x = x
this.y = y;
this.type = type;
this.r = fruitTypes[type].r;
this.rgb = fruitTypes[type].rgb;
this.idx = fruitTypes[type].idx;
this.points = fruitTypes[type].points;
this.dropped = false;
this.combined = function () {
this.body = Bodies.circle(this.x, this.y, this.r, options);
this.dropped = true;
Composite.add(world, this.body);
}
this.drop = function () {
var xPos = clamp((width - boxWidth + boxThickness) / 2 + this.r, (width + boxWidth - boxThickness) / 2 - this.r, mouseX);
this.body = Bodies.circle(xPos, this.y, this.r, options);
this.dropped = true;
Composite.add(world, this.body);
}
this.show = function () {
push();
if (!this.dropped) {
var xPos = clamp((width - boxWidth + boxThickness) / 2 + this.r, (width + boxWidth - boxThickness) / 2 - this.r, mouseX);
translate(xPos, this.y);
}
else {
var pos = this.body.position;
var angle = this.body.angle;
this.x = pos.x;
this.y = pos.y;
translate(this.x, this.y);
rotate(radians(angle));
}
stroke(this.rgb);
fill(this.rgb);
circle(0, 0, this.r * 2);
pop();
};
}