-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.js
112 lines (94 loc) Β· 3.07 KB
/
box.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
class Box {
constructor(x,y,t,l){
// represents is left hand or right
this.l = l;
// represents the gesture
this.bType = t;
// the width of the box
this.w = 50;
this.scored = false;
this.shrinking = false;
this.emojiSize = 48;
this.particleAdded = false;
this.body = Bodies.circle(x, y, this.w, { friction: 0, frictionAir: 0 });
this.v = 1;
Body.setVelocity(this.body, { x: 0, y: this.v });
Composite.add(engine.world, this.body);
}
// Function to get the appropriate emoji for the gesture
getGestureEmoji(gesture) {
const emojiMap = {
"Closed_Fist": "π",
"Open_Palm": "ποΈ",
"Pointing_Up": "βοΈ",
"Thumb_Down": "π",
"Thumb_Up": "π",
"Victory": "βοΈ",
"ILoveYou": "π€"
};
return emojiMap[gesture] || "β"; // Return default emoji if gesture not found
}
shrink() {
if (this.shrinking) {
this.emojiSize *= 0.95;
if (this.emojiSize < 5) {
this.removeBody();
}
}
}
show() {
let position = this.body.position;
let angle = this.body.angle;
rectMode(CENTER);
push();
fill(127);
stroke(0);
strokeWeight(0.5);
translate(position.x, position.y);
rotate(angle);
// Flip the emoji horizontally if it's a right hand gesture
if (!this.l) {
scale(-1, 1); // Flip horizontally
}
ellipse(0, 0, this.r * 2);
// Draw the emoji inside the circle
textAlign(CENTER, CENTER);
textSize(this.emojiSize); // Adjust text size for the emoji
text(this.getGestureEmoji(this.bType), 0, 0);
pop();
this.shrink();
// // Display gesture text above the box
// push();
// fill(0); // set text color
// textSize(16); // set text size
// textAlign(CENTER, CENTER); // center the text
// text(this.bType, position.x, position.y - this.w / 2 - 10); // display the gesture name above the box
// pop();
}
checkEdge() {
return this.body.position.y > canvasH + this.w;
}
// This function removes a body from the Matter.js world.
removeBody() {
Composite.remove(engine.world, this.body);
}
checkLine() {
if (this.body.position.y > canvasH * 0.75 - this.w / 2 &&
this.body.position.y < canvasH * 0.75 + this.w / 2) {
if (!this.particleAdded) {
let i;
for (i = 0; i < 10; i++){
particleSystem.addParticle(this.body.position.x, this.body.position.y); // Add particles on tap
}
this.particleAdded = true;
}
// update score when gesture matched
if (this.scored == false) {
score += 100;
this.scored = true;
}
return true;
}
return false;
}
}