-
Notifications
You must be signed in to change notification settings - Fork 0
/
particle.js
145 lines (127 loc) · 3.76 KB
/
particle.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
function Particle(_x, _y, _size, _color, target_x, target_y) {
this.position = new p5.Vector(_x, _y);
this.velocity = new p5.Vector(0, 0);
this.acc = new p5.Vector(0, 0);
this.target = new p5.Vector(target_x ?? _x, target_y ?? _y);
this.size = _size;
this.shape = random(4);
this.mapped_angle =
map(_x, 0, width, -180, 180) + map(_y, 0, height, -180, 180);
this.color = _color;
this.maxForce = random(MIN_FORCE, MAX_FORCE);
this.inBox = false;
this.goToTarget = function () {
let steer = new p5.Vector(this.target.x, this.target.y);
let distance = dist(
this.position.x,
this.position.y,
this.target.x,
this.target.y
);
if (distance > 0.5) {
let distThreshold = 20;
steer.sub(this.position);
steer.normalize();
steer.mult(
map(min(distance, distThreshold), 0, distThreshold, 0, this.maxForce)
);
this.acc.add(steer);
}
};
this.avoidMouse = function () {
if (this.inBox) return;
if (!mousePressed) return;
let mx = mouseX;
let my = mouseY;
let mouseDistance = dist(this.position.x, this.position.y, mx, my);
if (mouseDistance < REPULSION_RADIUS * 0.5) {
this.inBox = true;
SCORE++;
// class score 인 부분 업데이트
document.getElementById("score").innerHTML = `${SCORE}/${particleCount}`;
} else if (mouseDistance < REPULSION_RADIUS) {
let repulse = new p5.Vector(this.position.x, this.position.y);
repulse.sub(mx, my);
repulse.mult(
map(mouseDistance, REPULSION_RADIUS, 0, 0, REPULSION_STRENGTH)
);
this.acc.add(repulse);
}
};
this.followBox = function () {
if (this.inBox) {
let mx = mouseX;
let my = mouseY;
let mouseDistance = dist(this.position.x, this.position.y, mx, my);
if (mouseDistance > REPULSION_RADIUS * 0.5) {
let repulse = new p5.Vector(this.position.x, this.position.y);
repulse.sub(mx, my);
repulse.mult(
map(mouseDistance, REPULSION_RADIUS * 0.5, 0, 0, REPULSION_STRENGTH)
);
this.acc.add(repulse);
}
}
};
this.move = function () {
this.goToTarget();
this.avoidMouse();
this.followBox();
this.velocity.mult(0.75);
this.velocity.add(this.acc);
this.position.add(this.velocity);
this.acc.mult(0.15);
};
this.draw = function () {
push();
noStroke();
translate(this.position.x, this.position.y);
const spin = this.velocity.mag() * SPIN_MULTIPLIER;
rotate(radians(this.mapped_angle + spin));
fill(!this.inBox ? this.color : color(100, 50, 255));
if (this.shape >= 2) {
push();
fill(255, 255, 255, 230);
// 눈 모양 그리기
snowGen(this.size * 2);
pop();
} else if (this.shape >= 1) {
fill(255, 255, 255, 230);
// 육각형 모양 그리기
beginShape();
vertex(0, -this.size / 2);
vertex(-this.size / 2, -this.size / 4);
vertex(-this.size / 2, this.size / 4);
vertex(0, this.size / 2);
vertex(this.size / 2, this.size / 4);
vertex(this.size / 2, -this.size / 4);
endShape(CLOSE);
} else {
ellipse(0, 0, this.size * 0.5, this.size * 0.5);
}
pop();
};
}
// 재귀함수로 눈의 가지를 그리는 함수
function snowGenPart(size) {
noStroke();
push();
rect(0, 0, size, size * 0.05);
rotate(radians(90));
rect(0, size * 0.15, size * 0.2, size * 0.05);
rect(0, size * 0.35, size * 0.2, size * 0.05);
rect(0, -size * 0.15, size * 0.2, size * 0.05);
rect(0, -size * 0.35, size * 0.2, size * 0.05);
pop();
}
function snowGen(size) {
push();
fill(255, 255, 255, 230);
snowGenPart(size);
rotate(radians(60));
snowGenPart(size);
rotate(radians(60));
snowGenPart(size);
rotate(radians(60));
pop();
}