xxxxxxxxxx
86
class Ball{
constructor(x,y,corona,ix,iy){
this.radius = 35;
this.x = x;
this.y = y;
this.corona = corona;
this.colour = color(239,120,255);
this.ix = ix;
this.iy = iy;
}
draw(){
if(this.corona === "positive"){
this.colour = color(255, 0, 0);
}else{
this.colour = color(239,120,255);
}
noStroke();
fill(this.colour);
ellipse(this.x,this.y,this.radius);
}
infect(obj){
if(dist(this.x,this.y,obj.x,obj.y) <= this.radius && this.corona === "positive" || obj.corona === "positive"){
this.corona = "positive";
obj.corona = "positive";
}
}
move(){
this.x += this.ix;
this.y += this.iy;
if(this.ix > 5){
this.ix = 5;
}
if(this.ix < -5){
this.ix = -5;
}
if(this.iy > 5){
this.iy = 5;
}
if(this.iy < -5){
this.iy = -5;
}
}
colWall(){
if (this.x + this.radius > width) {
this.x = width-this.radius;
this.ix *= -1;
} else if (this.x - this.radius < 0) {
this.x = this.radius;
this.ix *= -1;
}
if (this.y + this.radius > height) {
this.y = height-this.radius;
this.iy *= -1;
} else if (this.y - this.radius < 0) {
this.y = this.radius;
this.iy *= -1;
}
}
colBall() {
for (let i = 0; i < 15; i++) {
let dx = balls[i].x - this.x;
let dy = balls[i].y - this.y;
let distance = sqrt(dx * dx + dy * dy);
let minDist = balls[i].radius/2 + this.radius/2 + 1;
if (distance < minDist) {
let angle = atan2(dy, dx);
let targetX = this.x + cos(angle) * minDist;
let targetY = this.y + sin(angle) * minDist;
let ax = (targetX - balls[i].x) * 0.1;
let ay = (targetY - balls[i].y) * 0.1;
this.ix -= ax;
this.iy -= ay;
balls[i].ix += ax;
balls[i].iy += ay;
}
}
}
}