class Particle {
constructor(params) {
this.acceleration = p5Instance.createVector(0, 0);
this.velocity = P5.Vector.random2D();
this.position = p5Instance.createVector(
Math.random() * width,
Math.random() * height
);
this.colRowIndex = [0, 0];
this.color = ["#ffa822", "#134e6f", "#ff6150", "#1ac0c6", "#dee0e6"];
}
update() {
this.velocity.add(this.acceleration);
this.position.add(this.velocity);
this.velocity.limit(max_speed);
this.acceleration.setMag(0);
}
applyForce(force) {
this.acceleration.add(force);
}
follow() {
this.applyForce(
flowfield[roundAbs(this.position.x / size)][
roundAbs(this.position.y / size)
]
);
}
show(context) {
context.fillStyle = this.color[randInt(0, 4)];
context.fillRect(this.position.x, this.position.y, 3, 3);
}
goBackToField() {
if (this.position.x > width) {
this.position.x = 0;
} else if (this.position.x < -size) {
this.position.x = width - 1;
}
if (this.position.y > height) {
this.position.y = 0;
} else if (this.position.y < -size) {
this.position.y = height - 1;
}
}
}