class Particle {
constructor(pos = [], enemies = []) {
this.pos = pos;
this.enemies = enemies;
const maxVel = 3.8;
this.vel = [rand(-maxVel, maxVel), rand(-maxVel, maxVel)];
}
checkNeigborhood(points, context) {
for (let point of points) {
const [p_x, p_y, p_r] = point.pos;
const [x, y, r] = this.pos;
const d = dist2D([p_x, p_y], [x, y]);
if (d < minDist && d > 0.01) {
line([p_x, p_y], [x, y], context, {
color: 'rgba(150,150,150,0.2)',
lineWidth: 0.5
});
}
if (d > 0.01 && d <= p_r + r) {
const direction = [p_x - x, p_y - y];
const dirLen = Math.hypot(...direction);
this.vel = [
((-15 / 10) * (p_x - x)) / dirLen,
((-15 / 10) * (p_y - y)) / dirLen
];
}
}
for (let enemy of this.enemies) {
const d = dist2D(enemy.pos, this.pos);
if (d < enemy.pos[2] + enemyDist) {
const [e_x, e_y] = enemy.pos;
const [x, y] = this.pos;
const direction = [e_x - x, e_y - y];
const dirLen = Math.hypot(...direction);
this.vel = [
(-fleeFactor * (e_x - x)) / dirLen,
(-fleeFactor * (e_y - y)) / dirLen
];
}
}
}
checkBorders() {
const [x, y, r] = this.pos;
const [vx, vy] = this.vel;
if (x < -width / 2 + r || x > width / 2 - r) this.vel = [-vx, vy];
if (y < -height / 2 + r || y > height / 2 - r) this.vel = [vx, -vy];
}
step() {
const [x, y, r] = this.pos;
const [vx, vy] = this.vel;
this.pos = [x + vx, y + vy, r];
this.checkBorders();
}
show(context, points) {
this.checkNeigborhood(points, context);
circle(this.pos, context);
}
}